0

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:

  1. name:afasdf

  2. extra info:afdsaf

  3. unit cost:123123214

  4. name: extra info: adflskjflk also, entering Stop isn't ending the loop either

  5. 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);
}
7
  • 1
    Use brackets ({ }) around your if and else bodies. For instance, the else statement at the bottom only executes s.SetNames(Name), and it always executes s.SetInfo(Details) and s.SetCost(Money). Finally, look into the break keyword, which will immediately exit its containing loop in its most basic form. if (Input ...) { breaker = true; } else { s.SetNames(Name); s.SetInfo(Details); s.SetCost(Money); } (use blank lines to separate in your code). Commented Jul 10, 2012 at 3:57
  • i added the brackets, but i am still getting the same problem Commented Jul 10, 2012 at 4:00
  • 1
    if(Input.equals("stop") || Input.equals("stop")) You take no chances I see. Commented Jul 10, 2012 at 4:04
  • my paranoia isn't working =.= Entering Stop still doesn't stop the while loop Commented Jul 10, 2012 at 4:07
  • 1
    Well in that case at least you're calling equals on Input itself, which is presumably a Scanner, not a String. Commented Jul 10, 2012 at 4:08

2 Answers 2

1

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.SetMoney would be s.setMoney. Also, variables follow the same rules where Money would be money, and ID would be id. If your teacher is teaching you otherwise, then follow their style.

The loop should also be a do-while loop:

do
{
    // read each value in sequence, and then check to see if you should stop
    // you can/should simplify this into a function that returns the object
    // that returns null if the value should stop (requiring a capital D
    // double for the return type)

    if ( /* reason to stop */)
    {
        break;
    }

    s.setNames(name);
    s.setId(id);
    s.setMoney(money);

} while (true);

private String getString(Scanner input)
{
    String result = input.nextLine();

    // look for STOP
    if (result.equalsIgnoreCase("stop"))
    {
        result = null;
    }

    return result;
}

private Double getDouble(Scanner input)
{
    Double result = null;
    // read the line is a string looking for STOP
    String line = getString(input);

    // null if it's STOP
    if (line != null)
    {
        try
        {
            result = Double.parseDouble(line);
        }
        catch (NumberFormatException e)
        {
            // not a valid number, but not STOP either!
        }
    }

    return result;
}

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 Money is a double, you must read the value as a String. I suspect that Input is a Scanner object, so you can check Input.hasNextDouble() if it's not, then you can conditionally check the String value 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

System.out.print("Unit Cost:\t");

if (Input.hasNextDouble())
{
    Money = Input.nextDouble();

    // you can now set your object
    // ...
}
// it's not a double; look for "stop"
else if (Input.nextLine().equalsIgnoreCase("stop"))
{
    // exit loop
    break;
}

// NOTE: if it's NOT a double or stop, then you have NOT exited
//       and you have not set money
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, now the program stops at Money when i put Stop, but the while loop stopped working ;d
Why are you checking for "stop" in getString()? It's a reusable method for reading input form the console.
@sreehari Because if I didn't check for it, then there would be no purpose of the method. You should just use input.nextLine() in its place if you have no need for a check in the method.
0
breaker = true;
while(breaker){
   Name = readInput("Name");
   Details = readInput("Details");
   Money = Double.parseDouble(readInput("Money"));

   if(Name.equals("stop") || Details.equals("stop"))
       breaker = false;
   else {
      // set ArrayItem
   }
}

private static String readInput(String title){
   System.out.println(title+":");
   //... read input
   // return value
}

2 Comments

You have a NumberFormatException in your future.
i tried this, but i got an error when i tried to put Stop into the double variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.