Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8573223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:12:04+00:00 2026-06-11T19:12:04+00:00

I am using GridWorld in my AP Computer Science class, and part of what

  • 0

I am using GridWorld in my AP Computer Science class, and part of what we are doing is changing the color of the bug (an object). I have found a very basic way to do this but am trying to incorporate strings to allow the user to type in what color he would like the bug to be instead of having to put in number values for RGB. I am able to get the string value to be the color that I want, by typing in, “red” and having the string store that. But how would I get that string to translate into the color? I’m not really sure if I worded this clearly enough but I have attached my code so hopefully someone will understand and can help.

Color red = new Color (255, 0, 0);
Color green = new Color (0, 255, 0);
Color blue = new Color (0, 0, 255);

System.out.println("What color would you like the first bug to be? (red, green, blue)");
String name = "color1";
String color1 = keyboard.next();

if (color1 == "red")
{
   world.add (new Location (bugx1, bugy1), new Bug(red));
}
if (color1 == "blue")
{
   world.add (new Location (bugx1, bugy1), new Bug(blue));
}
if (color1 == "green")
{
   world.add (new Location (bugx1, bugy1), new Bug(green));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:12:05+00:00Added an answer on June 11, 2026 at 7:12 pm

    This old chestnut…

    When comparing Strings in java, you must use .equals(). The == operator is true only if both operands are the same exact object. Try this:

    if (color1.equals("red"))
    

    This is a common “mistake” made by programmers familiar with javascript, where the == operator would work as you have coded it. IMHO, the “mistake” is actually in the java language specification – they should have allowed == to execute equals() and use === for identity comparison (which is rarely actually used).


    To do this properly, I recommend using an enum for the colors. Then your code would be just one line:

    public enum BugColor {
        red(255, 0, 0),
        green(0, 255, 0),
        blue(0, 0, 255);
    
        private final Color color;
    
        BugColor(int r, int g, int b) {
            color = new Color(r, g, b);
        }
    
        public Color getColor() {
            return color;
        }
    }
    

    then simply:

    String color1 = keyboard.next();
    world.add (new Location (bugx1, bugy1), new Bug(BugColor.valueOf(color1).getColor()));
    

    If Color is an interface, you can simplify and improve your code by making the enum BugColor extends Color – I’ll leave the implementation as an exercise for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using MVC2 I have an AJAX form which is posting to a bound model.
Using Core Data, I have a fetch request to fetch the minimum of a
I am trying to do an assignment using Gridworld, a case study put out
Using Java,I have to fetch multiple sets of values from an XML file to
Using MVVM. I have a DataTemplate which I am using to display an expander
using chrome and ff so far have made a nasty glitch when using scrollTo
Using the ndk I have compiled a code written in C. The program is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.