13

The code I have written takes as input just a single string and not a whole sentence and I want a whole sentence to be taken as input:

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i; 
        i= scan.nextInt();
        double d;
        d=scan.nextDouble();
        String s;
        s=scan.next();
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

The test case is "Welcome to Java" and it's just showing "Welcome" in the output. Everything else is working fine. Please help.

Marius Jaraminas
  • 643
  • 4
  • 18
Sarthak Rana
  • 165
  • 1
  • 1
  • 9

7 Answers7

10

you can use scan.nextLine(); to read the entire line.

Andy Turner
  • 122,430
  • 10
  • 138
  • 216
guru_001
  • 465
  • 5
  • 11
  • Its showing this now :-Nice try, but you did not pass this test case. Input (stdin) 42 3.1415 Welcome to HackerRank's Java tutorials! Your Output (stdout) String: Double: 3.1415 Int: 42 Expected Output String: Welcome to HackerRank's Java tutorials! Double: 3.1415 Int: 42 – Sarthak Rana Jun 25 '16 at 11:41
  • 1
    int i; i= scan.nextInt(); double d; d=scan.nextDouble(); – guru_001 Jun 25 '16 at 11:43
  • its showing nothing now....the word 'Welcome' was being displayed earlier but after making the changes it shows nothing--just a blank space – Sarthak Rana Jun 25 '16 at 11:48
  • 1
    d=scan.nextDouble(); scan.next(); s=scan.nextLine(); this will work – guru_001 Jun 25 '16 at 11:50
  • when reading double,int types third time when reading string the scan object skips so,scan.next() represents skip so,the next scan.nextLine() works. – guru_001 Jun 25 '16 at 11:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115585/discussion-between-sarthak-rana-and-guru-001). – Sarthak Rana Jun 25 '16 at 12:14
6

you can try the following, it will work.

public static void main(String args[]) {    
        // Create a new scanner object
        Scanner scan = new Scanner(System.in); 

        // Scan the integer which is in the first line of the input
        int i = scan.nextInt(); 

        // Scan the double which is on the second line
        double d = scan.nextDouble(); 

        /* 
         * At this point, the scanner is still on the second line at the end
         * of the double, so we need to move the scanner to the next line
         * scans to the end of the previous line which contains the double. 
         */
        scan.nextLine();    

        // reads the complete next line which contains the string sentence            
        String s = scan.nextLine();    

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
  }
Yug Singh
  • 2,571
  • 3
  • 17
  • 42
Arafat
  • 156
  • 2
  • 6
  • [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) Please add Few line comment, it would increase readability of of answer and it's good practice your answer is well formatted and have meaning its better to read this help gape for better answer writing practice – v8-E Jan 10 '19 at 05:21
2

You should put a scan.nextLine() after the above integer scan and the double scan. Then use String s = scan.nextLine(). Like this,

int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
kevinSpaceyIsKeyserSöze
  • 2,122
  • 1
  • 15
  • 19
1

Try below code, this is working code, I tried with IntelliJ and Online compiler Like Hacker Rank.

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);
    }
}
brandt.codes
  • 853
  • 2
  • 8
  • 15
1

In your code you are using next() . This is use for input one word.

You can use nextLine() to input sentence in java.

example :

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s;
        s=scan.nextLine();
        System.out.println("String: " + s);
    }
}

But above code(question) you are taking three inputs. You use nextLine() after nextDouble(). In this case nextLine() read input as a newline character of the double input. So output String is empty. To avoid that use another nextLine() after the nextDouble(). This nextLine() read the newline character of the double input and next nextLine() read the sentence.

So try this out :

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

Thank You...!

0
import java.util.*; 
public class Solution {

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

       //For input a Integer
       int i = scan.nextInt(); 

       //For input a Double
       double d = scan.nextDouble(); 

       //For input a String
       scan.nextLine(); //For using to get the input string that was skipped of the Scanner object.(Use it when scan a string after scanning different type of  variables.)
       String s = scan.nextLine();

       //For print String, Double and Integer variable
    
       System.out.println("String: " + s);
       System.out.println("Double: " + d);
       System.out.println("Int: " + i);
   }
}
0
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();

        // Write your code here.

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}
a.ak
  • 386
  • 4
  • 15