Archived

This topic is now archived and is closed to further replies.

java question

This topic is 5574 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s a simple one for ya: How do you test to see if a user''s input isn''t an integer? int whatever; whatever.readInt(); if(whatever...) How do I test to see if what was provided by the user isn''t an integer at all?

Share this post


Link to post
Share on other sites
dont think you can .

i am not sure, but a sure way to test is to read as a string, and then compare the characters to see if its numbers.

but if you read as an int, i am not sure, since even characters are numbers.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sicne this is a java question why don''t you read the line in as a string and then use the parseInt() function, if this throws an exception then it isn''t an integer. Hope this helps.

Share this post


Link to post
Share on other sites
There are probly a number of ways, but the easiest would be to use a try/catch method and use NumberFormatException, look these up in the API and it should help.

e.g.

try {

int whatever;
whatever.readInt();

}

catch (NumberFormatException nfe) {

System.out.println(nfe.toString());

}

//Or something like that.


hobo-jo

Share this post


Link to post
Share on other sites
Hmmm.. I''ve never done the whole try and catch thing, but I''ll learn it.

How do I use the parseInt() function, and what''s all the other info I need? It sounds like a function that would fit perfectly here...

Share this post


Link to post
Share on other sites

  
boolean isInt;
int val = 0;

try
{
val = Integer.parseInt(s);
isInt = true;
}
catch (NumberFormatException e)
{
isInt = false;
}

if (isInt)
{
// do something with val

}
else
{
// do something (val is 0)

}

Share this post


Link to post
Share on other sites
c_wraith: That sucks, its completly against how try/catch is supposed to be used. It should be:


int val = 0;
try
{
val = Integer.parseInt(s);

// Continue processing val here
// ...
}
catch (NumberFormatException e)
{
// Code reaches here if var is not an int.
// do error checking
}

Share this post


Link to post
Share on other sites
You''re correct that it''s an ugly way to use try/catch. However, since there are no isXX() methods on the primitive''s container classes, it what you''re stuck with in many cases.


  
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int val;

while (true)
{
System.out.println("Please enter an integer (within range)");
try
{
val = Integer.parseInt(br.readLine());
break;
}
catch (NumberFormatException e)
{
System.out.println("Not an integer, or out of range. Try again");
}
}

// use val....



That''s probably how it''ll be used... Notice how the processing involving the number is NOT done in the try block. Putting the code in there would work, but it obscures the logic.

Since the code inside the try block will only be executed successfully once, yet it''s inside a while (true) loop, you really want to make what''s going on as clear as possible.

It is ugly. But it''s the least ugly option, in my experience.

Share this post


Link to post
Share on other sites