0

The code is not scanning the second string form user. It just prints 'Hello' second string is not printed.

package online_questions;
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner scan = new Scanner(System.in);
    int i = 4;
    double d = 4.0;
    String s = "Hello ";
    int a = scan.nextInt();
    double b = scan.nextDouble();
    String c = scan.nextLine();
    System.out.println(i+a);
    System.out.println(d+b);
    System.out.println(s + c);
}

}

3 Answers3

0

After reading double b there will be a newline character in the buffer. nexLine() will read upto new line character '\0 so that's why your getting empty string simply addscan.nextLine();after reading your double

gajapathy p
  • 113
  • 12
0

You need to call nextLine after nextDouble.

double b = scan.nextDouble();
scan.nextLine();
String c = scan.nextLine();
alayor
  • 3,336
  • 4
  • 20
  • 39
0

after

double b = scan.nextDouble();

add

scan.nextLine();

The methods nextInt nextDouble etc doens't move to the next line automatically. So you need to call them explicitely.

stinepike
  • 50,967
  • 14
  • 89
  • 108