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+" ");
        }
      }

}

replace the following statement (at line no 21 in BufferedReader version )

StringTokenizer tokens = new StringTokenizer(data,"!.,", true);

with the bellow statement in BufferedReader version

StringTokenizer tokens = new StringTokenizer(data);

juct check it once

(the only difference is in BufferedReader version you have passed some delimiters to parse the input string but in Scanner version you used default delimiter)

commented: Bad advice -3

there is no problem with try and catch blocks
i think you got the answer

Actually I need those parameters to prevent punctuation marks to be reversed also. Do you have any alternative way to do it? Because it's also included in the exercise.

pls first you run the code by giving the whatever input you want

then check the output once

Yah, I did, if I remove ("!.,", true) in the parameter the output becomes "olleH !dlroW" by inputting "Hello World!". I needed the output "olleH dlroW!". Yeah, I forgot to include that on the scanner version, my bad. But with those parameters on the scanner version, it's working perfectly. You can also try it. Thanks for helping by the way, really appreciate it.

we can get the your desired output by comparing value of element variable like as follows

what i mean is just check whether the element variable contains the following charecters just follow the code once (remove line no 25 and place the following code)

if(buffer.toString().contains(",") || buffer.toString().contains(".") || buffer.toString().contains("!") ){
       element = buffer.reverse().toString();
       element = element.substring(1,element.length())+ element.charAt(0);
}else{
              element = buffer.reverse().toString();
}

YES ! That did the trick. Thank You very much for the help. I really appreciate it. Didn't know that there's such thing. Thanks again :)

First off, if you don't need the synchronization provided by a StringBuffer, then prefer using a StringBuilder.
Here's what the Java API states about StringBuilder:

This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.

  1. Provides an API compatible with StringBuffer;
  2. Designed for use as a drop-in replacement for StringBuffer;
  3. It is recommended that this class be used in preference to StringBuffer;
  4. Will be faster under most implementations;

Second, the following piece of code can be improved upon from a performance perspective (without writing unreadable code, but by merely applying the concept of avoiding unnecessary String instantiation):

if (buffer.toString().contains(",") || buffer.toString().contains(".") || buffer.toString().contains("!"))
{
    element = buffer.reverse().toString();
    element = element.substring(1, element.length()) + element.charAt(0);
}
else
{
    element = buffer.reverse().toString();
}

There's a possibility that buffer.toString() will be called multiple times in a row, while yielding the same result every time. You have to be careful about this. Every call to a StringBuilder's [1] toString() method will instantiate a new String object! And after all that contradicts with why we initially want to use a StringBuilder: avoid the unnecessary instantiation of String objects.
A more performant way would be making use of other StringBuilder methods to achieve the same. Instead of writing buffer.toString().contains(",") you could for example write buffer.indexOf(",") >= 0. However, I'm actually a bit disappointed to see that StringBuilder does not have an indexOf() method that takes a char.

Similarly what's happening here, is - again - showing the ignorance of a StringBuilder:

element = buffer.reverse().toString();
element = element.substring(1, element.length()) + element.charAt(0);

Why not write?

element = buffer.reverse().append(buffer.charAt(0)).substring(1);

(substring)

[1] Read as 'StringBuffer or StringBuilder' throughout the whole post.

That whole approach to solving it is hideously over-complex (and badly coded as Tux points out). The only problem was that the original delimiter string omitted blank. Ie instead of "!.," it needs to be " !.,"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.