Reputation: 31
I am required to create a method that prompts user to input three words and then it has to store the data in an array. The method should then print the three lines in reverse so for example word OVER would come out as REVO.
I have got it done sort of however I don't know how to get the other 2 lines to work. As it is, only the first user input gets reversed.
Here is the code so far;
import java.io.*;
public class Average {
public static void main (String[] args) throws IOException {
BufferedReader getit;
getit = new BufferedReader
(new InputStreamReader (System.in));
System.out.println ("Enter first line:");
System.out.flush ();
String text = getit.readLine();
while (true) {
System.out.println (reverse(text));
System.out.println("Enter 2nd line:");
System.out.flush ();
text = getit.readLine();
System.out.println("Enter 3rd line:");
System.out.flush ();
text = getit.readLine();
System.out.println("Finish");
break;
}
}
public static String reverse (String original) {
String reversed = "";
int pos = original.length() - 1;
while (pos >= 0) {
reversed = reversed + original.charAt(pos);
pos -= 1;
}
return reversed;
}
}
Upvotes: 0
Views: 2610
Reputation: 1282
You called the function reverse just once. Try to call it every time you get a string.
What you are doing is, You took the first string as input and your program went into an loop near while(true)
you printed the reverse of the string. You took two more strings.
Where are you reversing them? and the break at the end of the loop doesn't make sense.
you may remove the while loop and break and add call the reverse function. I am not writing any code as you could do it easily.
Upvotes: 2
Reputation: 3108
Write System.out.println (reverse(text));
after text = getit.readLine();
each time.
Upvotes: 0