Problem with scanner.nextInt in do-while loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problem with scanner.nextInt in do-while loops

Hi everybody, i'm currently working on the "Do-while" test with the call center. I just can't seem to figure out how to print the string e.g. 1 for Language support. I can get my code to enter any number and keep it going until the user enters "0". I really thought that I had cracked it, but it doesn't work... Would appreciate any help! import java.util.Scanner; public class DoWhile { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); int nummber = scanner.nextInt(); do { number = scanner.nextInt(); if (number == 1){ System.out.println ("Language selection"); if (number == 2); System.out.println("Customer support"); if (number == 3) System.out.println("Check the balance"); if (number == 4) System.out.println("Check loan balance"); if (number == 0) System.out.println("Exit"); } } while(number !=0); } }

19th Jan 2021, 6:03 PM
Binh Tan Nguyen
Binh Tan Nguyen - avatar
8 Answers
0
I made this if you get confused about anything just ask 👍 https://code.sololearn.com/caj7RiiAjgsg/?ref=app
19th Jan 2021, 9:40 PM
D_Stark
D_Stark - avatar
+ 3
variable naming error braces missing on for loops if and else statements should be used https://code.sololearn.com/cnCAY6gxW3PX/?ref=app
19th Jan 2021, 6:21 PM
D_Stark
D_Stark - avatar
+ 1
thank you so much! It all make sense. As you said in the first comment I didnt use the else if statements. Furthermore I didnt think of using a boolean value. Appreciate your willing to help a beginner like me 🙏
20th Jan 2021, 6:32 AM
Binh Tan Nguyen
Binh Tan Nguyen - avatar
+ 1
Binh Tan Nguyen no probelm 👍👍
20th Jan 2021, 7:55 AM
D_Stark
D_Stark - avatar
+ 1
Okay with a fresh mind this morning I was able to figure this out, now boolean needed. The main issue I had is as someone stated, the scanner input needs to be assigned IN the do while statement, so it can take a new input each time. Also, i remembered that the first line after the while requirement is printed when the while requirement is not met. So when the input is 0, the while requirement fails, and the first line after is printed, in this case "Exit". import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int number; do { number = scanner.nextInt(); if (number == 1){ System.out.println ("Language selection"); } else if(number == 2){ System.out.println("Customer support"); } else if(number == 3){ System.out.println("Check the balance"); } else if(number == 4){ System.out.println("Check loan balance"); } } while(number != 0); System.out.println("Exit"); } }
10th Apr 2021, 3:49 PM
HoneyBadJer
HoneyBadJer - avatar
0
D_Stark I still can't seem to run the code accordingly... This is the problem description: You are creating an automated phone system for bank clients. Number selections should activate the actions noted below as follows: 1 => Language selection 2 => Customer support 3 => Check account balance 4 => Check loan balance 0 => Exit You can use the first 4 commands in a random sequence without interrupting the phone call - only the number 0 does. Write a program that will continuously take a number as input and output the corresponding message, until the client enters 0. Sample Input 1 4 3 0 Sample Output Language selection Check loan balance Check the balance Exit
19th Jan 2021, 7:15 PM
Binh Tan Nguyen
Binh Tan Nguyen - avatar
0
D_Stark. Forgive my ignorance. But I'm having trouble understanding what purpose the boolean serves? I am currently having issues with this problem myself. Complete newbie to coding.
10th Apr 2021, 2:30 AM
HoneyBadJer
HoneyBadJer - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc = new Scanner(System.in); do{ int x = sc.nextInt(); if(x == 1){System.out.println("Language selection");} else if(x == 2){System.out.println("Customer support");} else if(x == 3){System.out.println("Check the balance");} else if(x == 4){System.out.println("Check loan balance");} else if(x == 0){System.out.println("Exit");} else{ } } while (sc.hasNextInt()); } }
11th Aug 2021, 10:27 PM
Julian Betancourt
Julian Betancourt - avatar