-1

My program is supposed to ask someone to enter a serial number and the price of an object.

Here is the class driver

import java.io.*;
import java.util.Scanner;

public class InventoryDriver {
    public static void main(String[] args)throws IOException {
        Inventory storeItem = new Inventory(" ", 1);
        PrintWriter storeFile = new PrintWriter("StoreInventory.txt");
        Scanner scan = new Scanner(System.in);
        boolean running = true;

        String itemNum = " ";
        double originalPrice = 1;

        while (running) {

            System.out.println("Enter the item number or enter 0 when you are done.");
            itemNum = scan.nextLine();

            storeItem.setItemNum(itemNum);

            storeFile.println(itemNum);

            if(itemNum.equals("0")) break;


            System.out.println("Enter the original price or enter 0 when you are done.");
            originalPrice = scan.nextDouble();

            storeItem.setOriginalPrice(originalPrice);

            storeFile.println(originalPrice);

            if(originalPrice == 0) break;
        }

        //create a scanner variable named myFile
        Scanner myFile = new Scanner(new File("StoreInventory.txt"));

        //Read my data from my file into the variablles
        //itemNum = myFile.nextLine();
        //originalPrice = myFile.nextDouble();

        printSaleData(myFile, storeItem);

        //Close the file
        myFile.close();
        storeFile.close();
    }
}

After working perfectly the first time with this output

    Enter the item number or enter 0 when you are done.
    input

    Enter the original price or enter 0 when you are done.
    input

But when the loop goes back, it prints this

    Enter the item number or enter 0 when you are done.
    input

    Enter the original price or enter 0 when you are done.
    input

    Enter the item number or enter 0 when you are done.
    Enter the original price or enter 0 when you are done.
    input

What about my code changes the loop after the first go?
P.S. Even after I break the loop the whole program stops, and won't call the "printSaleData" sub-module to the main module.

jbetty56
  • 3
  • 1
  • 1
    Can you please clarify exactly what you gave as input to get these results? What exactly is the output you are looking for? – Keara Dec 03 '17 at 20:34
  • Maybe change `itemNum = scan.nextLine();` to `itemNum = scan.nextInt();` ? – icarumbas Dec 03 '17 at 20:34

1 Answers1

0

Update scan.nextLine to scan.next(). This will do it.

Barr3l3y3
  • 41
  • 3