Results 1 to 13 of 13
- 09-25-2010, 12:55 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
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.
- 09-25-2010, 01:17 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 09-25-2010, 01:25 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
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.
- 09-25-2010, 01:30 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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; } }
- 09-25-2010, 01:32 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
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.
- 09-25-2010, 01:56 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
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; }
- 09-25-2010, 02:22 AM #7
r = components[0];
Is this reversed?
- 09-25-2010, 02:27 AM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
this will never be true, again look at my println statements in void main from earlierJava Code:if (standardColors[i].toString().equals(stringColor)) {
- 09-25-2010, 02:43 AM #9
Cross posted at Java homework help - Java
- 09-25-2010, 03:12 AM #10
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
- 09-25-2010, 03:15 AM #11
Member
- Join Date
- Sep 2010
- Posts
- 21
- Rep Power
- 0
- 09-25-2010, 03:15 AM #12
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 09-25-2010, 06:13 AM #13
Similar Threads
-
need in help in comparing Strings
By jaq in forum New To JavaReplies: 1Last Post: 11-25-2009, 01:06 PM -
homework help comparing strings
By djester55 in forum New To JavaReplies: 2Last Post: 10-19-2009, 10:15 PM -
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02-25-2009, 07:14 PM -
comparing strings
By diggitydoggz in forum New To JavaReplies: 7Last Post: 12-23-2008, 04:40 AM -
Comparing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks