+ 2
Does this app have a limit on how many user input your code can recieve?
I am currently practicing with nested statements and my code tends to have errors with certain parts which I am sure to be syntax and logically sound.
6 Answers
+ 4
There is no limit (that I know of), but because SL requires that all input is received at the beginning of the execution of your program, that may mess with inputs that are places within loops and such
+ 2
Can you show us your code so we can explain to you?
+ 2
One problem is that when taking input in int and next in String it reads pressing enter after number as String. You should add: sc.nextLine(); before taking next input.
The other problem is when you want age as input and you press something else than number and program fails. You could add some verification by using hasNextInt() method.
+ 1
Thank you guys so much for the help. Mostly to PBu, I didnt realize the difference between sc.next() and sc.nextLine()
0
System.out.println("Enter your age here");
Scanner sc = new Scanner(System.in);
String scanned = sc.next();
//Converts user input to integer
int age = Integer.parseInt(scanned);
if (age >= 18){
System.out.print("What is your favorite food?");
String food = sc.nextLine();
if (food.equals("pizza")){
System.out.println("Mine too!");
}
else{
System.out.print("I like other foods");
}
}
else if (age >= 13){
System.out.println("You are a teenager");
}
else{
System.out.println("You are a young child");
}
0
you can rewrite that code this way, and it will works
String food = sc.next();
next() reads string to next space or end of line (as default)
nextLine() reads string to next end of line (as default)