Gokul Raj
Gokul Raj

Reputation: 151

how to read int,double,and sentence of string using same scanner variable

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String s=new String();
            int x=sc.nextInt();
            double y=sc.nextDouble();
            s = sc.next();

            System.out.println("String:"+s);
            System.out.println("Double: "+y); 
            System.out.println("Int: "+x);     
     }       
}

it scans only one word pleasd give any suggestions...

Upvotes: 13

Views: 76560

Answers (9)

Mukta
Mukta

Reputation: 1557

Here is your answer

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();  

    //if you want to skip the unnecessary input
    //scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    System.out.println("String: " + s);       
    System.out.println("Double: " + d);      
    System.out.println("Int: " + i);

   scan.close();
}

Upvotes: 0

jprism
jprism

Reputation: 3474

Why don't you skip as follows

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int i = scan.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    double d = scan.nextDouble();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    scan.nextLine();
    String s = scan.nextLine();

    System.out.println("String: " + s);
    System.out.println("Double: " + d);
    System.out.println("Int: " + i);
}

Upvotes: 4

Puneet Soni
Puneet Soni

Reputation: 261

You can try this code:

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);
}

Because the Scanner object will read the rest of the line where its previous read left off.

If there is nothing on the line, it simply consumes the newline and moves to the starting of the next line.

After double declaration, you have to write: scan.nextLine();

Upvotes: 26

Arun
Arun

Reputation: 1010

import java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     scan.nextLine();
     String s = scan.nextLine();
     scan.close();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}

When switching between reading tokens of input and reading a full line of input, you need to make another call to nextLine() because the Scanner object will read the rest of the line where its previous read left off.

If there is nothing on the line, it simply consumes the newline and moves to the beginning of the next line.

After double declaration, you have to write: scan.nextLine();

Upvotes: 0

Sneha
Sneha

Reputation: 1

import java.util.Scanner;

public class Solution 
{
    public static void main(String[] args) 
    {
     Scanner scan = new Scanner(System.in);
     int i = Integer.parseInt(scan.nextLine());
     double d = Double.parseDouble(scan.nextLine());
     String s = scan.nextLine();

     System.out.println("String: " + s);
     System.out.println("Double: " + d);
     System.out.println("Int: "+ i);
     }
}

Upvotes: 0

shrihari wankhade
shrihari wankhade

Reputation: 1

If you use the nextLine() method immediately following the nextInt() method, recall that 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).

Upvotes: 0

Bittu Sharma
Bittu Sharma

Reputation: 1

I'd rather suggest to try out this block of code

Scanner scan = new Scanner();
int i = Integer.parseInt(scan.nextLine());
double d = Double.parseDouble(scan.nextLine());
String s = scan.nextLine();

I hope this would not skip reading the String input (or just read a single word skipping rest of the Line) after a double or integer input..

follow me on fb/ayur.sharma

Upvotes: 0

Arun
Arun

Reputation: 81

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        double y = sc.nextDouble();
        sc.nextLine();
        String s = sc.nextLine();

        System.out.println("String: " + s);
        System.out.println("Double: " + y);
        System.out.println("Int: " + x);
    }
}

Upvotes: 8

Peter Lawrey
Peter Lawrey

Reputation: 533530

s = sc.next();

it scans only one word pleasd give any suggestions...

This is what next(); is intended to do. If you want to read multiple words, the simplest solution is to read a line of text. e.g.

String s = sc.nextLine();

If you need to read multi-line text, you need to workout a strategy for doing this such as reading everything in a loop until you have a blank line.

Note: while the answer is similar to Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods the difference is that you don't discard the rest of the line, but rather you use the String returned by nextLine();

If you are expecting the input to be

 1 2.0 Hello World

then you want to use the suggestion above, however if the input is on multiple lines like this

 1 2.0
 Hello World

You will need to discard the rest of the line with an extra class to nextLine() as the link above suggests.

Upvotes: 6

Related Questions