• Post Reply Bookmark Topic Watch Topic
  • New Topic

My scanner does not scan what I would like  RSS feed

 
Marnix De Cauwsemaecker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hey, I've just very recently started learning how to code! I'm having a lot of fun! So, I decided I would create my own little console program put myself to the test. I've used only things that I have learned so far, but this is my problem.

This is what I want to happen in the console.

Output: "What is your first name?" :
Input : "Johan"
Output: "What is your last name?":
Input : "The Test" :
Ouput: Fullname: Johan The Test

But in the end I only get "JohanThe". How do I get it to copy the space between Johan and the, and also add test?

help would be very appreciated!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

The next() method gets the next "word". As you noted, a word ends when there is a space. There's another method nextLine() that gets the whole line. (And so it doesn't seem like pulled this knowledge out of thin air, it is documented here.)

Also, a note so you develop good habits: in Java, we use lowercase letters for variable names. So Scanner1 should be scanner1. It's good to get in this habit now before you get uses to a "non-standard" way.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marnix De Cauwsemaecker wrote:
This is what I want to happen in the console.

Output: "What is your first name?" :
Input : "Johan"
Output: "What is your last name?":
Input : "The Test" :
Ouput: Fullname: Johan The Test

But in the end I only get "JohanThe". How do I get it to copy the space between Johan and the, and also add test?

help would be very appreciated!


The Scanner class next() method returns a token. And tokens are words that are separated by whitespace. And of course, the space in "the test" is a whitespace.

Perhaps you want the whole line? In which case, you can get it via the nextLine() method.

Henry
 
Marnix De Cauwsemaecker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, and I will use lowercases next time!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!