• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code without try-catch, just if/else or other  RSS feed

 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to change the "private static boolean isInteger(String s) {" section at the bottom of this code so it does Not use try-catch exception handling... just simple if/else type checks or something. I need to do this to do a comparison of the two ways to handle errors of input but everything I try with an if/else ends up with compilation errors.

Does anyone how to change the code below the "//HOW TO REMOVE TRY-CATCH FROM HERE???" comment to retain the same functionality check for Integers without using try-catch??

Any help would be appreciated.


[ June 14, 2004: Message edited by: Rob James ]
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first thing come to my mind is :

looping through the String s, for each
character ues Character.isDigit() to check.

Also pay attention to the possible positive/negative sign
at the beginning of the String s, as +/- are not digits
but could be part of valid integer.
[ June 14, 2004: Message edited by: chi Lin ]
 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chi Lin:
first thing come to my mind is :

looping through each char of the String s, for each
character ues Character.isDigit() to check.

Pay attention to the possible positive/negative sign
at the beginning of the String s


Chi,
Character.isDigit, ok, yes I have to check for negative numbers to.....Do you know any good examples of that in action I could reference?? I'm trying to search the web for something...


I just found this but I'm having trouble getting it in without errors...... is this the right stuff??


[ June 14, 2004: Message edited by: Rob James ]
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was discussed recently at great, great length in this very forum. See here.


--Tim
 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim West:
This was discussed recently at great, great length in this very forum. See here.


--Tim


His question is "without using Integer.parseInt(String)" which I have in mine so I can not follow that to an answer for mine it appears.
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said you were trying to change your method so it doesn't use try/catch. The thread I posted a link to asks essentially the same thing (ie, avoiding the try/catch on parseInt()).

If you want to use parseInt() to implement isInteger(), you'll have to catch the exception. If you don't want to use parseInt(), read the other thread and consider using the suggestions there.

...unless I've missed something obvious, that is.



--Tim
[ June 14, 2004: Message edited by: Tim West ]
 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim West:
You said you were trying to change your method so it doesn't use try/catch. The thread I posted a link to asks essentially the same thing (ie, avoiding the try/catch on parseInt()).

If you want to use parseInt() to implement isInteger(), you'll have to catch the exception. If you don't want to use parseInt(), read the other thread and consider using the suggestions there.

...unless I've missed something obvious, that is.



--Tim

[ June 14, 2004: Message edited by: Tim West ]


I'm trying to find something that will replace the isInteger() portion , that does not use try-catch, that will check if the input is an integer and also be able to handle a negative integer.

I've been trying to use isDigit() but have had troubles doing so.... basically whatever works I'll do whether its isInteger() or isDigit() or other..........so long as it'll check for an integer & a negative integer and allow the calculation to occur........

I'm a newbie......I'm open to suggestions

Rob
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to get rid of the try and catch clauses? Sure, you could write your own string parsing code, but someone has already done it for you - and better yet, debugged it - so why reinvent the wheel?
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to find something that will replace the isInteger() portion , that does not use try-catch, that will check if the input is an integer and also be able to handle a negative integer.


OK, so in the thread I linked to, the person asks:

How can I detect if the String represents a valid integer without using Integer.parseInt(String) and catch NumberFormatException ??


These are pretty much the same, once you realise that "does not use a try-catch" in this situation is pretty much equivalent to "without using Integer.parseInt() and catching the NumberFormatException".

Somewhere in that post, Max posts the solution:


boolean isInt= false;
if (in.matches("-?\\d{1,10}"))
{
double d = Double.parseDouble(in);
isInt = d == (int) d ;
}

(Quoted not coded to avoid UBB tag bug)

Doesn't that do exactly what you wanted???

FWIW, I'm not averse to the try/catch method - although catching unchecked exceptions is generally bad, in this case it's a clean, simple and obviously correct solution. It also keeps the "nastiness" inside one method, so it won't have greater repercussions.


--Tim
[ June 14, 2004: Message edited by: Tim West ]
 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim West:

(Quoted not coded to avoid UBB tag bug)

quote:
--------------------------------------------------------------------------------
How can I detect if the String represents a valid integer without using Integer.parseInt(String) and catch NumberFormatException ??
--------------------------------------------------------------------------------



These are pretty much the same, once you realise that "does not use a try-catch" in this situation is pretty much equivalent to "without using Integer.parseInt() and catching the NumberFormatException".


--Tim

[ June 14, 2004: Message edited by: Tim West ]


@Tim,
I think you misread my post... the post you are referring me to is to do it without try-catch AND without Integer.parseInt(String).

BUT I'm asking how to do it JUST without try-catch... anything else I can use. I can and have used Integer.parseInt(String) but I'm not entirely done. The code you quoted I'm not sure if it works, I'm trying to understand it now.


@Warren, I have to it the hard way without try-catch to learn it for a course I'm taking.



@Tim/Warren,

I've been told that what I should do is use the isDigit method of the Character class... Character.isDigit( ch ); Thats what I'm trying to work into that last part to augment public static boolean isInteger
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,

As Tim mentioned if you want to use Integer.parseInt(), you
have to handle the exception otherwise the program may terminated
ugly with some annoying exception message when user enter non-parsable string.

Originally posted by Tim West:
....
If you want to use parseInt() to implement isInteger(), you'll have to catch the exception.
....
 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chi Lin:
Rob,

As Tim mentioned if you want to use Integer.parseInt(), you
have to handle the exception otherwise the program may terminated
ugly with some annoying exception message when user enter non-parsable string.



Ughh, sorry guys, I've got a thousand things on the go lately... I guess I misread that part of Tim's post.... my mistake

That's kinda wierd though.... my contact for question for this course said I should implement a boolean method that will verify
whether a String consists of digits and that I might use public static boolean isInteger( String s ) to do that. He also said to verify whether an individual character of the input String is a digit, I might use the isDigit method of the Character class... Character.isDigit( ch );

So that was bad advice I got above?

I'll go reread the link to the other thread......

[ June 15, 2004: Message edited by: Rob James ]
[ June 15, 2004: Message edited by: Rob James ]
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, glad we got that all (semi) worked out.

Using isDigit is a good start, but there are other complications when you want to make the method 100% correct. Mostly these are to do with boundary conditions: although 100000000000000000 is a valid number, it isn't a valid Java int: it's too big. Similar for negative integers.

I think the parseDouble solution is nice. Otherwise, if you don't want to worry about overflow, digit-checking may be acceptable. Depends how correct you wanna be



--Tim
[ June 15, 2004: Message edited by: Tim West ]
 
Rob James
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim West:

Using isDigit is a good start, but there are other complications when you want to make the method 100% correct. Mostly these are to do with boundary conditions: although 100000000000000000 is a valid number, it isn't a valid Java int: it's too big. Similar for negative integers.
--Tim

[ June 15, 2004: Message edited by: Tim West ]


I don't get why my contact would tell me to use isDigit if you guys say it can't work with negative numbers. This is getting frustrating.


..................................


You know what, I just relooked at the other thread and that regex chat stuff sprung a memory of seeing something I saw somewhere......I researched and lookie here.......

private static boolean isInteger(String s) {
return s.matches("^-?\\d+$");
}


I replaced that with my old "commented out" private static boolean isInteger(String s) { code and she did it... i think she did, i'm testing right now.... thanks for all the responses & assistance.
[ June 15, 2004: Message edited by: Rob James ]
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"in" is the String you want to test.



The in.matches(...) line does the digit checking you were talking about - regular expression style. It returns true if in matches "an optional minus sign, then at least one, but no more than ten, digits".


--Tim
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!