I am accepting input from user in the form of a string (which contains both char & int) .I am further dividing string into char & int and using char in case statement,but my case statement giving me infinite loop.I am not able to figure out the error..
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] part = str.split("\\s+");
do
{
switch(part[0])
{
case "AB":
System.out.println("hi");
int data=Integer.parseInt(part[1]);
System.out.println(data);
break;
case "PR":
System.out.println("printining");
break;
case "AE":
System.out.println("AE");
break;
case "EXIT":
System.exit(0);
break;
}
}
while(true);
}
You don't change part inside the loop; so unless you enter EXIT initially, it's going to keep looping.
Move the two lines above the loop into the loop:
do
{
String str = sc.nextLine();
String[] part = str.split("\\s+");
// Rest of loop body.
break statement quit the switch not the loop, and the loop condition is ALWAYS true!