hello . . . I have a new prob in here. I WANT to convert a string value
to INTEGER. The following will compile but returns an error when I run
the code because of the presence of the letter "c" on the third
element. How will I skip this element and proceed to the next element.
public void test()
{
String[] x = {"1","2","c","3","5"};
int counter = 0;
int number = 0;
try
{
while(counter < x.length)
{
number =
Integer.parseInt(x[counter]);
System.out.println("the NUMBER is:
"+number+".");
counter++;
}
} catch(Exception ex)
{
System.out.println("Error in test() "+e.toString());
}
}
Furious George - 13 May 2006 11:42 GMT
> hello . . . I have a new prob in here. I WANT to convert a string value
> to INTEGER. The following will compile but returns an error when I run
> the code because of the presence of the letter "c" on the third
> element. How will I skip this element and proceed to the next element.
You just wrap the try block differently. Instead of wrapping it around
the while loop, put it inside of the while loop ... like this
try { number = Integer.parseInt(x[counter]); System.out.println("The
Number is:" + number ) ;} catch (Exception e) { System.err.println (
"You are really stupid!"); } finally { System.out.println("You should
remove the finally block or your professor will know you cheated");}
> public void test()
> {
[quoted text clipped - 18 lines]
> }
> }
IveCal - 14 May 2006 11:14 GMT
Oh, yes! I encountered this solution many months ago but I forgot.
Thanks sir...
Bjorn Abelli - 13 May 2006 11:46 GMT
"IveCal" <ive.cal@gmail.com> wrote...
> hello . . . I have a new prob in here. I WANT to convert a string value
> to INTEGER. The following will compile but returns an error when I run
> the code because of the presence of the letter "c" on the third
> element. How will I skip this element and proceed to the next element.
There are several possibilities for this. One example is to catch the
NumberFormatException thrown by parseInt, and simply skip that String...
class Test
{
public static void main (String[] args)
{
String[] x = {"1","2","c","3","5"};
int counter = 0;
int number = 0;
while(counter < x.length)
{
try
{
number =
Integer.parseInt(x[counter]);
System.out.println(
"the NUMBER is " + number + ".");
}
catch(NumberFormatException ex)
{
continue;
}
finally
{
counter++;
}
}
}
}
Another possibility, that of some are preferred, is to check if the String
contains a parseable numeric value before trying to parse it. This can e.g.
be done with String.matches(...), or some other RegEx construction.
// Bjorn A
IveCal - 14 May 2006 11:11 GMT
Thanks... This is good... Hmmm... Thanks...
Bjorn Abelli - 13 May 2006 11:59 GMT
"IveCal" wrote...
> hello . . . I have a new prob in here. I WANT to convert
> a string value to INTEGER. The following will compile but
> returns an error when I run the code because of the presence
> of the letter "c" on the third element. How will I skip this
> element and proceed to the next element.
Why skip it?
In this case you can use:
number = Integer.parseInt(x[counter], 16);
:-)
// Bjorn A
IveCal - 14 May 2006 11:08 GMT
You gave an idea . . . Thanks a lot . . .