0
Need hint on asking multiple questions in Java
In Java (1) display a text, (2) ask a question, (3) pick up the answer, (4) ask next question, (5) pick up answer and (6) print result. My program does 2, 3 and 6. Any hints? https://code.sololearn.com/c9JwJCvHoohp/?ref=app
9 Respuestas
+ 1
it works fine on IDE's and on online compilers - you don't need two Scanner objects though
Scanner sc = new Scanner(System.in);
System.out.println("How many letters in the word?");
int userInput = 0;
String letter = null;
try {
userInput = Integer.parseInt(sc.nextLine());
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage() +"\n"+e.toString());
}
System.out.println("What is the 1st letter?");
try {
letter = sc.nextLine();
}
catch (Exception e) {
System.out.println("Error"+e.getMessage());
}
System.out.println("Letters : "+ userInput);
System.out.println("1st letter: " + letter.charAt(0));
also, type your inputs in separate lines in sololean like so:
5
a
+ 4
Use switch statment
+ 1
Not sure why it doesn't work, I'll try compiling something myself and get back to you
+ 1
Thanks again!
Happy new year!
+ 1
Cheese
0
Here in Sololearn, you need to give all required inputs at one time only.
You can use loops for that with switch statements. If 2,3,6 does, why not 1,4,5?
All same but you need repeating,.. specifically , which you are not getting?
string. charAt(0) extracts first letter.
string.length gives the length of string.
No need of 2 Scanner objects.
0
HNNX, thanks very much for optimizing my little test program! Indeed, it works like you say. And I did learn from your answer.
However, I very much want to know if it is possible here, in Sololearn, in Java, to ask the user something and, maybe depending on the answer, ask a second question in a new popup.
Possible?
If not, then I have to tackle my idea from a different angle.
0
Sumit and Jayakrishna, thanks also for your replies! I will check out the swith statement.
0
Nope, unfortunately sololearn takes arguments before runtime and it isn't possible to make it interactive.
Happy I could help :)