473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1617
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

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

Similar topics

15
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
7
by: mx2k | last post by:
Hello @ all, we have written a small program (code below) for our own in-developement rpg system, which is getting values for 4 RPG-Characters and doing some calculations with it. now we're...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
0
by: Paolo Tavalazzi | last post by:
I have a problem on FROM subselect that i don't understand. I do two query different only for a WHERE clause in a FROM subquery . 1) explain analyze SELECT DISTINCT ON...
7
by: paolo.arimado | last post by:
Dear everyone, Can someone please help me on my HW. I'm trying to write a program that will display the following: * *** ***** ******* *********
10
by: Putty | last post by:
In C and C++ and Java, the 'for' statement is a shortcut to make very concise loops. In python, 'for' iterates over elements in a sequence. Is there a way to do this in python that's more concise...
2
by: bitong | last post by:
I'm a little bit confuse with regard to our subject in C..We are now with the Loops..and I was just wondering if given a problem, can you use Do-while loops instead of a for loops or vise versa? are...
5
Reika
by: Reika | last post by:
Hello, I need help understanding some things in programming. I'm taking a high school level programming course, however my teacher is less than helpful in explaining or even understanding the...
1
by: massdeletion101 | last post by:
I'm using Bloodshed Dev-Cpp version 4.9.9.2 (version five beta) as my compiler and IDE, and I am trying to compute pi truncated to the first 15 decimal places, only there is a problem. I am using a...
8
by: Dameon99 | last post by:
my program compiles without problems but when i try to run it pauses shortly and then crashes. When i set it to debug it came up with the following message: "An Access Violation (Segmentation...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.