Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Scanner keeps breaking (Java question)

Name: Anonymous 2007-04-10 17:53 ID:afRMT8dH

See, it works like it should until the end of the loop, but it just prints "Add another item? (y/n)." and then terminates. Why isn't it prompting for input?

Yeah Java sucks, great, that doesn't change the fact that I'm enrolled in a Java class.

import java.util.Scanner;

public class Shopping
{
    public static void main (String[] args)
    {
    Scanner scan = new Scanner (System.in);
    ShoppingCart cart = new ShoppingCart();
    String another = "y";
    String item;
    double price;
    int quantity;

    while (another.equalsIgnoreCase("y"))
    {
    System.out.println ("What item are you adding to your cart?");
    item = scan.nextLine();

    System.out.println ("What is the unit cost of " + item + "?");
    price = scan.nextDouble();

    System.out.println ("What quantity of " + item + " are you buying?");
    quantity = scan.nextInt();

    cart.addToCart (item, price, quantity);
    System.out.print (cart);

    System.out.print ("\nAdd another item? (y/n).");
    another = scan.nextLine();
    }
    }
}

Name: Anonymous 2007-04-10 18:15 ID:CkKWBDWa

print the value of "another" the last thing you do in the while loop. Or use next() instead of nextLine().

Name: Anonymous 2007-04-10 18:18 ID:tiIzR965

Spend two minutes debugging, boy. What value does `another' have after the first iteration? does it equalsIgnoreCase("y")?

Name: Anonymous 2007-04-11 0:06 ID:nXAioA4G

I think you're having problems with lazy evaluation

Name: Anonymous 2009-01-14 13:27

YES

Name: Anonymous 2011-02-04 16:31

Name: Anonymous 2011-02-04 19:34

Name: Anonymous 2012-03-25 10:17

All work and no play makes Jack a dull boy

Name: Sgt.Kabuⶩ늕kiman陂 2012-05-29 0:15

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List