0
Java getting user input
how many inputs does the Scanner object accept?
8 ответов
+ 1
You must be writing something wrong.
Also note that using Scanner in sololearn app is kinda clumsy
0
1?
0
D3n as many as you want
0
I wrote three n only one result came back
0
D3n show me your code!
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
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
0
The "Get User Input" section of Java won't unlock ; what do I do ?