Results 1 to 13 of 13
  1. #1
    SergeantJoKer is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Unhappy Homework help involving comparing Color objects to Strings

    Hey everyone. I could use some serious help with a Java method that I have to construct for my CMIS class. Basically, I have to develop a static method that has two arrays in it, one a String array that has values of colors; the other, a Color array that has the proper names for the colors. I have to take a user inputted String and compare it the the Color array and when a match is found, return the RGB integer values of the color as a 3 value int array. I have placed my code below for what I have. I believe that everything is correct except for the if statement that compares the String to the Color object. All help is greatly appreciated. Thanks!

    EDIT:


    Java Code:
    import java.awt.Color;
    
    /**
     *
     * @author johnkershaw
     */
    public class homework7 {
    
        /**
         * @param args the command line arguments
         */
       public static void main(String[] args) {
            // TODO code application logic here
    }
        
    
        public static int[] getColors(String stringColor) {
    
            Color[] standardColors = new Color[] {
                Color.BLACK, Color.BLUE, Color.CYAN,
            Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
            Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
            Color.YELLOW};
            
            
    
            String[] inputColors = {"BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY",
            "GREEN", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK", "RED", "WHITE",
            "YELLOW"};
            int[] components = new int[3];
            //Color color = new Color();
            for (int i = 0; i < standardColors.length; i++) {
                if (stringColor.equals(standardColors[i].toString()))
                
                //if (Color.stringColor.equals(standardColors[i]))
    
                    
                    //standardColors[i] = new Color();
    
    
    
                     int r = standardColors[i].getRed();
                     int g = standardColors[i].getGreen();
                     int b = standardColors[i].getBlue();
    
    
                     components[0] = r;
                     components[1] = g;
                     components[2] = b;
            }
    
                     
                     return components;
                     
                     
            }
                
        }
    Last edited by SergeantJoKer; 09-25-2010 at 01:25 AM.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    your classname does not follow java standards, your if statement has not a complete body (best way I can define it), void main(String[]args) is not closed off properly.

    Revise your basic java.

  3. #3
    SergeantJoKer is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    I have changed the curly brace on the void main statement. As regards to my class name not following standards, I am not sure what is wrong with it but I am only using the name for my own purposes. The "if" statement is what is wrong with the whole thing. I cannot figure out how to compare the String variable to the Color object to see if they match or not.

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    I have re-worked your code slightly, take special interest in the 3 println statements in side static void main, its key to your problem i feel.

    Java Code:
    public class homework7 {
    
        /**
         * @param args the command line arguments
         */
       public static void main(String[] args) {
    	   	/* have a look at these three println values*/
             System.out.println(Color.RED.toString().equals("RED")?"true":"false");
             System.out.println(Color.RED);
             System.out.println("RED");
             
       }
        public static int[] getColors(String stringColor) {
    
            Color[] standardColors = {
                Color.BLACK, Color.BLUE, Color.CYAN,
            Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
            Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
            Color.YELLOW};
            
            
    
            String[] inputColors = {"BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY",
            "GREEN", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK", "RED", "WHITE",
            "YELLOW"};
            int[] components = new int[3];
            //Color color = new Color();
            for (int i = 0; i < standardColors.length; i++) {
                
    			if (stringColor.equals(standardColors[i].toString())){
                
                //if (Color.stringColor.equals(standardColors[i]))
    
                    
                    //standardColors[i] = new Color();
    
    
    
                     int r = standardColors[i].getRed();
                     int g = standardColors[i].getGreen();
                     int b = standardColors[i].getBlue();
    
    
                     components[0] = r;
                     components[1] = g;
                     components[2] = b;
            }
    
                     
                     return components;
                     
                     
            }
    		return null;
        }
    }

  5. #5
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    I am trying to find a method that could help you...wow!!!! i couldnt find something that seems so easy...the only method that converts string to color is the decode(), but it gets as parameter the octal or deximal number.
    I recommend you to to visit this site which says all the methods of the Class Color and find how to use them in order to get what you want.

    Color (Java 2 Platform SE v1.4.2)

    But converting the color to string with the toString() doesnt return you tha name but only the r,g, b...so check about decode()...it maybe your solutions but maybe you have to do many alterations to get what you want..

    edit : if decode() is your key you can take the name of the color from the user...
    then with if-else you can parse the right deximal number (something tells me that you have to look for them and put them by yourself)
    and the you can use this new string for the method decode().
    Last edited by jlmp; 09-25-2010 at 01:43 AM.

  6. #6
    SergeantJoKer is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Jlmp: Tell me about it. It seems easy but have been beating my head all day about this. I will try the decode() method in a bit if nothing else works. Sure hope its not that, because that has definitely not been covered in this class...

    Al: Unfortunately, my solution must all be within the method and I need it to return the integer RGB value of the color once it matches in the array.

    This is my updated code, which compiles but doesn't produce the required result...The output is supposed to be the array of integers that make up each color but instead, I am only outputing 0's...

    Java Code:
        public static int[] getColors(String stringColor) {
    
            Color[] standardColors = {
                Color.BLACK, Color.BLUE, Color.CYAN,
            Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
            Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
            Color.YELLOW};
    
    
    
            String[] inputColors = {"BLACK", "BLUE", "CYAN", "DARK_GRAY", "GRAY",
            "GREEN", "LIGHT_GRAY", "MAGENTA", "ORANGE", "PINK", "RED", "WHITE",
            "YELLOW"};
    
            int[] components = new int[3];
            //Color color = new Color();
            for (int i = 0; i < standardColors.length; i++) {
    
    			if (standardColors[i].toString().equals(stringColor)) {
    
                     int r = standardColors[i].getRed();
                     int g = standardColors[i].getGreen();
                     int b = standardColors[i].getBlue();
    
    
                     r = components[0];
                     g = components[1];
                     b = components[2];
            }
    
            }
    	return components;
        }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    r = components[0];
    Is this reversed?

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    if (standardColors[i].toString().equals(stringColor)) {
    this will never be true, again look at my println statements in void main from earlier

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Cross posted at Java homework help - Java

  10. #10
    SergeantJoKer is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    Java Code:
    if (standardColors[i].toString().equals(stringColor)) {
    this will never be true, again look at my println statements in void main from earlier
    Why won't this be true? I recognize the fact that your lines in the main make it all work but the project needs to be all contained within the getColor method, that is why I am trying to decipher the "if" statement.

  11. #11
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by SergeantJoKer View Post
    Why won't this be true? I recognize the fact that your lines in the main make it all work but the project needs to be all contained within the getColor method, that is why I am trying to decipher the "if" statement.
    As I said:
    By converting the color to string with the toString() doesnt return you the name but only the r,g, b so..you will never get true with this statement

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    My lines in main do not make your program work but serve to highlight why that if statement you are complaining about will never equate to true.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,235
    Rep Power
    19

    Default

    Quote Originally Posted by Norm View Post
    Thanks for the heads-up, Norm

    db

Similar Threads

  1. need in help in comparing Strings
    By jaq in forum New To Java
    Replies: 1
    Last Post: 11-25-2009, 01:06 PM
  2. homework help comparing strings
    By djester55 in forum New To Java
    Replies: 2
    Last Post: 10-19-2009, 10:15 PM
  3. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  4. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 04:40 AM
  5. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM

Tags for this Thread

Posting Permissions

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