Need hint on asking multiple questions in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

29th Dec 2019, 4:02 PM
Ger Hemmes
9 Answers
+ 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
29th Dec 2019, 9:01 PM
HNNX 🐿
HNNX 🐿 - avatar
+ 4
Use switch statment
29th Dec 2019, 5:31 PM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 1
Not sure why it doesn't work, I'll try compiling something myself and get back to you
29th Dec 2019, 6:32 PM
HNNX 🐿
HNNX 🐿 - avatar
+ 1
Thanks again! Happy new year!
31st Dec 2019, 1:07 PM
Ger Hemmes
+ 1
Cheese
23rd Sep 2021, 12:07 AM
Riley Ibarra
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.
29th Dec 2019, 8:58 PM
Jayakrishna 🇮🇳
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.
30th Dec 2019, 4:39 PM
Ger Hemmes
0
Sumit and Jayakrishna, thanks also for your replies! I will check out the swith statement.
30th Dec 2019, 4:41 PM
Ger Hemmes
0
Nope, unfortunately sololearn takes arguments before runtime and it isn't possible to make it interactive. Happy I could help :)
30th Dec 2019, 8:47 PM
HNNX 🐿
HNNX 🐿 - avatar