J. Hill
J. Hill

Reputation: 21

Data entered is not writing to file

I am attempting to create and write to a .txt file so that another program can open and read it. The problem is that the entered data is not being written to the file created. It is a blank .txt document.

import java.util.Scanner;
import java. io.*; //import class for file input.
public class inventoryStock
{
  public static void main(String args[]) throws Exception
  {
    //Declarations
    String[] itemName = new String [10];
    double[] itemCost = new double [10];
    double[] inStockNumber = new double [10];
    int counter = 0;
    //End declarations

    Scanner input = new Scanner (System.in);
     //Open output file.
    FileWriter fw = new FileWriter("updatedStock.txt");
    PrintWriter pw = new PrintWriter(fw);


      do
      {
        System.out.print("Enter item name");
        pw.println();
        itemName[counter] = input.next();
        System.out.print("Enter item cost");
        pw.println();
        itemCost[counter] = input.nextDouble();
        System.out.print("Enter Number in stock");
        pw.println();
        inStockNumber[counter] = input.nextDouble();

        counter += 1;
      }while(counter<10);
        pw.flush();

    pw.close();
    System.exit(0);
  } //End of main method
} //End of InventoryStock class.

Upvotes: 2

Views: 61

Answers (2)

Eugene
Eugene

Reputation: 11085

It seems that you didn't really write what you want to file. You can try the code below.

pw.println(itemName[counter] + ", " + itemCost[counter] + ", " + inStockNumber[counter]);

Two recommendations to you.

  1. Since the size 10 is everywhere in your code. You'd better extract it to a single variable for better maintainability.
  2. Please follow the naming convention of java. For your case, the first letter of class name should be capitalized. Use InventoryStock instead of inventoryStock.

The entire code is like below, hope it will help. Thx.

import java.util.Scanner;
import java.io.*; //import class for file input.

public class InventoryStock {
    public static void main(String args[]) throws Exception {

        int size = 10;
        // Declarations
        String[] itemName = new String[size];
        double[] itemCost = new double[size];
        double[] inStockNumber = new double[size];
        int counter = 0;
        // End declarations

        Scanner input = new Scanner(System.in);
        // Open output file.
        FileWriter fw = new FileWriter("updatedStock.txt");
        PrintWriter pw = new PrintWriter(fw);

        {
            do {
                System.out.print("Enter item name");
                itemName[counter] = input.next();
                System.out.print("Enter item cost");
                itemCost[counter] = input.nextDouble();
                System.out.print("Enter Number in stock");
                inStockNumber[counter] = input.nextDouble();

                pw.println(itemName[counter] + ", " + itemCost[counter] + ", " + inStockNumber[counter]);

                counter += 1;
            } while (counter < size);
            fw.flush();
        }
        fw.close();
        System.exit(0);
    } // End of main method
} // End of InventoryStock class.

Upvotes: 1

Bill
Bill

Reputation: 4646

You'll have to actually tell PrintWriter to write to file or else it won't do anything even though you grab the user's input with input.next(). Try something like this:

Scanner input = new Scanner (System.in);
//Open output file.
FileWriter fw = new FileWriter("updatedStock.txt");
PrintWriter pw = new PrintWriter(fw, true);

do
{
    System.out.print("Enter item name");    
    itemName[counter] = input.next();
    pw.write(itemName[counter]);
    pw.println();

    System.out.print("Enter item cost");
    itemCost[counter] = input.nextDouble();
    pw.write(String.valueOf(itemCost[counter]));
    pw.println();

    System.out.print("Enter Number in stock");
    inStockNumber[counter] = input.nextDouble();
    pw.write(String.valueOf(inStockNumber[counter]));
    pw.println();

    counter += 1;
}while(counter<10);

pw.flush();
pw.close();
System.exit(0);

Upvotes: 0

Related Questions