0
public class Solution {

    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";

        Scanner scan = new Scanner(System.in);
        /* Declare second integer, double, and String variables. */
        /* Read and save an integer, double, and String to your variables.*/
        int sec = scan.nextInt(); 
        double db = scan.nextDouble(); 
        String newWord = scan.nextLine(); 

        /* Print the sum of both integer variables on a new line. */
        System.out.println(i + sec);
        /* Print the sum of the double variables on a new line. */
        System.out.println(d + db);
        /* Concatenate and print the String variables on a new line; 
            the 's' variable above should be printed first. */
        System.out.println(s + newWord);
        scan.close();
    }
}

If String newWord = scan.nextLine(); is placed before int sec = scan.nextInt(); it works fine though.

Caleb Kleveter
  • 10,260
  • 8
  • 56
  • 79

2 Answers2

2

Just replace String newWord = scan.nextLine(); by String newWord = scan.next();

Here :

    double db = scan.nextDouble(); 
    String newWord = scan.nextLine(); 

scan.nextDouble() doesn't read all the line, only the double.
So scan.nextLine() assign to the newWord variable the rest of the current line, that is an empty String : "".

If String newWord = scan.nextLine(); is placed before int sec = scan.nextInt(); it works fine though.

Indeed because "" is a valid value for a String. Therefore newWord has the "" value when you does that.


Edit for comment

Try to avoid mixing nextInt(), nextDouble(), etc... with the nextLine() method.
You could perform a nextLine() to read the empty String and perform again another nextLine() to read the input of the user.

Or you could only use nextLine() even for numeric type and create numeric values from the String input. Here is an example with your code :

public static void main(String[] args) {
    int i = 4;
    double d = 4.0;
    String s = "HackerRank ";

    Scanner scan = new Scanner(System.in);
    /* Declare second integer, double, and String variables. */
    /* Read and save an integer, double, and String to your variables.*/
    int sec = Integer.valueOf(scan.nextLine()); 
    double db = Double.valueOf(scan.nextLine()); 
    String newWord = scan.nextLine(); 

    /* Print the sum of both integer variables on a new line. */
    System.out.println(i + sec);
    /* Print the sum of the double variables on a new line. */
    System.out.println(d + db);
    /* Concatenate and print the String variables on a new line; 
        the 's' variable above should be printed first. */
    System.out.println(s + newWord);
    scan.close();
}
davidxxx
  • 104,693
  • 13
  • 159
  • 179
  • Thank you. haha. But what if there are multiple strings that I want to be scanned and assigned to – Michael G. Dec 21 '16 at 22:15
  • Thank you. haha. But what if there are multiple strings that I want to be scanned and assigned to "String newWord".Say i input "is the best". Scan.next(); only scans the first word "is". how do i get it to scan the rest "the best". – Michael G. Dec 21 '16 at 22:17
  • Next line should work – GlacialMan Dec 21 '16 at 22:22
  • You use String class, you should use string like a type of variable – GlacialMan Dec 21 '16 at 22:24
  • Try to avoid mixing `nextInt()`, `nextDouble()`, etc... with `nextLine()`. I updated my answer to give you some alternatives if you want to read String containing whitespaces in a single input (whitespace which is the default delimiter in Scanner) – davidxxx Dec 21 '16 at 22:34
0

To understand this error you would need to know how the Scanner works so here is a brief description. The nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), next- Double(), and next() methods are known as token-reading methods, because they read tokens separated by delimiters. By default, the delimiters are whitespace characters. A token-reading method first skips any delimiters (whitespace characters by default), then reads a token ending at a delimiter. The token is then automatically converted into a value of the byte, short, int, long, float, or double type. For the next() method, no conversion is performed. The token-reading method does not read the delimiter after the token. If the nextLine() method is invoked after a token-reading method, this method reads characters that start from this delimiter and end with the line separator. The line separator is read, but it is not part of the string returned by nextLine().

  • Thank you for responding. :) I get that .nextLine() will only read after the last scanned input to the end of the line. My problem was trying to code a way to not use another .nextLine() just to read through the rest of that line. to scan what i really wanted with another scan.nextLine() – Michael G. Dec 22 '16 at 00:42