java question

Started by
7 comments, last by Stibmit 21 years, 7 months ago
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?
If you keep on dividing (man) you end up as a collection of monkeys throwing nuts at each other out of separate trees." - Merlyn, The Once and Future King (T.H. White)
Advertisement
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.

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.
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
love the big green hobo
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...
If you keep on dividing (man) you end up as a collection of monkeys throwing nuts at each other out of separate trees." - Merlyn, The Once and Future King (T.H. White)

  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)}  
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
}
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.
Java''s (over)use of exceptions is beginning to get to me. I think. I think that they should remove (or at least reduce) the use of checked exceptions.
"Linux is not about free software, it is about community," -- Steve Balmer, Microsoft Chief Executive.

This topic is closed to new replies.

Advertisement