Results 1 to 4 of 4

Thread: Cast String to int....*Solved*

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Cast String to int....*Solved*

    Ok I have a car number like this

    DJ44444

    in a String object. Now I want the three numbers in the middle to be casted to a int. I know I kan use substring() to get the numbers out into a new string, but how do I convert them to an integer variable? This might be a dumb question but please answer. Thanks.
    Last edited by NoteMe; May 6th, 2003 at 02:52 PM.

  2. #2

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I got it....It was a stupid mistake...

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry. I just couldn't resist.
    Code:
    public class Z{
      public static void main(String[] args){
        String s = new String("DJ12345");
        String s2 = s.substring(2,5);
        int i = Integer.parseInt(s2); 
        System.out.println(i + i); 
      }
     }

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    It's ok....I just didn't think that parseInt was the right function. It said throws numberFormatException an I was scared and did run away...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width