• Post Reply Bookmark Topic Watch Topic
  • New Topic

"JABA" I'm getting error when I type 2 separate words in result test run after compiling.  RSS feed

 
Haider Mezta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi fellas!

I really need help regarding the simple basic program that I've created yesterday, let's just call it Newbie POS if we really getting real here ^_^.
So basically this program gives you the following results:

1. Total QTY of each items.
2. The total amount of all ITEMS.

So my problem here is every time I run the program and Enter the name of the Item in 2 separate words like "Mineral Water" it gives me error but when I type the item in single word it works as it's expected.
below is the full code which I made and please tell me what I should add or chance to solve the 2 separate words error.

Thank you very much for any kind of help.





 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner reads tokens based on a delimiter.
The default delimiter is whitespace, so any newlines, tabs, or spaces.

Consequently the String "Mineral Water" is actually two tokens, so require two calls to next() to read them in.
This isn't what you want, clearly.
You want it to read up to a line ending.
You can either change the regex used as the delimiter to only cover newlines/carriage returns, or stick with using nextLine() which introduces its own complexities.

Or possibly use a BufferedReader, which means you will then need to parse integers yourself.
Which raises a minor point, but why use nextDouble when you actually want an int? Scanner has a nextInt method, so saving any casting.
 
Haider Mezta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much DAVE it's working now i don't get any error when I type two separate words, by changing next to nextLine();
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you solved the issue with left over delimiters?

Scanner has a sort of quirk.
All the next methods except nextLine leave the last delimiter in the buffer. In the case of your nextDouble calls that would be a newline character(s).

When you then call nextLine afterwards you would get an empty string, as the nextLine will consume that newline character(s).

This can cause issues:

With the above code you might expect the following output:
Enter int
1
Enter String
A string
1A string

Except what you would see is actually:
Enter int
1
Enter String
1
Because the nextLine is consuming that empty line left behind by the nextInt method.

It's a common problem, which is why I said that nextLine() introduces its own complexities.
 
Haider Mezta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As of now the error has been eliminated by the help of the information that you posted recently, But I still need to improve it into something like if I enter a character in the Quantity field then a message will pup up and it will tell me that "Number Is Required in this Field" and it will again switch back to the quantity field as empty field so that I can input an integer so on and so forth. For me I am so very happy and excited as well to learn coding even though I'm kinda late for this trends

So like I said I am gonna build this simple system until I add and add some more feature for it's own capacity. I hope you can share something about the thing which I wanted to add like what I mentioned above.

Sorry for my awful english ^_^...

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!