English Nerd
4
Q:

how to provide a long string in Java Scanner class

If you use the nextLine() method immediately following the nextInt() method, 
nextInt() reads integer tokens; because of this, the last newline character for 
that line of integer input is still queued in the input buffer and the next 
nextLine() will be reading the remainder of the integer line (which is empty). 
So we read can read the empty space to another string might work. Check below 
code.
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();

        // Write your code here.
        double d = scan.nextDouble();
        String f = scan.nextLine();
        String s = scan.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
1
import java.util.Scanner;//import Scanner

Scanner input=new Scanner(System.in);//Create the scanner Obj

float numF = input.nextFloat();//Returns float
int num1 = input.nextInt(); //Returns int
byte byte1 = input.nextByte();//Returns Byte
long lg1 = input.nextLong();//Returns long
boolean b1 = input.nextBoolean();//Returns bool
double num2 = input.nextDouble();//Returns Double
String nome = input.nextLine();//Returns String

//execution is pause until you give input

7
import java.util.Scanner;  // Import the Scanner class

class MyClass {
  public static void main(String[] args) {
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter username");

    String userName = myObj.nextLine();  // Read user input
    System.out.println("Username is: " + userName);  // Output user input
  }
}
1
import java.util.Scanner;
4

New to Communities?

Join the community