Results 1 to 4 of 4

Thread: Scanner Trouble

  1. #1
    Snoman is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default Scanner Trouble

    *Assignment*

    I'm very sorry to bother you with this, as it's a school assignment, but I'm having a problem with it. I've tried multiple things and can't seem to get it to work. I was hoping that maybe you guys would be able to point me in the right direction.

    The program is supposed to act as a shopping list that takes input from the user then output a sort of receipt with the name of the item and cost. The problem I'm having is after the first time through it outputs:

    Would you like to add more items? (Y/N)
    y
    Enter item name:
    apple
    Enter item cost:
    5.00
    Would you like to add more items? (Y/N)
    y
    Enter item name:
    Enter item cost:


    Java Code:
    import java.util.*;
    
    
    
    class Shopping {
    
        ArrayList<Item> itemList = new ArrayList<Item>();
    
        Scanner scan = new Scanner(System.in);
    
            String name = "";
            double cost;
    
        void createItem () {
    
            System.out.println("Enter item name:  ");
            name = scan.nextLine();
    
            System.out.println("Enter item cost:  ");
            cost = scan.nextDouble();
    
            itemList.add(new Item(name, cost));
    
            }
    
        void printReceipt () {
    
            for (Item item : itemList) {
    
            System.out.print(item.itemName + "\t");
            System.out.printf("%.2f%n", item.itemCost);
    
            }
    
        }
    }
    
    class Item {
    
        public Item (String name, double cost) {
    
            itemName = name;
            itemCost = cost;
    
        }
    
        String itemName = "";
        double itemCost;
    
        public double getCost () {
            return itemCost;
        }
    
        public String getName () {
            return itemName;
        }
    
    }
    
    class Main {
        public static void main(String[] args) {
    
        Shopping list = new Shopping();
        Scanner scan = new Scanner(System.in);
    
        boolean moreItems = true;
    
        String userInput = "";
    
        while (moreItems == true) {
            System.out.println("Would you like to add more items? (Y/N)");
    
            userInput = scan.nextLine();
    
            if (userInput.equalsIgnoreCase("y")) {
    
                list.createItem();
    
            }
            else {
    
                moreItems = false;
    
            }
        }
    
        list.printReceipt();
    
        }
    }
    Any help would be appreciated.

    Thank you,
    Snoman
    Last edited by Snoman; 09-28-2010 at 03:30 AM.

  2. #2
    Snoman is offline Member
    Join Date
    Jul 2010
    Posts
    9
    Rep Power
    0

    Default

    I seem to have found the solution to my own problem, I'm sorry. The nextDouble does not go to the next line and the next time it comes around nextLine it already has a line in the buffer.

    Hope this helps anyone who is having the same problem.

    Does anyone know of a cleaner way around the nextDouble problem?

    Java Code:
        void createItem () {
    
            System.out.println("Enter item name:  ");
            name = scan.nextLine();
    
            System.out.println("Enter item cost:  ");
            cost = scan.nextDouble();
            scan.nextLine();
    
            itemList.add(new Item(name, cost));
    
            }
    Sorry,
    Snoman
    Last edited by Snoman; 09-28-2010 at 03:30 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Snoman View Post
    Does anyone know of a cleaner way around the nextDouble problem?
    Your solution looks clean and good to me.



    Sorry,
    Snoman
    Why sorry? :)

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    For answering his own question, apparently. Nothing to be sorry about, in my opinion anyway. It's a learning experience regardless of who answers it.

Similar Threads

  1. trouble with Scanner(new File("input"));
    By ronyosi in forum New To Java
    Replies: 9
    Last Post: 10-28-2010, 12:34 AM
  2. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 03:11 PM
  3. Replies: 0
    Last Post: 12-01-2008, 10:50 PM
  4. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 02:41 PM
  5. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 09:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •