Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Skip an EXCEPTION!!!

Thread view: 
IveCal - 13 May 2006 11:30 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.

    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 . . .


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.