0

//when i run the program nothing prints. its like an infinite loop on //on the entering of my string console. //Write a Java program that accepts the input of a string from the console //and reverses it using recursion. Print the result after the string is //reversed.

import java.util.Scanner;

public class ReverseTry {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence and I will reverse it");
        reverseLine(input);
    }

    public static Scanner reverseLine(Scanner input) {
        if (!input.hasNextLine()) {
            return input;
        } else {
            //String word = input.nextLine();
            //return reverseLine(input) + " " + word;

            String line = input.nextLine();
            reverseLine(input);
            System.out.println(line);
        }
        return input;
    }
}
mjuarez
  • 16,372
  • 11
  • 56
  • 73
  • Possible duplicate of [Reverse a string in Java](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – Infern0 Oct 27 '18 at 06:53

2 Answers2

0

you don't need to pass the scanner in function. take a input from user and pass it to recursive function. first pass the string , starting index and the last index. Here is the code :

 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a sentence and I will reverse it : ");
    String line = input.nextLine();
    String reverse = reverseLine(line, 0, line.length() - 1);
    System.out.println(reverse);
  }

Reverseline function take a input and swap input characters at specified positions and call recursively.

  public static String reverseLine(String input, int startIndex, int endIndex) {
    if (startIndex >= endIndex) {
      return input;
    } else {
      input = swap(input, startIndex, endIndex);
      return reverseLine(input, startIndex + 1, endIndex - 1);
    }
  }

this function only swap characters of string.

  public static String swap(String input, int start, int end) {
    char[] arr = input.toCharArray();
    arr[end] = input.charAt(start);
    arr[start] = input.charAt(end);
    return String.valueOf(arr);
  }
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
-1
import java.util.Scanner;

public class ReverseTry {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence and I will reverse it");
        reverseLine(input); //no need to pass Scanner object as input to method since the return value isn't used anyway
    }

    public static Scanner reverseLine(Scanner input) {
        if (!input.hasNextLine()) {
            return input;
        } else {
            //String word = input.nextLine();
            //return reverseLine(input) + " " + word;

            String line = input.nextLine(); 
            reverseLine(input); // after you get the line over here, 
                                //this function will be called again. 
                                //In the recursively called function, 
                                //it will again ask you for input and 
                                //once you give it, again call itself 
                                //and this will go on forever. Instead 
                                //of this endless recursive call, you 
                                //need to give a base condition on which 
                                //this method should return a useful value 
                                //without calling itself. For example, you 
                                //know that there is nothing to be 
                                //reversed in a single character string ...
            System.out.println(line);
        }
        return input;
    }
}

You probably want something like the following,

import java.util.*;

public class ReverseTry {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a sentence and I will reverse it");
    String s = input.nextLine(); //no need to pass Scanner object into the reverseline method. Scanner object is used to read the console input only once
    s = reverseLine(s); // here is where the reversing logic goes
    System.out.println(s); // printing out the reversed string
}

public static String reverseLine(String s) {

    if(s.length() == 1 ) { //base case --> there is nothing to reverse when the string length is 1
        return s;
    }
    return s.substring(s.length()-1)+reverseLine(s.substring(0,s.length()-1)); //recursion logic
}
}
mettleap
  • 1,390
  • 8
  • 17
  • No explanation of what was wrong with the original. Just writing their code for them (when it is probably homework) is pretty poor. – John3136 Oct 27 '18 at 05:21