java basics | Sololearn: Learn to code for FREE!


java basics

hello, when I try to get user input I have issue when I enter integer and string value using nextLine() method .so how I overcome this problem (sorry if iam asking a silly question)

11/5/2021 8:44:02 AM

Lina Akatuki

4 Answers

New Answer


Could you please put your code in a script on playground and link it here So we can check on your code? Without seeing the code it us hard to tell what is going on


nextLine() Reads Strings for Numerical input : nextInt(); , nextDouble(); , nextFloat(); Scanner scanner = new Scanner(; int n = scanner.nextInt(); double d = scanner.nextDouble();


Not silly at all So in order to get a value using nextLine() method Scanner s= new Scanner(; // Create a Scanner called s(you can name it whatever you want though) System.out.println("Enter your name"); //here you are telling the user what input are you asking for String name = s.nextLine(); // Read user input System.out.println("name: " + name); // Output user input there are diffrent methods that you can practice too like nextBoolean() ,nextLong(), read about them so that you can accumulate knowledge


nextLine() reads entire line as input but where as nextInt() reads an integer next in console input and stay in same line. So in combination: nextInt(); nextLine(); input must in same line as ex: 12 string_input Ex: input: 12 String_input //this will not read by nextLine(), because it's already read \n carriage after 12. To overcome this: read similar datatypes in sequence,then add a empty nextLine(); or read all inputs by nextLine() and convert to appropriate compatible needed types....