infinite loop unable to close loop in Switch Statements - java

import java.util.Scanner;
class MenuFastFood {
public static void main(String[] args) {
String s;
char order;
Scanner keyboard = new Scanner(System.in);
s = keyboard.next();
s = s.toUpperCase();
order = s.charAt(0);
do {
switch(order) {
case 'A':
System.out.println("CheeseBurger");
System.out.println("Onion Rings");
System.out.println("Soda");
break;
case 'B':
System.out.println("Hot dog");
System.out.println("Fries");
System.out.println("Milk Shake");
break;
default:
System.out.println("error");
return;
case 'X':
System.out.println("EXIT");
break;
}
}while(order != 'X');
}
my program is suppose to pick an item based on the character enter in to keybaord and then loops back if another item is selected. when i run this and pick an item. it loops that item for ever. How do i get that to stop and makes it able for me to select another item?

Move your code for reading input to inside do..while
s = keyboard.next();
s = s.toUpperCase();
order = s.charAt(0);

Related

How can I make the program stop after entering a value from a user?

I am trying to write a program that receives the number of sides from the
user and determines the type of figure using switch structure and a while sentinel-controlled loop, but every time I get an infinite loop. How can that be fixed?
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of sides:");
int s = input.nextInt();
while ( s!=-1)
{
switch (s)
{
case 1: System.out.println("Line");
break;
case 2:System.out.println("Angle");
break;
case 3:System.out.println("Triangle");
break;
case 4:System.out.println("Quadrilateral");
break;
case 5:System.out.println("Pentagon ");
break;
case 6:System.out.println("Hexagon");
break;
case 7:System.out.println("Heptagon");
break;
case 8:System.out.println("Octagon");
break;
case 9:System.out.println("Nonagon");
break;
case 10:System.out.println("Decagon");
break;
default: System.out.println("Enter a valid value:");
}
}
}
}
The while loop is written to continue as long as s!=-1; so you need to change s so that this expression is no longer true.

How to loop back to start of array after incorrect input

first post here.
I was instructed to change my code to loop back to the beginning of the array and ask the user for input again after they input something invalid (Like 0 or 5 for example).
If someone could point me in the right direction, I would be thankful.
package lepackage;
import java.util.Scanner;
public class SwitchItUp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter menu item:");
int input = scanner.nextInt();
String inputString;
switch (input) {
case 1: inputString = "User want to Enter data";
break;
case 2: inputString = "User want to Display sata";
break;
case 3: inputString = "User want to Print data";
break;
case 4: inputString = "User want to Exit";
break;
default: inputString = "Invalid Number";
break;
}
System.out.println(inputString);
}
}
I'd surround it with a do...while loop
do {
//your code here
} while (!(input > 0 && input < 5));
See it online!
how about using a label here. though It's not the cleaner approach compare to do.. while. see the code . Also don't forget to close the scanner !!
Scanner scanner = new Scanner(System.in);
int input;
badinput: while (true) {
System.out.println("Enter menu item:");
input = scanner.nextInt();
String inputString;
if ((!(input > 0 && input < 5)))
continue badinput;
else {
switch (input) {
//switch case
}
System.out.println(inputString);
break;
}
}
scanner.close();

Looping within a switch/case statement

I've been playing around with basic Java and begun to create a project which includes a user input, system output ad a switch/case statement too. The user inputs a given animal and the system outputs the necessary noise that relates to the animal. At the moment the program asks the user once and then it is finished, however I want the user to be able to input as many animals as they'd like and get appropriate responses. Any help welcome, thanks. (I understand there may be some errors in my code, but I have only really just begun Java.)
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String myString;
System.out.println("Enter your first animal: ");
myString = user_input.next();
loop: switch (myString) {
case "Cow":
System.out.println("Mooo!");
break;
case "Sheep":
System.out.println("Baaaa!");
break;
case "Mouse":
System.out.println("Squeak Squeak!");
break;
case "Horse":
System.out.println("Neighhh!");
break;
case "Goat":
System.out.println("Skreachh!");
break;
case "Fish":
System.out.println("*Bubble Bubble*");
break;
default:
System.out.println("Invalid animal.");
break;
}
}
You need a loop and a condition to stop.
while (true) {
...
switch(...) {
....
case "Stop":
System.out.println("bye bye");
System.exit(0);
break;
}
}
You can also use do-while loop
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
do {
System.out.println("Enter your first animal: ");
myString = input.next();
switch (myString) {
case "Cow":
System.out.println("Mooo!");
break;
........
default:
System.out.println("Invalid animal.");
break;
}
} while (!myString.equals("Stop"));
}
You can get a well-explanation from here and here

