-3

My code is supposed to keep asking the user to type in a string (where the user types in an integer) and convert it to an integer until the user types in -1 which will then make the program break, but I am confused on how to do that?

import java.util.Scanner;
public class StringConverter {
    public static void main(String[] args) {
        boolean flagBoolean = true;
        while (flagBoolean) {
            Scanner keyBoard = new Scanner(System.in);
            System.out.println("Your number:");
            String input = keyBoard.next();
            if (input == -1) {
               break;
            }
            int result = 0;
            for (int i = 0; i < input.length(); i++) {
                result = result * 10 + input.charAt(i) - '0';
            }
            System.out.println("Converted to:" + result);
        }
    }
} 

My problem lies in if (input == -1), since input is a String, which will prevent it from compiling.

Alexander Vogt
  • 17,491
  • 13
  • 49
  • 66
Robben
  • 245
  • 3
  • 11

4 Answers4

3

Just compare it to the string literal "-1". Note that Strings are objects, so it should be compared using the equals method:

String input = keyBoard.next();
if (input.equals("-1")) {
    break;
}
Mureinik
  • 277,661
  • 50
  • 283
  • 320
  • Is there a built in function that I can use in order to verify that my result is indeed an integer? Because I know in python there is a function called `type`. Is there a similar function in Java? – Robben Jan 20 '15 at 05:34
  • @Robben you can use [`Integer.parseInt(Stirng)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)), and catch the `NumberFormatException`. – Mureinik Jan 20 '15 at 05:46
  • Hm, I used your method but I did not catch the `numberformatexception`. Does that mean my code didn't convert the string into an integer? – Robben Jan 20 '15 at 06:04
  • @Robben If the exception was thrown and you didn't catch it (i.e., handle it), your program didn't continue to the parsing part. Note that comments aren't the most comfortable medium to have this conversation - if you modified your code and have a different issue, please open a new question. Thanks! – Mureinik Jan 20 '15 at 06:06
2

If you use keyboard.nextInt() the method will return an integer. the next() method returns a string.

NOTE: If you use nextInt(), the program will throw an exception if what is entered is not an integer. You can use a TRY CATCH to deal with that possibility.

Also, when you say the program needs to break, I assume you mean you need it to end. Putting the break; in your if statement is not the way to do this. Try putting (input != -1) condition in your while instead of a flag. This way your program will exit only when -1 is input by the user. Just remove the if statement.

I hope this helps.

Dhanuka
  • 2,776
  • 5
  • 26
  • 37
sauv0168
  • 113
  • 8
2

Comapre the String you are passing using equals(Object anObject):

String input = keyBoard.next();
if ("-1".equals(input )) {
    break;
}
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
1

I guess for your problem.

input == -1        --> No


input.equals("-1") --> Yes
Dhanuka
  • 2,776
  • 5
  • 26
  • 37
chaester
  • 11
  • 2