Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A portion of the assignment that I'm currently doing, requires the user to select a pen color of their choosing. However whenever I run the program it won't allow me to input a string so that I can select a color. I've tried asking for a solution from other programmers and sites like stackoverflow, only to be told that I should include a
Java
System.out.print
statement, but that didn't solve the problem. What am I doing wrong?

What I have tried:

public class BallGame extends JFrame {

    public static void main(String[] args) {
        Scanner cool = new Scanner(System.in);
        System.out.println("Please enter the color that you wish to use to draw your point(color options: Red, blue, light blue, pink, green, orange, magenta, and yellow):");
        color = cool.nextLine();
        System.out.println(color);

        StdDraw.setPenRadius(0.05);

        if ("red".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.RED);
        } else if ("blue".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.BLUE);
        } else if ("light blue".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
        } else if ("pink".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.PINK);
        } else if ("green".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.GREEN);
        } else if ("orange".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.ORANGE);
        } else if ("magenta".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.MAGENTA);
        } else if ("yellow".equalsIgnoreCase(color)) {
            StdDraw.setPenColor(StdDraw.YELLOW);
        } else {
            StdDraw.setPenColor(StdDraw.CYAN);
        }
    }
}
Posted
Updated 2-Apr-18 16:55pm
v2
Comments
wseng 16-Nov-17 23:35pm    
what is StdDraw ?
Member 13525617 17-Nov-17 12:15pm    
StdDraw is a type of class.
wseng 19-Nov-17 11:39am    
Any error you get ? Which line having error ?
Richard MacCutchan 3-Apr-18 4:18am    
color = cool.nextLine();
This won't even compile because you have not declared a type for color. Judging from the rest of the code it should be a String.

[edit]
This is a Java GUI program so you do not have access to System.in. You need to take input via the window using a text box or other controls.
[/edit]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900