Answers for "java scanner next string"

1

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);
    }
}
Posted by: Guest on October-19-2020
0

java scanner next()

String s = "Hello World! 3 + 3.0 = 6 ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // find the next token and print it
      System.out.println("" + scanner.next()); // "Hello"

      // find the next token and print it
      System.out.println("" + scanner.next()); // "World!"

      // close the scanner
      scanner.close();
Posted by: Guest on March-05-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language