0
import java.util.Scanner;

public class StudySession {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    String inData;
    int age;
    System.out.println("Enter your age");
    while(true) {
    inData = in.nextLine();
    if(inData == "STOP") {
        System.out.println("Terminated");
        break;
    }
    age = Integer.parseInt(inData);



    if(age < 5) {
        System.out.println("Under 5's rate.");
    }
    if(age >= 60) {
        System.out.println("Senior citizen rate.");
    }

    if(age <= 17) {
        System.out.println("Child rate.");
    }
    if(age > 17 && age < 65) {
        System.out.println("Adult rate.");
    }
    System.out.println("Enjoy the show!"); //This prints no matter what.

   }
 }
}

I want the program to stop when the user types "STOP". Why is it not working? You don't have to give me a straight up answer. A clue will do. Perhaps it's something to do with the order of my statements?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Stephen-Wisniewski
  • 321
  • 1
  • 2
  • 13
  • ```break``` only breaks out of the if statement, you need to use ```return```. Also, compare strings using equals, not ==. – Siddhartha Oct 19 '15 at 16:59
  • Duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – gonzo Oct 19 '15 at 16:59
  • @Siddhartha `break` is well used here. The problem is in string comparison. – Luiggi Mendoza Oct 19 '15 at 16:59
  • Use `System.exit(0)` – dguay Oct 19 '15 at 16:59
  • @dguay IMO that's a code smell. The fact that *works* doesn't mean it's *right*. – Luiggi Mendoza Oct 19 '15 at 17:00
  • @LuiggiMendoza How is this a smell code? I'm curious. – dguay Oct 19 '15 at 17:00
  • @LuiggiMendoza Actually, ```break``` can't be used here. Tried this locally, got the error ```error: break outside switch or loop``` – Siddhartha Oct 19 '15 at 17:03
  • Okay guys. I have used if(inData.equals("STOP")) { instead of the == sign. The program runs perfectly, but what does this error mean:Exception in thread "main" java.lang.NumberFormatException: For input string: "STOP" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at StudySession.main(StudySession.java:16) – Stephen-Wisniewski Oct 19 '15 at 17:03
  • @dguay imagine the program should just break that part and continue doing something else. `System.exit(0)` will just stop the whole application. The fact that does what OP needs right now doesn't mean it's right. Also, I prefer to use `System.exit` with a number different than 0 to alert that there was a problem, because 0 is for normal or proper termination of the application. – Luiggi Mendoza Oct 19 '15 at 17:04
  • @Siddhartha `break` is inside the `while` loop... – Luiggi Mendoza Oct 19 '15 at 17:04
  • @LuiggiMendoza Gah my bad. I didn't even notice the ```while loop``` ! – Siddhartha Oct 19 '15 at 17:05

0 Answers0