• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to a check to see if an entry is a either a primitive or a string  RSS feed

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to check to see if a what a user has entered is a string or a other variable type, if I'm asking this the right way?
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:... what a user has entered ...

How, exactly, is the user to enter data?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you are reading from a non‑text file or from a network, all input is in the form of Strings. Some objects, e.g. a Scanner object have methods to test the type of the next input, otherwise assume everything is a String.\u2019 = '
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant that when someone is prompted to enter data from the keyboard; is there a way for java to tell if it's a number of a string?

Let's say that someone has an array list of items, and java asks the if they'd like to get rid of one of the items, by means of entering the number of the item (corresponding to it's index number in the array). So far as i know the only way is to parse the entered string into a number, and then remove that from the list.

But I want to know if java can recognize whether the entered data is a number or a string without having to parse anything.

Did I ask it better?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose you have the input in a String, then you could use regular expressions to check if it has a certain format, such as only digits. For example:

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:
But I want to know if java can recognize whether the entered data is a number or a string without having to parse anything.

It's all about context and you, the programmer, have to provide it. Java can't "recognize" anything, at least not in the way it seems you're thinking about it. It can treat the input as an integer but you have to tell the program to do that by calling Scanner.nextInt. By doing that, you have told the program, "Expect whatever the user enters next to be an integer value." When you instruct it that way, the program will "recognize" the next valid entry as an integer and return an int value that you can use.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote: When you instruct it that way, the program will "recognize" the next valid entry as an integer and return an int value that you can use.

and you have to be sure you handle it if the user enters something that is NOT an integer.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote: . . . Did I ask it better?
No.

You do not want to know whether you have got correct input so much as knowing whether you have incorrect input. If you say, “I want an int between 0 and 99,” then the trick Rob Spoor taught me a long time ago finds the errors. I recommend you put a Scanner object into a utility class
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!