Results 1 to 5 of 5
  1. #1
    Abhishek is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default Method to write if the string object can be converted to Integer or not.

    I need to write a method to see if the string representation of the object can be translated to an integer or not.ASAP

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Abhishek is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default

    String Object.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, you can't convert String object like "abcde" into int value. Only you can convert String objects like "123123" into int value.

    So depend on that try to build your method.

    Hope that what you expect.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    A code something like this may help to you.

    Java Code:
        public void StringToInt(){
            String charString = "Java"; // Try this for exception
            String numericString = "262";
            
            try{
                int intOfString = Integer.parseInt(numericString);  // String should be numeric
                System.out.println(intOfString);
            }
            catch(NumberFormatException nfe){
                System.out.println("NumberFormatException:" + nfe.getMessage());
            }
        }

Similar Threads

  1. How to write multiline String in a JLabel
    By JavaBean in forum AWT / Swing
    Replies: 4
    Last Post: 12-14-2009, 06:09 AM
  2. Replies: 0
    Last Post: 04-06-2008, 08:43 PM
  3. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:22 PM
  4. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 04:59 PM
  5. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM

Posting Permissions

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