user3345791
user3345791

Reputation: 105

end java program when input 'q'

i want to end my java program if i input q or Q. otherwise, the program will continue running, printing the question and asking for input. will this work?

Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
System.out.print("Enter departure city <'Q' or 'q' to exit>: ");
String input = sc.next();

if(!(input.equals('q')) || !(input.equals('Q'))){
//system continues
}
else{
System.exit(0);
}
}

Upvotes: 1

Views: 5610

Answers (1)

ARM
ARM

Reputation: 11

String input = "";

while (!input.toUpperCase().equals("Q"))
{
   System.out.print("Enter departure city. <'Q' or 'q' to exit>: ");
   input = sc.next();
}

Exit after the while loop.

Upvotes: 1

Related Questions