0

Java getting user input

how many inputs does the Scanner object accept?

23rd Dec 2019, 7:52 PM
Simon Ihemba
Simon Ihemba - avatar
8 Answers
+ 1
You must be writing something wrong. Also note that using Scanner in sololearn app is kinda clumsy
23rd Dec 2019, 8:39 PM
HNNX 🐿
HNNX 🐿 - avatar
0
1?
23rd Dec 2019, 7:53 PM
Simon Ihemba
Simon Ihemba - avatar
0
D3n as many as you want
23rd Dec 2019, 8:18 PM
Abdol Hashimi
Abdol Hashimi - avatar
0
I wrote three n only one result came back
23rd Dec 2019, 8:29 PM
Simon Ihemba
Simon Ihemba - avatar
0
D3n show me your code!
23rd Dec 2019, 8:33 PM
Abdol Hashimi
Abdol Hashimi - avatar
0
import java.util.Scanner; public class FavoriteSong { public static void main(String[ ] args) { //Input name of Favorite song Scanner fsong = new Scanner(System.in); //Input first line of song Scanner l1 = new Scanner(System.in); //Input second line of song Scanner l2 = new Scanner(System.in); //Input third line of song Scanner l3 = new Scanner(System.in); String fvrtSong = fsong.nextLine(); String line1 = l1.nextLine(); String line2 = l2.nextLine(); String line3 = l3.nextLine(); String shortVerse = line1 +" " + line2 + " " + line3; System.out.println(shortVerse); } } //Bugs to be Debugged
23rd Dec 2019, 8:45 PM
Simon Ihemba
Simon Ihemba - avatar
0
I wouldn't make 3 Scanner objects for this... import java.util.Scanner; public class Song{ public static void main(String[] args){ Scanner sc = new Scanner (System.in); String firstLine, secondLine, thirdLine; System.out.print("enter three lines"); firstLine = sc.nextLine(); secondLine = sc.nextLine(); thirdLine = sc.nextLine(); sc.close(); String verse = firstLine+" "+secondLine+" "+thirdLine; System.out.println(verse); } } explanation: - Use one Scanner to get multiple inputs - Send the user a message otherwise they won't know the program is running - use .close() method on your Scanner object to stop stream - use names that are easy to debug and faster to recognise if you're working with an IDE
23rd Dec 2019, 11:02 PM
HNNX 🐿
HNNX 🐿 - avatar
0
The "Get User Input" section of Java won't unlock ; what do I do ?
12th Apr 2021, 3:10 AM
Chris Christian
Chris Christian - avatar