Answers for "why nextline is not working after nextint"

1

java scanner string nextline after nextint

//The problem is with the input.nextInt() method - it only reads the int value. So when you continue reading with input.nextLine() you receive the "\n" Enter key. So to skip this you have to add the input.nextLine(). Hope this should be clear now.

//Try it like that:

System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();
Posted by: Guest on August-24-2020
1

sc.nextline skips

int option = input.nextInt();
input.nextLine();  // Consume newline left-over
String str1 = input.nextLine();
Posted by: Guest on May-07-2020
0

why nextline is not working after nextint

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d = scan.nextDouble();
        scan.nextLine();
        String s = scan.nextLine();
        
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
Posted by: Guest on October-12-2021

Code answers related to "why nextline is not working after nextint"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language