469,950 Members | 2,054 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Help with loops

nomad
664 Expert 512MB
I'm very new to java and programing.
I need some help with this.
Check each user entry to ensure validity. Color is either Black, White, or Red. if the user enter the wrong color the following with show ERROR – Incorrect color – try again:

I'm tring to figure out if this is a
while loop or a do while loop. and help would be great.

Here is what I have so far.

//make a Scanner object for color
Scanner kbd_color = new Scanner(System.in);

//prompt the user for data and start adding it to the object
System.out.print("\nEnter Product Color: ");
System.out.print("\nMust be Black, White, or Red: ");
color_flag = kbd_color.next();

//compare color from input of the scanner to color_set1
if (color_flag.equalsIgnoreCase("black"));
System.out.printf("Color is: ", color_set1);
System.out.println(color_set1);
//compare color from input of the scanner to color_set2
if(color_flag.equalsIgnoreCase("white"));
System.out.printf("Color is: ", color_set2);
System.out.println(color_set2);
//compare color from input of the scanner to color_set3
if(color_flag.equalsIgnoreCase("red"));
System.out.printf("Color is: ", color_set3);
System.out.println(color_set3);
//compare color with scanner if it does not equals color_set() then print white
while
(color_flag != color_set1 || color_flag != color_set2 || color_flag != color_set2);
{
System.out.print("Incorrect Color - try again");// if color is not correct then return you to input
color_flag = kbd_color.next();
}

//print out...
System.out.print("Color: ");
System.out.println(color_flag);

//Setting the values for color
public static final String color_set1 = ("black");
public static final String color_set2 = ("white");
public static final String color_set3 = ("red");
}

Thanks
nomad
Mar 14 '07 #1
4 1353
r035198x
13,262 8TB
I'm very new to java and programing.
I need some help with this.
Check each user entry to ensure validity. Color is either Black, White, or Red. if the user enter the wrong color the following with show ERROR Incorrect color try again:

I'm tring to figure out if this is a
while loop or a do while loop. and help would be great.

Here is what I have so far.

//make a Scanner object for color
Scanner kbd_color = new Scanner(System.in);

//prompt the user for data and start adding it to the object
System.out.print("\nEnter Product Color: ");
System.out.print("\nMust be Black, White, or Red: ");
color_flag = kbd_color.next();

//compare color from input of the scanner to color_set1
if (color_flag.equalsIgnoreCase("black"));
System.out.printf("Color is: ", color_set1);
System.out.println(color_set1);
//compare color from input of the scanner to color_set2
if(color_flag.equalsIgnoreCase("white"));
System.out.printf("Color is: ", color_set2);
System.out.println(color_set2);
//compare color from input of the scanner to color_set3
if(color_flag.equalsIgnoreCase("red"));
System.out.printf("Color is: ", color_set3);
System.out.println(color_set3);
//compare color with scanner if it does not equals color_set() then print white
while
(color_flag != color_set1 || color_flag != color_set2 || color_flag != color_set2);
{
System.out.print("Incorrect Color - try again");// if color is not correct then return you to input
color_flag = kbd_color.next();
}

//print out...
System.out.print("Color: ");
System.out.println(color_flag);

//Setting the values for color
public static final String color_set1 = ("black");
public static final String color_set2 = ("white");
public static final String color_set3 = ("red");
}

Thanks
nomad
Alright then

1.) You should indent your code. It makes it easier to follow
See if (color_flag.equalsIgnoreCase("black")); is not what you want. You don't put the ; for the if statement. Remove it at the end of your if statements

2.) What do you want to use the loop for? How many colours do you want to set?

3.)Wrap your code around code tags when posting code.
Mar 14 '07 #2
nomad
664 Expert 512MB
Here is the update

2.) What do you want to use the loop for? How many colours do you want to set? Three colors, black, white and red. If they don't enter the right color the scanner will take you back until you enter the right color.

