1

In this snippet what I am doing taking three different types of variables and adding them and then printing then down.

 public static void main(String[] args) {
    int i = 4;
    double d = 4.0;
    String s = "HackerRank ";
    Scanner scan = new Scanner(System.in);      
    int firstVariable = 0;
    double secondVariable = 0.0;       
    String theString = "";

    firstVariable = scan.nextInt();
    secondVariable = scan.nextDouble();
    theString = scan.nextLine();

    System.out.println(firstVariable+i);
    System.out.println(secondVariable+d);
    System.out.println(s+""+theString);

}

I am providing input for firstVariable hitting enter and then providing the input for secondVariable and now as soon as I hit enter theString is capturing that value(I know it should capture it).
EDIT: in this case how should I provide the input to theString without as well as with space ?

I did try something like this,

    while(scan.hasNext())
        theString = scan.nextLine();

But it didn't work either.

Govinda Sakhare
  • 3,317
  • 4
  • 22
  • 51
  • @Tom No it ain't duplicate, please do check the edit part. These duplicate linked questions don't have an answer of that EDIT part. I mean handling both the cases i.e providing input after space as well as new line. – Govinda Sakhare Jun 01 '16 at 04:17

3 Answers3

3

Simply check scan.nextLine() is empty if so call scan.nextLine() again as next:

secondVariable = scan.nextDouble();
theString = scan.nextLine().trim();
if (theString.isEmpty()) {
    theString = scan.nextLine();
}

Another approach with a pattern:

firstVariable = scan.nextInt();
secondVariable = scan.nextDouble();
theString = scan.next("\\w+");
Nicolas Filotto
  • 39,066
  • 11
  • 82
  • 105
  • Please check the edit I have made in my question. In this if the user provides `theString` by giving space after `secondVariable` variable's input then it the solution you've provided won't work. – Govinda Sakhare May 31 '16 at 19:49
  • I have updated my answer to support also a double followed by the string – Nicolas Filotto May 31 '16 at 19:57
  • @Tunaki Nope question is way different than the usual ones where people normally want to know the difference between different types of scanner methods – Govinda Sakhare Jun 01 '16 at 04:13
  • @Tunaki I understand your point of view but I personally never met this exact same question and if you check well, you will see that I answered more than an hour before the comments have been added indicating a duplicate. But anyway you are not supposed to down vote for a correct answer unless I obviously copy/paste the answer of someone else which is clearly not the case here, I'm a big boy I can provide my own answer. Autrement dit, si je dois passer mon temps à vérifier si la question a déjà été posée ou pas, je ne réponds plus :-) – Nicolas Filotto Jun 01 '16 at 10:18
  • @Tunaki That's not fair, if you think the question is duplicate you have every right to downvote my question I don't mind that. But downvoting the correct answer is lame, the guy is helping someone(newbie like me). – Govinda Sakhare Jun 01 '16 at 14:10
1

There are two ways to solve your problem:

The first one is to use

nextLine()

each time you read from the user like this:

int firstVariable = scan.nextInt();
scan.nextLine();
double secondVariable = scan.nextDouble();
scan.nextLine();
String theString = scan.nextLine();

The second one is to parse the integer and double value from nextLine() like this:

int firstVariable = Integer.parseInt(scan.nextLine());
Double secondVariable = Double.parseDouble(scan.nextLine());
String theString = scan.nextLine();
theVoid
  • 743
  • 4
  • 14
0

You need to add an extra scan.nextLine(); before your String theString, to capture the Enter after the double. Like this:

firstVariable = scan.nextInt();
secondVariable = scan.nextDouble();
scan.nextLine();
theString = scan.nextLine();

Just as an advice to reduce your code, there's no need to add these:

int firstVariable = 0;
double secondVariable = 0.0;       
String theString = "";

Just add the type to the variables to capture the scan:

int firstVariable = scan.nextInt();
double secondVariable = scan.nextDouble();
scan.nextLine();
String theString = scan.nextLine();
Nooblhu
  • 525
  • 12
  • 32