0
I have trouble with my Code
I need your help can you please check my code. Everything is running fine except the selection 1 . it wont let me type my name. can you please tell me whats wrong with my code? https://code.sololearn.com/cPl482KxpH36/?ref=app
1 Respuesta
+ 3
/*
I think you have taken this code from outside SoloLearn due to which there were spaces that were showing the errors. I had removed line 1 which might help you to run the code properly.
Also I have changed your line 17 with int selection =Integer.parseInt(input_obj.nextLine()); because if you will choose case 1 then it will not take input.
The original line is syntactically correct but after scanning the cursor remains after integer and instead of scanning the string it scans "\n". I hope you understood. I have pasted the code below. Please check it and tell me whether it is working or not.
*/
import java.util.*;
class Activity3
{
public static void main(String[] args) {
char user_continue;
Scanner input_obj = new Scanner(System.in);
do {
System.out.println("What do you want to display.");
System.out.println("1. Display your Name");
System.out.println("2. Display your Age");
System.out.println("3. Display your Salary");
System.out.println("Enter the number of your selection:");
int selection =Integer.parseInt(input_obj.nextLine());
if ( selection == 1 ) {
System.out.println("Enter your name:");
String name = input_obj.nextLine();
System.out.println("");
System.out.println("Your name is " +name);
} else if ( selection == 2 ) {
System.out.println("Enter your age:");
int age = input_obj.nextInt();
System.out.println("");
System.out.println("Your age is " + age);
} else if ( selection == 3 ) {
System.out.println("Enter your salary:");
double salary = input_obj.nextDouble();
System.out.println("");
System.out.println("Your salary is " + salary);
} else {
System.out.println("");
System.out.println("Invalid selection! Thank you.");
}
System.out.println("Do you want to continue?");
System.out.println("(Y) Yes | (X) Exit");
user_continue = input_obj.next().charAt(0);
}
while ( user_continue == 'y' || user_continue == 'Y' );
}
}