4 Replies - 6136 Views - Last Post: 24 October 2005 - 08:24 AM Rate Topic: -----

#1 Drift-O-Manaic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 19-August 05

String Help

Post icon  Posted 23 October 2005 - 12:41 PM

I am making a program where I am importing a string and converting it to a integer. I would like to be able to know is there is a way to tell is there are any characters in the string so that I can through my own error so that the computer does not have to. I knew how to do this in C++ but I have not figured out how to do this in java. Is it possible in java?
Is This A Good Question/Topic? 0
  • +

Replies To: String Help

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: String Help

Posted 23 October 2005 - 02:13 PM

You can loop through the string and check each character to see if it's numeric, using things like isNaN, but I would caution you to let the java compiler do it through an exception process when you try to convert it (I'm assuming you are using one of the parseTo? methods...if t cannot be converted, and error will be thrown, you can catch it and set your response...this method is much less taxing performance wise than making your own validation and looping through the string.
Was This Post Helpful? 0
  • +
  • -

#3 Drift-O-Manaic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 19-August 05

Re: String Help

Post icon  Posted 23 October 2005 - 04:42 PM

How do you catch the error and set your own response?

That would accomplish every thing I need.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: String Help

Posted 23 October 2005 - 07:23 PM

Defined methods in Java throw exception errors...in this case, you have stated that you will be converting a string to an integer. You can do this by using the parseInt method of the Integer object. When an error is encountered with that method, it throws a NumberFormatException error. The general procedure is this:

Try to task, catch the error if one is thrown. Take the following code:
public class test
{
   public static void main (String args[])
   {
      int i;
      String s1 = "1";
      try
      {
         i = Integer.parseInt(s1);
      }
      catch(NumberFormatException e)
      {
         System.out.println("Conversion error");
      }
   }
}


When run, the error message is not printed, but the following:
public class test
{
   public static void main (String args[])
   {
      int i;
      String s1 = "a";
      try
      {
         i = Integer.parseInt(s1);
      }
      catch(NumberFormatException e)
      {
         System.out.println("Conversion error");
      }
   }
}


results in the error message being printed. As part of the catch, you can do whatever you wish...set a flag, loop, whatever...

In general it is better to use inherent methods in a programming language.
Was This Post Helpful? 0
  • +
  • -

#5 Drift-O-Manaic  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 19-August 05

Re: String Help

Posted 24 October 2005 - 08:24 AM

WOW!! I can't believe I didn't remember that :crazy:

I used that in the first program that I did but I did not realy know what it did.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1