9
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...

Madhawa Priyashantha
  • 9,208
  • 7
  • 28
  • 58
Gokul Raj
  • 111
  • 1
  • 1
  • 2

9 Answers9

20

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

Thomas Rollet
  • 1,635
  • 3
  • 16
  • 28
Puneet Soni
  • 201
  • 2
  • 3
6
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.

Community
  • 1
  • 1
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • 1
    Well, since you're one of those who removed the dupe note of [this question](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods), you could add a note to your answer to avoid a following question with exactly this problem :). If the uses `nextLine` instead, then his input will be skipped. – Tom Oct 05 '15 at 13:19
  • @Tom Thank you, I have added a comment as to why it is similar but not a dup. – Peter Lawrey Oct 05 '15 at 13:22
  • That's not what I meant :P. It is currently not a dupe, correct, but since you suggest to use `nextLine` instead of `next` to read the whole line, he will run into the same problem asked in that question. His new `nextLine` call will read the rest of the line, that was left behind of the prior `nextDouble()` call. With "the note you could write" I meant that you can lead him to that question to get this problem solved when he runs into it. – Tom Oct 05 '15 at 13:30
  • 1
    @Tom You could be right, it's not clear from the OP's question whether the String is on the same line as I assumed or on a different line. – Peter Lawrey Oct 05 '15 at 13:57
  • 1
    Right, this could be true ... never thought about an input like "1 1.0 test blub test" (on the same line). – Tom Oct 05 '15 at 13:58
  • 1
    @Tom worth adding a note about each possibility. Thank you. – Peter Lawrey Oct 05 '15 at 14:00
6
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);
    }
}
Markus
  • 2,944
  • 6
  • 30
  • 46
Arun
  • 61
  • 1
  • 3
3

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);
}
jprism
  • 2,680
  • 2
  • 32
  • 45
0

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

0

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).

0
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);
     }
}
Sneha
  • 1
  • 1
0
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();

Arun
  • 960
  • 5
  • 16
  • 32
0

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();
}
Mukta
  • 777
  • 8
  • 10