0

I'm doing some basic homework, and it's honestly all stuff I know. But I decided to jazz it up a little, and something I'm missing is causing an unexpected error. The idea is to use a while loop which asks if the user would like to do something. So long as they say yes, the loop continues the operations that it holds (in this case rounding a decimal to an int). However, as soon as I enter yes, or anything really, the loop breaks there and won't continue on to the rounding portion.

public class DecimalRounder {

   //Main class method that starts and ends the program. It is prepared to throw an IO exception if need be.
    public static void main(String args[])throws IOException
    {
        //Main initializes a new reader to take input from System.in
        InputStreamReader rawInput = new InputStreamReader(System.in);
        //Main then initializes a new buffer to buffer the input from System.in
        BufferedReader bufferedInput = new BufferedReader(rawInput);
        //Main initializes other used variable
        double castInput = 0.0;
        String contin = "yes";

        //Program then sets up loop to allow user to round numbers till content
        while (contin == "yes")
        {

            //Program asks user if they'd like to round.
            System.out.println("Would you like to round? Type yes to continue... ");

            contin = bufferedInput.readLine();
            //If user says yes, rounding begins. ERROR FOUND HERE?

            if (contin == "yes") //INPUT "yes" DOESN'T MATCH?
            {
                //Program then requests a decimal number
                System.out.println("Please enter a decimal number for rounding: ");
                String givenLine = bufferedInput.readLine();

                //rawInput is worked on using a try catch statement
                try {
                    //givenLine is first parsed from String into double.
                    castInput = Double.parseDouble(givenLine);
                    //castInput is then rounded and outputted to the console
                    System.out.println("Rounded number is... " + Math.round(castInput));
                    //Branch then ends restarting loop.
                }catch(Exception e){
                    //If the data entered cannot be cast into a double, an error is given
                     System.err.println("That is not a roundable number: " + e.getMessage());
                    //And branch ends restarting loop.
                }
            }
        }
        System.out.println("Have a nice day!");
    }
}
Day64
  • 1

1 Answers1

0

Use .equals instead of == to compare strings in JAVA. Try this :

contin.equals("yes") 
Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
  • Fantastic, I have no idea how I missed that. I apologize for what I realize now is a duplicate question. It really didn't dawn on me that is was the string comparison where the issue was occurring. – Day64 Jun 15 '16 at 21:24
  • @Day64 : No problem. Since you are a newbie,Its a bit difficult to find out that. But here you should use your debugging skills. First you should have tried out printing something just inside while loop. You would have seen that it is not getting printed. Then you should have tried to print contin == "yes" outside while loop. You would have seen false. And then you would have figured out that there is some problem in comparing strings and then a google search would have helped you :) – Priyansh Goel Jun 16 '16 at 08:48