How to drop out of a programme in java?

I have made a main menu in my code. When the user types q I want the code to stop running. Instead it just presents the menu again, what code should i use to stop this. If you press any other letter it works.Thank you
import java.util.Random;
public class Aaa {
AQAConsole2016 console = new AQAConsole2016();
Random random = new Random();
int boardSize;
boolean moveIsValid;
char [][] board;
int move;
char choice;
String playerName="Human";
String player2="Computer";
public Aaa() {
boardSize = 6;
do {
displayMenu();
choice = getMenuChoice(playerName);
switch (choice) {
case 'p' : playGame(playerName, boardSize);
break;
case 'e' : playerName = getPlayersName();
break;
case 'c' : boardSize = changeBoardSize();
break;
case 'm' : Multiplayer( boardSize,playerName,player2);
break;
case 'r' :readBoard(board,boardSize);
break;
case 'q' : quit();
}
}while (choice!='p'||choice!='e'||choice!='c'||choice!='m'||choice!='r'||choice!='q');
}
void quit(){
}
this is not going to work just because
case 'q' : quit(); is just a call to a method, i.e. when the method is done it will return to the do while...
you need instead to add some boolean condition that can be modified if the option menu is selected...
doing something like
while (!exit);
where exit is a boolean variable...
and you can in the void method:
void quit(){
//Do your stuff before exit
exit=true;
}

Conflicts between 'for' and 'switch'?

I want to try a little programming that can read user input continuously unless input is 0.
But the problem is whatever I enter (except 0), it always shows "Please choose one" (in default part). If I enter 4, it will show me this phrase twice!
I do not understand why. Is there a conflict between for and switch or something?
Here is code:
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println("Choose one: ");
char ch = (char)System.in.read();
while (ch!= '0') {
switch(ch) {
case '1':
System.out.println("The If");
break;
case '2':
System.out.println("The Case");
break;
default:
System.out.println("Please choose one");
}
ch = (char)System.in.read();
}
The problem is char ch = (char)System.in.read();. Java does not support character based input very well, I recommend using a Scanner which fixes your output, however the user now has to press return after each input.
import java.io.IOException;
import java.util.Scanner;
public class Switch
{
public static void main(String[] args) throws IOException
{
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println("Choose one: ");
Scanner in = new Scanner(System.in);
String s = in.nextLine();
while (!s.equals("0"))
{
switch(s)
{
case "1":
System.out.println("The If");
break;
case "2":
System.out.println("The Case");
break;
default:
System.out.println("Please choose one");
}
s = in.nextLine();
}
}
}
If you don't want to press return, you can also read the character twice, although I can only speculate why this works is that there is a control character sent over the stream. Edit: I thought it could also be another byte of a UTF-16 character which is not used when typing in ASCII characters but System.in.read() returns integers not bytes.
import java.io.IOException;
public class Switch
{
public static void main(String[] args) throws IOException
{
System.out.println("Help on:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println("Choose one: ");
char ch = (char)System.in.read();
while (ch!= '0')
{
switch(ch)
{
case '1':
System.out.println("The If");
break;
case '2':
System.out.println("The Case");
break;
default:
System.out.println("Please choose one");
}
ch = (char)System.in.read();
ch = (char)System.in.read();
}
}
}
System.in.read() reads a byte from the InputStream and returns it. When you type 1 or any single digit number and press enter, it reads two characters.
Try tying multiple digit number to see how System.in.read() behaves.
You should use scanner for the console input:
http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

Categories