Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Problem with String (and integer) conversion!

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Problem with String (and integer) conversion!

    The program below shows an array of "crayons". The user should be able to enter a number between 1-8 and show the appropriate crayon. Also, when the user type "end", it should terminate the program. My problem is that it ends the program when I type "end", but it's giving me the error. Since my variable is a String, to show the array, I've converted it to an integer using Integer.parseInt() method.

    The error:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "end"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.parseInt(Integer.java:527)
    	at Crayon.main(Crayon.java:32)
    Java Result: 1

    Result:

    Color: Red 		Length in mm: 16 	Sharpness: 1
    Color: Yellow 		Length in mm: 17 	Sharpness: 4
    Color: Green 		Length in mm: 10 	Sharpness: 2
    Color: Blue 		Length in mm: 11 	Sharpness: 5
    Color: Brown 		Length in mm: 16 	Sharpness: 1
    Color: Black 		Length in mm: 23 	Sharpness: 2
    Color: Orange 		Length in mm: 29 	Sharpness: 3
    Color: Purple 		Length in mm: 17 	Sharpness: 4
    Which crayon you like to see? (1-8) 3
    That crayon is Color: Green 		Length in mm: 10 	Sharpness: 2
    Color: Red 		Length in mm: 16 	Sharpness: 1
    Color: Yellow 		Length in mm: 17 	Sharpness: 4
    Color: Green 		Length in mm: 10 	Sharpness: 2
    Color: Blue 		Length in mm: 11 	Sharpness: 5
    Color: Brown 		Length in mm: 16 	Sharpness: 1
    Color: Black 		Length in mm: 23 	Sharpness: 2
    Color: Orange 		Length in mm: 29 	Sharpness: 3
    Color: Purple 		Length in mm: 17 	Sharpness: 4
    Which crayon you like to see? (1-8) end
    Exception in thread "main" java.lang.NumberFormatException: For input string: "end"
    	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    	at java.lang.Integer.parseInt(Integer.java:492)
    	at java.lang.Integer.parseInt(Integer.java:527)
    	at Crayon.main(Crayon.java:32)
    Java Result: 1

    Code:

    import java.util.Scanner;
    import java.util.Random;
     
    public class Crayon {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            Random rand = new Random();
            String choice;
     
            choice = "";
     
            String[]crayon = {"Color: Red \t\tLength in mm: 16 \tSharpness: 1",
                              "Color: Yellow \t\tLength in mm: 17 \tSharpness: 4",
                              "Color: Green \t\tLength in mm: 10 \tSharpness: 2",
                              "Color: Blue \t\tLength in mm: 11 \tSharpness: 5",
                              "Color: Brown \t\tLength in mm: 16 \tSharpness: 1",
                              "Color: Black \t\tLength in mm: 23 \tSharpness: 2",
                              "Color: Orange \t\tLength in mm: 29 \tSharpness: 3",
                              "Color: Purple \t\tLength in mm: 17 \tSharpness: 4",}; 
     
            while(!choice.equals("end"))
            {
                for(int a=0; a<crayon.length; a++)
                {
                    System.out.println(crayon[a]);
                }
     
                System.out.print("Which crayon you like to see? (1-8) ");
                choice = scan.nextLine();            
                System.out.println("That crayon is " + crayon[Integer.parseInt(choice) - 1]);        
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with String (and integer) conversion!

    Check the value of choice for "end" before trying to convert it to an int.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    tonynsx (November 7th, 2013)

  4. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with String (and integer) conversion!

    Quote Originally Posted by Norm View Post
    Check the value of choice for "end" before trying to convert it to an int.
    I'm sorry, but not sure if I understand.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with String (and integer) conversion!

    If string is not "end" then convert string to int
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Problem with String (and integer) conversion!

    Thank you so much, it works now

    Result:

    run:
    Color: Red 		Length in mm: 16 	Sharpness: 1
    Color: Yellow 		Length in mm: 17 	Sharpness: 4
    Color: Green 		Length in mm: 10 	Sharpness: 2
    Color: Blue 		Length in mm: 11 	Sharpness: 5
    Color: Brown 		Length in mm: 16 	Sharpness: 1
    Color: Black 		Length in mm: 23 	Sharpness: 2
    Color: Orange 		Length in mm: 29 	Sharpness: 3
    Color: Purple 		Length in mm: 17 	Sharpness: 4
    Which crayon you like to see? (1-8) type [end] to terminate 1
    That crayon is Color: Red 		Length in mm: 16 	Sharpness: 1
    Color: Red 		Length in mm: 16 	Sharpness: 1
    Color: Yellow 		Length in mm: 17 	Sharpness: 4
    Color: Green 		Length in mm: 10 	Sharpness: 2
    Color: Blue 		Length in mm: 11 	Sharpness: 5
    Color: Brown 		Length in mm: 16 	Sharpness: 1
    Color: Black 		Length in mm: 23 	Sharpness: 2
    Color: Orange 		Length in mm: 29 	Sharpness: 3
    Color: Purple 		Length in mm: 17 	Sharpness: 4
    Which crayon you like to see? (1-8) type [end] to terminate 2
    That crayon is Color: Yellow 		Length in mm: 17 	Sharpness: 4
    Color: Red 		Length in mm: 16 	Sharpness: 1
    Color: Yellow 		Length in mm: 17 	Sharpness: 4
    Color: Green 		Length in mm: 10 	Sharpness: 2
    Color: Blue 		Length in mm: 11 	Sharpness: 5
    Color: Brown 		Length in mm: 16 	Sharpness: 1
    Color: Black 		Length in mm: 23 	Sharpness: 2
    Color: Orange 		Length in mm: 29 	Sharpness: 3
    Color: Purple 		Length in mm: 17 	Sharpness: 4
    Which crayon you like to see? (1-8) type [end] to terminate end
    BUILD SUCCESSFUL (total time: 6 seconds)

    New Code:

    import java.util.Scanner;
    import java.util.Random;
     
    public class Crayon {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            Random rand = new Random();
            String choice;
     
            choice = "";
     
            String[]crayon = {"Color: Red \t\tLength in mm: 16 \tSharpness: 1",
                              "Color: Yellow \t\tLength in mm: 17 \tSharpness: 4",
                              "Color: Green \t\tLength in mm: 10 \tSharpness: 2",
                              "Color: Blue \t\tLength in mm: 11 \tSharpness: 5",
                              "Color: Brown \t\tLength in mm: 16 \tSharpness: 1",
                              "Color: Black \t\tLength in mm: 23 \tSharpness: 2",
                              "Color: Orange \t\tLength in mm: 29 \tSharpness: 3",
                              "Color: Purple \t\tLength in mm: 17 \tSharpness: 4",}; 
     
            while(!choice.equals("end"))
            {
                for(int a=0; a<crayon.length; a++)
                {
                    System.out.println(crayon[a]);
                }
     
                System.out.print("Which crayon you like to see? (1-8) type [end] to terminate ");
                choice = scan.nextLine();          
     
                //If string is not "end" then convert string to int 
     
                if(!choice.equals("end"))
                {
                    System.out.println("That crayon is " + crayon[Integer.parseInt(choice) - 1]);  
                }   
            }
        }
    }

Similar Threads

  1. DECtoBIN Conversion WITHOUT using String Methods
    By Putsie in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2013, 04:18 PM
  2. String to Double Conversion
    By suteki73 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2013, 05:51 AM
  3. Replies: 6
    Last Post: June 3rd, 2013, 04:57 AM
  4. Need help regarding String to int Conversion
    By fredsilvester93 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 20th, 2012, 04:39 PM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 07:06 AM