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

Thread: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    Hi There,

    JDK 1.7 via NetBeans IDE 7.3.1

    I have a program that asks for user input using .keyReadString();

    The user can type in a color to be used to color a shape.

    I know that color words can only be blue or BLUE.

    How do you convert the string the user enters into a form that the drawShape( format ) can use?

    I've tried:
    String eColor = sg.keyReadString();
    sg.drawFilledEllipse(startX, startY, (int)radius1, (int)radius2, Color.eColor, 1.0, "");

    sg.drawFilledEllipse(startX, startY, (int)radius1, (int)radius2, Color.getColor(eColor), 1.0, "");

    String eColor = sg.keyReadString();
    double eColorC = Double.parseDouble(eColor);
    sg.drawFilledEllipse(startX, startY, (int)radius1, (int)radius2, Color.eColorC, 1.0, "");

    etc., always getting an error when running the program, no errors on the code itself.
    Exception in thread "main" java.lang.NumberFormatException: For input string: "RED"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241)
    at java.lang.Double.parseDouble(Double.java:540)
    at shapedrawertest.ShapeDrawerTest.main(ShapeDrawerTe st.java:30)

    Any ideas would be appreciated!!

    Regards,


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    Exception in thread "main" java.lang.NumberFormatException: For input string: "RED"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241)
    The user needs to enter the type of data that the program is trying to read. The String "RED" is not a number: NumberFormatException
    Or the program needs to be rewritten to read in a String and use its contents to make a decision by using some of the String class's methods.

    --- Update ---

    What packages and classes is your program using? I do not recognize many of them.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2013
    Posts
    96
    My Mood
    Amused
    Thanks
    16
    Thanked 9 Times in 9 Posts

    Default Re: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    I think you would need to use some if statements. When you refer to something like Color.BLUE, you're referring to an integer value in the Color object. You can't really glue the word BLUE on to a string "Color." to create a Color.BLUE statement, as far as I know. You could do a series of String.compareToIgnoreCase() if statements, and assign values to a Color variable accordingly. So like...

    Color drawingColor;
    if eColor is "BLUE"
    --> set drawingColor equal to Color.BLUE;

    and so on for however many colors you have.

    I could be wrong, but I don't think you can use Java to build variable names dynamically, which is what you seem to be trying to do. You gotta hard code them.

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    Hi and Thank you.

    import simplegui.SimpleGUI;
    import java.awt.Color;

    The SimpleGUI is from a prepackage of demos for doing different things; from here: https://sites.google.com/a/temple.edu/simplegui/
    On the left is a Downloads section, containing the prepack of files.

    --- Update ---

    Though I didn't think it wise to post all the code, the request from user for inputs are in an If-else statement.

    I can type the color word into the .drawShape format and it works, but I can get the user input assigned to some kind of variable and use that variable in the .drawShape( ... Color._____ ) format.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    The value of Color._______ is set when the program is compiled. The code will need to use a variable that gets assigned a value depending on what the user types in.

    --- Update ---

    The variable would have to be of type Color.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    Hmm, so it sounds like the following:

    sg.print("What color would you like your Ellipse to be?"); //Prompt user for a color.
    String eColor = sg.keyReadString(); //reads the color word user enters as a string to variable eColor

    Say the user entered red
    the String variable eColor is now set to red.

    How do you convert the Sting variable eColor to a a Color variable for use in the drawShape ( Color.____)

    Just not possible? If not, how do you get the user to give you input that can be used as a their chosen color to draw a shape. Sounds very straight forward to me 8-)

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: How to take User Inputted String for color, and use it in the drawShape(x,y,x,y, Color. )

    Use if statements to compare the String to each of the desired Strings with the colors: "RED", "BLUE", etc.
    When there is a match, assign the Color variable the associated Color value: Color.RED, Color.BLUE, ...

    --- Update ---

    There needs to be a variable of type Color that will be assigned the Color value.
    It will be used in the drawShape method: (,,,theColorVariableHere)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to store a number that was inputted by the user? please help
    By shep in forum Java Theory & Questions
    Replies: 3
    Last Post: December 1st, 2012, 09:52 AM
  2. Testing if an inputted String exists in an String Array
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2012, 02:46 PM
  3. Why does the Color class have two variables for each color?
    By Melawe in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2012, 04:21 PM
  4. Help me Color in this section
    By Bagzli in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2012, 04:13 PM
  5. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM

Tags for this Thread