I am very much a beginner in Java. I am making a simple switch, in which the user is entering a number in words. For training I added a "for" loop.
package JavaExc;
import java.util.Scanner;
public class JavaStart {
public static void main(String[] args) {
Scanner sck = new Scanner(System.in);
System.out.println("Type loop counter ");
int counter = sck.nextInt();
System.out.println("Counter is: " + counter );
for (int i = 0; i <= counter; i++) {
System.out.println("Type number in words");
String ch = sck.nextLine();
switch (ch.toLowerCase()) {
case "one":
System.out.println("CHOICE 1");
break;
case "Two":
System.out.println("CHOICE 2");
break;
case "THREE":
System.out.println("CHOICE 3");
break;
case "FoUr":
System.out.println("CHOICE 4");
break;
case "fiVE":
System.out.println("CHOICE 5");
break;
default:
System.out.println("Wrong Data");
break;
}
System.out.println("Loops left:\n " + counter--);
}
System.out.println("End of Switch");
}
}
Here is the result:
Type loop counter
5
Counter is: 5
Type number in words
Wrong Data // ??
Loops left:
5
Type number in words
one
CHOICE 1
Loops left:
4
Type number in words
three // Should be ok, so what is wrong here?
Wrong Data
Loops left:
3
End of Switch //To early break loop I think
Process finished with exit code 0
My questions are: Why first loop make default code? Should I make 2 Scanners? One for take value for counter and second for switch? Why counter and numbers in words do not working properly?
I know I can do it with tables etc. but generally, the purpose of this program is to test the "toLowerCase()" method.