• Post Reply Bookmark Topic Watch Topic
  • New Topic

useDelimiter() and nextDouble(). Simple example, still frustrated  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I successfully used useDelimiter() and nextInt() to delimit two integer tokens, something I was having trouble doing in my earlier post

Now I'm having trouble scanning two double values delimited with ",*"

Here's my source code:



Here's the output from terminal:

Enter two 'double' values like this: a.b,c.d
39.45,33.88
3.00
9.00
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karen,

I have an idea. How about using a regular expression for your delimiter
that matches either a comma or a whitespace character?

- Nick
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in the delimiter, ",*". I'm not sure what it's intended to do, exactly. But the effect of the * is to say "zero or more of the previous thing", which was a comma. So the expression matches zero commas, one comma, two commas, or more. The real problem is that it matches zero commas - you can find zero commas anywhere in the string. Including between the '3' and the '9', or between the '9' and the '.'. Which is why you're getting "3" as the first token, and "9" as the second.

There are many possible solutions here, depending what sort of input you want to handle. Nick's suggestion is good. However, you may find other complications along the way. I suggest you reorganize your code a bit to print the first value before you try to parse the second value. That way, you can get more feedback for partial success along the way, which will help in understanding what's going on.
 
Karen Haq
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to understand how to implement various methods of the Scanner Class and Pattern Class, using simple examples. In my post, my intention was to read two separate double values from the user - in one sitting - versus prompting the user twice to enter in double values.

Thanks, gents for your replies. I'll work/play with the delimiter method a little more until I finally see the light at the end of the program (i.e. tunnel).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!