Hello Everybody, i'm having a problem with my code. I'm making a word reverser program though I'm not that much knowledgeable about java. The output that I'm aiming for is something like this "olleH dlroW" by inputting "Hello World" string. I'm getting the output that I'm looking for when I'm using scanner but when I use BufferedReader, i'ts outputting "dlroW olleH". I'm thinking that Try/Catch is the reason why I'm having this problem. My Prof. requires us to use BufferedReader. Please look at my code and correct me. THANKS !
WITH SCANNER
import java.util.StringTokenizer;
public class reverseSent {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.print("\nInput: ");
String data = scan.nextLine();
StringTokenizer tokens = new StringTokenizer(data);
while(tokens.hasMoreTokens()){
String element = tokens.nextToken();
StringBuffer buffer = new StringBuffer(element);
element = buffer.reverse().toString();
System.out.print(element+" ");
}
}
}
WITH BUFFERED READER
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class ReverseSent {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Input : ");
String data = null;
try {
data = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringTokenizer tokens = new StringTokenizer(data,"!.,", true);
while(tokens.hasMoreTokens()){
String element = tokens.nextToken();
StringBuffer buffer = new StringBuffer(element);
element = buffer.reverse().toString();
System.out.print(element+" ");
}
}
}