Expand|Select|Wrap|Line Numbers
  1. //        make a Scanner object for color
  2.         Scanner kbd_color = new Scanner(System.in);
  3.  
  4. //        prompt the user for data and start adding it to the object
  5.         System.out.print("\nEnter Product Color: ");
  6.         System.out.print("\nMust be Black, White, or Red: ");
  7.         color_flag = kbd_color.next();
  8.  
  9. //        compare color from input of the scanner to color_set1
  10.         if (color_flag.equalsIgnoreCase("black"))
  11.             System.out.printf("Color is: ", color_set1);
  12.         System.out.println(color_set1);
  13. //        compare color from input of the scanner to color_set2
  14.         if(color_flag.equalsIgnoreCase("white"))
  15.             System.out.printf("Color is: ", color_set2);
  16.         System.out.println(color_set2);
  17. //        compare color from input of the scanner to color_set3
  18.         if(color_flag.equalsIgnoreCase("red"))
  19.             System.out.printf("Color is: ", color_set3);
  20.         System.out.println(color_set3);
  21. //        compare color with scanner if it  does not equals color_set() then print white
  22.         while
  23.             (color_flag != color_set1 || color_flag != color_set2 || color_flag != color_set2);
  24.         {
  25.             System.out.print("Incorrect Color - try again");// if color is not correct then return you to input
  26.             color_flag = kbd_color.next();
  27.         }
  28.  
  29. //Printing the values
  30.     System.out.print("Color: ");
  31.     System.out.println(color_flag);
  32.  
  33. //    Setting the values for color
  34.     public static final String color_set1 = ("black");
  35.     public static final String color_set2 = ("white");
  36.     public static final String color_set3 = ("red");
  37.  
  38. }
thanks
Mar 14 '07 #3
r035198x
13,262 8TB
Here is the update

2.) What do you want to use the loop for? How many colours do you want to set? Three colors, black, white and red. If they don't enter the right color the scanner will take you back until you enter the right color.

Expand|Select|Wrap|Line Numbers
  1. //        make a Scanner object for color
  2.         Scanner kbd_color = new Scanner(System.in);
  3.  
  4. //        prompt the user for data and start adding it to the object
  5.         System.out.print("\nEnter Product Color: ");
  6.         System.out.print("\nMust be Black, White, or Red: ");
  7.         color_flag = kbd_color.next();
  8.  
  9. //        compare color from input of the scanner to color_set1
  10.         if (color_flag.equalsIgnoreCase("black"))
  11.             System.out.printf("Color is: ", color_set1);
  12.         System.out.println(color_set1);
  13. //        compare color from input of the scanner to color_set2
  14.         if(color_flag.equalsIgnoreCase("white"))
  15.             System.out.printf("Color is: ", color_set2);
  16.         System.out.println(color_set2);
  17. //        compare color from input of the scanner to color_set3
  18.         if(color_flag.equalsIgnoreCase("red"))
  19.             System.out.printf("Color is: ", color_set3);
  20.         System.out.println(color_set3);
  21. //        compare color with scanner if it does not equals color_set() then print white
  22.         while
  23.             (color_flag != color_set1 || color_flag != color_set2 || color_flag != color_set2);
  24.         {
  25.             System.out.print("Incorrect Color - try again");// if color is not correct then return you to input
  26.             color_flag = kbd_color.next();
  27.         }
  28.  
  29. //Printing the values
  30.     System.out.print("Color: ");
  31.     System.out.println(color_flag);
  32.  
  33. //    Setting the values for color
  34.     public static final String color_set1 = ("black");
  35.     public static final String color_set2 = ("white");
  36.     public static final String color_set3 = ("red");
  37.  
  38. }
thanks
and where do you want to store the colors?
You can use a boolean like this


Scanner kbd_color = new Scanner(System.in);
boolean correct = true;
Expand|Select|Wrap|Line Numbers
  1.  while(correct) { 
  2.           System.out.print("\nEnter Product Color: ");
  3.           System.out.print("\nMust be Black, White, or Red: ");
  4.           color_flag = kbd_color.nextLine();
  5.          if(color_flag.equalsIgnoreCase("black")) {
  6.           // 
  7.          }
  8.          else if(color_flag.equalsIgnoreCase("red")) {
  9.         // 
  10.          }
  11.          else {
  12.          //wrong input
  13.          correct = false;
  14.         }
  15. }
  16.  
Mar 14 '07 #4
nomad
664 Expert 512MB
Thanks r035198x
this will work. I figure the rest out. I hope...

nomad
Mar 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by chahnaz.ourzikene | last post: by
7 posts views Thread by mx2k | last post: by
15 posts views Thread by JustSomeGuy | last post: by
reply views Thread by Paolo Tavalazzi | last post: by
7 posts views Thread by paolo.arimado | last post: by
10 posts views Thread by Putty | last post: by
2 posts views Thread by bitong | last post: by
Reika
5 posts views Thread by Reika | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.