0

This is how my code looks so far.

public static void main(String[] args){

Scanner keyboard = new Scanner(System.in);

String name, age, job;

System.out.println("What is your name?");

name = keyboard.next();

System.out.println("How old are you?");

age = keyboard.next();

System.out.println("What do you do for a living?");

job = keyboard.next();

System.out.println("Your name is " + name + ". You are " + age + " year old. And you live as a " + job + ".");

}

If possible I'd like to learn it in details because it's something I really want to learn.

Every time the program gets the "job" input, only the first word before the space is stored into the "job" variable.

Thanks in advance. This is my first thread here. Hopefully, I'll be a part of this awesome community. :)

  • Possible duplicate of [Scanner doesn't see after space](http://stackoverflow.com/questions/19509647/scanner-doesnt-see-after-space) – Tom Feb 23 '17 at 12:46

4 Answers4

0

You may separate a given String by a character like so. Other than that, you may use a GUI (but if it's console, the best I could think of is the Split).

Edit:

Of course, you input like "John Do" and separate by the white space into two other strings.

Community
  • 1
  • 1
agiro
  • 2,018
  • 2
  • 30
  • 62
0

You are using Scanner.next() to retrieve data from the user. Without any additional configuration, this method will retrieve the next token, where tokens are sequences of non-space characters separated by spaces.
This explains why you only retrieve the first word of the job (that would also happen with the other informations).

If you want to retrieve more than a word, you could instead switch to Scanner.nextLine().
Note that you then won't be able to enter the three pieces of data and press enter, behavior that was possible with your current code.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • Hello, Aaron. I did as you said but my program would terminate after getting the age. So what I did, I created another scan just for getting that specific String. This seems like a weird alternative. But it worked. Do you know why it would not get an input and terminate? –  Feb 23 '17 at 12:57
  • @D.Tex That seems strange, you shouldn't need an additional Scanner. Can you reproduce the behaviour in an online environment like [ideone.com](https://ideone.com/) ? See [this snippet](https://ideone.com/YxiadJ) for an example of what I meant with my answer. – Aaron Feb 23 '17 at 13:01
  • [Getting an error](https://ideone.com/n23T5E) Mine is getting an error for some reason I don't know why. –  Feb 23 '17 at 13:12
  • @D.Tex there's no interactive input in ideone, you need to provide it before execution, as I did [here](https://ideone.com/0LCg4I). Of course this is specific to ideone, and I think your code should work in a standard environment. – Aaron Feb 23 '17 at 13:15
  • @D.Tex there's a problem with your code : when you read the next token which gives the age, you only consume the token rather than the whole line. The `nextLine` you use just after will return the (empty) rest of the 2nd line. In your specific case I suggest you use `nextInt` to read the age then use a `nextLine` whose result you can discard, then the next `nextLine` will return the job – Aaron Feb 23 '17 at 13:18
  • Aaron, I did as you said. I created an intenger for age and used nextInt but the "bug" still persists after the program gets the age input from the user it executes the rest of the code without getting any input. –  Feb 23 '17 at 13:37
  • @D.Tex have you added an extra `nextLine` after the `nextInt` ? The problem is that the `next` methods (except `nextLine`) won't consume the linefeed that follows the token they matched. If you do use another `next` method after them you won't notice anything since it will disregard the linefeed as a whitespace character (part of the delimiter) and continue parsing until it has read a token, but if you use `nextLine` instead it will only parse up to the linefeed. I've updated my original snippet to use `nextInt`, [check it out](https://ideone.com/YxiadJ) – Aaron Feb 23 '17 at 13:41
  • it worked I had forgotten to add an extraLine. Now my program is fully functional without the need of an extra scanner. Thanks a lot of your help. :) –  Feb 23 '17 at 13:52
  • You're welcome ! It would be appreciated if you upvoted helpful answers and marked one as accepted :) – Aaron Feb 23 '17 at 14:05
  • It says I don't have enough reputation to do so. Sorry. –  Feb 23 '17 at 14:40
  • Oh, there's a cap on that now? Well nevermind then. I upvoted your question to help solve that problem ;) – Aaron Feb 23 '17 at 14:41
  • Alright!! Thanks again, I feel like I'm making progress towards knowing Java. –  Feb 23 '17 at 16:04
0

You should use Scanner.nextLine() to read the entire line input by the user, instead of Scanner.next()

Troncoso
  • 2,343
  • 3
  • 33
  • 52
0

The reasons job variable holds just one word is because you use keyboard.next() Use job=keyboard.nextLine();

So job variable will store the whole line of string you have entered.

I recommend you use nextLine() instead of next() for all your String type keyboard inputs so you won't face this problem anymore

Oshan
  • 1