3
import java.util.*;

class DataTypes {

    public static void main(String args[]) {
        int i = 4;
        double d= 4.0;
        String s = "Hackerrank";
        Scanner scan = new Scanner(System.in);
        int j = scan.nextInt();
        double d1 = scan.nextDouble();
        String s1 = new String();
        s1 = scan.nextLine();
        j = j+i;
        System.out.println(j);
        d1 = d1+d;
        System.out.println(d1);
        System.out.println(s+" "+s1);
        scan.close();
    }
}

See the above code it is error free but after I input the double the program does not read the string it directly shows the output. But When I comment the scanning statements of int and double then the programs reads my string. Why is it so?

Dorukhan Arslan
  • 2,366
  • 1
  • 16
  • 36
Andy.inamdar
  • 125
  • 1
  • 1
  • 9
  • 1
    Why do you use a `Scanner` if your value is already set? – F0XS Dec 08 '17 at 08:31
  • @foxdie value is already set to s not to s1. I want to print concatenated string of s and s1 by reading s1 from user. – Andy.inamdar Dec 08 '17 at 08:50
  • He's talking about "new String()". – Dorukhan Arslan Dec 08 '17 at 08:51
  • @Andy.inamdar The `Scanner` allows you to input values ​​in the console ...it serves you to verify that your result is correct or that a variable has good values. This is not a GUI that you can format... This is more an information. – F0XS Dec 08 '17 at 08:59

7 Answers7

6

You are not reading the string as you want, just use below code to make it work as you want

s1 = scan.next();

For more information please see this link

EDIT :
If your input consist a string with spaces than you can do something like:

scan.nextLine();
s1 = scan.nextLine();

This is because of nextDouble(), this method does not consume the last newline character of your input, and thus that newline is consumed in the next call to nextLine(). Adding newLine() before reading String will consume the new Line and you will be able to input your String.

Lalit Verma
  • 759
  • 8
  • 22
0

Change this line

s1 = scan.nextLine();

to

s1 = scan.next();
Karan
  • 448
  • 4
  • 9
0

try this:

import java.util.*;
class Demo{
public static void main(String args[]){
  int i = 4;
  double d= 4.0;
  String s = "Hackerrank";
  Scanner scan = new Scanner(System.in);
  int j = scan.nextInt();
  double d1 = scan.nextDouble();
  String s1  = scan.next();
  j = j+i;
  System.out.println(j);
  d1 = d1+d;
  System.out.println(d1);
  System.out.println(s+" "+s1);
  scan.close();
 }
}
Amol Raje
  • 772
  • 2
  • 7
  • 16
  • Please format your code as it is hardly readable. You could also put some description instead of ready to use solution – Gaskoin Dec 08 '17 at 09:00
0

Changing the line

 s1 = scan.nextLine();

to below line

 s1 = scan.next();

will work.

Lalit Verma
  • 759
  • 8
  • 22
Abhishek
  • 177
  • 1
  • 2
  • 10
0

this is because of you enters: , so till get new line it can not go to next line so for non string use example:

int j = scan.nextInt(); scan.nextLine();

import java.util.*;

class DataTypes {

    public static void main(String args[]) {

        int i = 4;
        double d = 4.0;
        String s = "Hackerrank";
        Scanner scan = new Scanner(System.in);

//      System.out.print("Enter j: ");
        int j = scan.nextInt();
        scan.nextLine();
//      System.out.print("Enter dl: ");
        double d1 = scan.nextDouble();
        scan.nextLine();

        String s1 = new String();
//      System.out.print("Enter s1: ");
        s1 = scan.nextLine();
        j = j + i;
        System.out.println(j);
        d1 = d1 + d;
        System.out.println(d1);
        System.out.println(s + " " + s1);
        scan.close();
    }
}
0

Replace this lines as

double d1 = Double.parseDouble(scan.nextLine());
Karan
  • 448
  • 4
  • 9
0

This problem is appearing because a call to nextDouble only consumes the number itself, and not only newlines after it.

This happens:

  1. Your program asks the scanner for a new double
  2. Your give the console 4 characters: 0.1\n
  3. Scanner reads from the console, and gets 0.1\n
  4. Since it now can calculate where the double ends, it pushes back \n to its buffer
  5. Your program asks the scanner for a new newline
  6. The scanner reads from its buffers: \n
  7. Since it found a valid line ending, it returns the string in front of it without the trailing newline.

There are different solutions for this:

Prepend the string directly after the number in the input:

1 3.5I'm a short string

Consume the unneeded line character in your code manually:

        double d1 = scan.nextDouble();
        scan.nextLine();
        String s1 = scan.nextLine();

Call next instead of nextLine:

Notice, this will only consume 1 word at a time

        double d1 = scan.nextDouble();
        String s1 = scan.next();

Read the double using a call to nextline:

This will limit your program to newline separated numbers, instead of allowing any seperator:

int j = Integer.parseInteger(scan.nextLine());
double d1 = Double.parseDouble(scan.nextLine());
String s1 =scan.nextLine();
Ferrybig
  • 15,951
  • 6
  • 51
  • 69