• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program to check the data type of user input data  RSS feed

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a code to find out the data type input by the user. My code can only check integer, float and string values. It is unable to check ` long`,` boolean` and `double` values.
Here is my code:



I did not use switch statement here because there was no specific value to be given to the various cases of the switch statement.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the order of the if statements will be critical.  The number 123456789123456789 could be a float or a long or a double.  How do you want to interpret it?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I think the order of the if statements will be critical.  The number 123456789123456789 could be a float or a long or a double.  How do you want to interpret it?

And, hasNextLine() should be last. they can all be a 'line'.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right. The order of `if` statements matter here. However, the only data type this program fails to check is a Boolean value.
Eg: 3<9
It displays the following output:
"This input is of type string."
even if the `if block` for Boolean is placed first in the order.

It displays nothing to the console when the `if block` for checking string value is removed.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khusbu Sinha wrote:

It displays nothing to the console when the `if block` for checking string value is removed.


I mean it displays nothing for a `boolean value` when the if block for checking string is removed.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hasNextBoolean is looking for "true" or "false". It does not evaluate expressions.
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. I didn't know that.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:hasNextBoolean is looking for "true" or "false". It does not evaluate expressions.


Khusbu Sinha wrote:OK. I didn't know that.


This is actually true for all of the data types, for the Scanner class... eg. "123 + 456" doesn't return the sum, as an int type.

Henry
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:. . . hasNextLine() should be last. they can all be a 'line'.
Actually, hasNext() and hasNextLine() always return true for System.in until System.in is closed (which we never do, do we?). So that isn't helpful. Even if the user hasn't written anything yet, they still could, so hasNext() has to return true because there might be something following.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!