• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having the user input numbers separated by commas  RSS feed

 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm new to these forums and new to Java as well. Here is my dilemma. I have my program written here below and at the moment the user inputs 3 numbers and the program finds the smallest and largest. My problem is that the user needs to be able to input these three numbers seperated by commas like "2, 3, 4". I'm not sure how I could get them to do this and have the program read only the numbers. Can anyone please help? Thanks

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: StringTokenizer
 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have googled StringTokenizer and other methods but am unsure as how to implement them into my code. All the examples I find are with using Strings, and since I use Scanner I'm stumped.
 
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

Since you are already using the Scanner class, you can configure the Scanner to allow commas as a delimiter. Unfortunately, that is achieved with regular expressions -- and if you don't know what it is, then this may a big topic to understand first.

Henry
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know you didn't ask this question but if you declare another variable called highest and set the first value = highest you only have 2 if statements and if either number is higher set highest equal to that value if you understand what I am saying? Plus you print out the highest number at the end so you only have 1 print statement. ie



Just mess around with the scanner and do some reading and you'll get the answers
 
Hank Harrison
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for all the input. After googling scanner delimiter I was able to put this delimiter into the code, but if someone could explain to me what exactly this part ("[\\s,\r\n]+") means that would be great. I thought that might have something to do with reading the comma but what does the rest of it mean like the \\s \r \n + and such? Thanks




 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought they should be in the Java API Documentation but if they are - I can't find them???

//s is the switch for a space
/n is a new line (line feed)
/t is a horizontal tab
/r is a carriage return

there are loads more but hopefully this will help you out and I will find out where they are located as I need them myself!
 
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
Hank Harrison wrote:Ok thanks for all the input. After googling scanner delimiter I was able to put this delimiter into the code, but if someone could explain to me what exactly this part ("[\\s,\r\n]+") means that would be great. I thought that might have something to do with reading the comma but what does the rest of it mean like the \\s \r \n + and such? Thanks



The parameter being passed to the useDelimiter() method is a regular expression -- and as hinted to earlier, it is not something that can explained in a single post... but I guess I can explain the particulars of your regex.

The square brackets (ie. [ and ] ) represents a character set. Everything in it are valid characters to do a single character match.

The + symbol represents one or more of the previous character. Combined with the previous character set, it means one or more matches of the character set.

The \s is the character in regex to represent a whitespace.

And interestingly, regex also supports \r and \n, to represents carriage return and newline characters -- but you are not use the regex representation. Instead, you are passing the raw carriage return and newline characters directly into the regex engine, which works but is not recommended.

Henry
 
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
Brian Kellytt wrote:I thought they should be in the Java API Documentation but if they are - I can't find them???

//s is the switch for a space
/n is a new line (line feed)
/t is a horizontal tab
/r is a carriage return

there are loads more but hopefully this will help you out and I will find out where they are located as I need them myself!


Regular expressions patterns are summarized in the JavaDoc for the java.util.regex.Pattern class, here...

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

However, it is only a summary, it may be a good idea to pick up a good book on it.

Henry
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Kellytt wrote: . . .

//s is the switch for a space
/n is a new line (line feed)
/t is a horizontal tab
/r is a carriage return

. . .
I am afraid you are mistaken about all those. You are confusing ordinary escapes with regular expression predefined categories, too.

You will find the escape characters in the Java Language specification here and here. Despite what it says in many books, only ever use \r or \n (never \u000a or \u000d which confuse the compiler) if you have been told to supply an LF or CR character. If you have not been given specific instructions about LF or CR always use the %n tag with a format method, or System.out/err.printf("...%n..."...);
Don't use \123 (octal). Use \u0067 (hex equivalent) instead.

The regular expression for whitespace (not simply space) is \s, but you usually write that in a String literal, so you have to escape the backslash: "\\s". There are many place where you can read about regular expressions; it is a study in its own right. There is a good section in the Java Tutorials.
 
Brian Kellytt
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hank Harrison wrote:

I would say this is not really an ideal solution. What would you do if you had the users inputting 10 number? or you were reading 1000 from a file? or 1,000,000 from a database?

I would suggest you go back and think through the problem. Think about how you would solve it if all you had was your brain, some paper, some pencils, and an eraser. only THEN should you start coding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!