I’m trying to input 3 different variables into an array inside a while loop, as long as i don’t enter stop for any of the variables. the while loop is only suppose to let me input a second variable value if the 1st variable isn’t stop, and likewise with inputting a third variable value
Right now, the first loop goes fine and i can input all 3 variables, but the 2nd and 3rd time, the for loop outputs the first variable, but doesn’t allow me to input a value before skipping to the 2nd variable.
ex of what i mean:
-
name:afasdf
-
extra info:afdsaf
-
unit cost:123123214
-
name: extra info: adflskjflk
also, entering Stop isn’t ending the loop either -
unit cost:123217
i know that this loop works when there’s only one variable, and i’ve tried using a for loop instead of a while loop, and adding tons and tons of else statements, but it seems to stay the same
is there something wrong with the way i set up my breakers?
is the way i set up the last breaker(the one that stops even when i put stop for a double variable) messing up the rest of hte loop?
thank you so much
here is my code
ArrayItem s = new ArrayItem();
String Name = null, ID = null;
double Money = 0;
boolean breaker = false;
while(breaker ==false)
{
System.out.print("Name:" + "\t");
Name = Input.nextLine();
if(Name.equals("Stop")) //see if the program should stop
breaker = true;
System.out.print("Extra Info:" + "\t");
Details = Input.nextLine();
if(ID.equals("Stop"))
breaker = true;
System.out.print("Unit Cost:" + "\t");
Money = Input.nextDouble();
// suppose to let me stop even if i input stop
// when the variable is suppose to be a double
if(Input.equals("stop") || Input.equals("stop"))
breaker = true;
else
s.SetNames(Name);
s.SetInfo(Details);
s.SetCost(Money);
}
A couple of things about the code:
"Name:" + "\t"can be simplified ot"Name:\t". This is true for the rest of the code. In Java, it’s customary to use camelcase where the first word is lowercase. For example,s.SetMoneywould bes.setMoney. Also, variables follow the same rules whereMoneywould bemoney, andIDwould beid. If your teacher is teaching you otherwise, then follow their style.The loop should also be a do-while loop:
There are a lot of concepts in there, but they should help as you progress. I’ll let you put the pieces together.
Also, you did need to fix the brackets, but that’s not the only issue. Because
Moneyis adouble, you must read the value as aString. I suspect thatInputis aScannerobject, so you can checkInput.hasNextDouble()if it’s not, then you can conditionally check theStringvalue to see if it’s “stop” (note: you are checking for “Stop” and “stop”, which are not equal). Your last, no-chances check compares the Scanner to “stop”, which will never be true. Check