So I am writing 2 programs the first takes inventory name, price, and stock for
ID: 3764143 • Letter: S
Question
So I am writing 2 programs the first takes inventory name, price, and stock for 10 items and creates and saves a file with the information. The second program will output the contents of the file to the user's screen. The output will include appropriate labels for all of the data. I will output data using a loop and I will identify the file to be accessed using a graphic dialog box. The two programs will be independent. By that, I mean that one program will not reference the other using objects or classes.
I can't seem to find the correct path of the file i've created with the first program and am unable to move forward with the second. I was hoping for someone to take a look and tell me if i've done something wrong with the saving of my file...
First Program:
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
public class InventoryList {
public static void main(String args[]) throws IOException {
String itemPriceString;
String itemStockString;
final int NUM_ITEMS = 10;
String itemName[] = new String[NUM_ITEMS];
double itemStock[] = new double[NUM_ITEMS];
double itemPrice[] = new double[NUM_ITEMS];
FileWriter saveFile = new FileWriter("InventoryItems.txt");
PrintWriter pw = new PrintWriter(saveFile);
// accept the item name, and cost price
// cost price to double and then to final price
// Store data into a file
for (int loopIndex = 0; loopIndex < NUM_ITEMS; loopIndex++) {
itemName[loopIndex] = JOptionPane
.showInputDialog(" This program will accept item name, cost, and inventory. Enter item name: ");
itemPriceString = JOptionPane
.showInputDialog(" Enter the price of item: ");
itemPrice[loopIndex] = Double.parseDouble(itemPriceString);
itemStockString = JOptionPane
.showInputDialog(" Enter number of items in stock: ");
itemStock[loopIndex] = Double.parseDouble(itemStockString);
}
// exit program
for (int loopIndex = 0; loopIndex < NUM_ITEMS; loopIndex++) {
pw.println(itemName[loopIndex] + " $" + itemPrice[loopIndex]
+ " $" + itemStock[loopIndex]);
saveFile.close();
pw.close();
}
System.out.println("Thank you for creating a new file");
}
}
what I have so far for the second Program:
import java.io.*;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadInventoryList
{
public static void main(String args[]) throws Exception
{
String itemName;
double itemPrice, itemStock;
String fileName;
// Get user input for file selection
fileName = JOptionPane.showInputDialog("Please enter the name of the file you would like to display.");
//open input file.
FileReader fr = new FileReader (fileName);
BufferedReader br = new BufferedReader (fr);
System.exit(0);
}
}
Explanation / Answer
Program 1:
To Read input into a file:
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JOptionPane;
public class InventoryList {
public static void main(String args[]) throws IOException {
String itemPriceString;
String itemStockString;
final int NUM_ITEMS = 3;//changed the number of items to 3
String itemName[] = new String[NUM_ITEMS];
double itemStock[] = new double[NUM_ITEMS];
double itemPrice[] = new double[NUM_ITEMS];
FileWriter saveFile = new FileWriter("InventoryItems.txt");
PrintWriter pw = new PrintWriter(saveFile);
// accept the item name, and cost price
// cost price to double and then to final price
// Store data into a file
for (int loopIndex = 0; loopIndex < NUM_ITEMS; loopIndex++) {
itemName[loopIndex] = JOptionPane.showInputDialog(" This program will accept item name, cost, and inventory. Enter item name: ");
itemPriceString = JOptionPane.showInputDialog(" Enter the price of item: ");
itemPrice[loopIndex] = Double.parseDouble(itemPriceString);
itemStockString = JOptionPane.showInputDialog(" Enter number of items in stock: ");
itemStock[loopIndex] = Double.parseDouble(itemStockString);
}
// exit program
for (int loopIndex = 0; loopIndex < NUM_ITEMS; loopIndex++) {
pw.println(itemName[loopIndex] + " $" + itemPrice[loopIndex]
+ " $" + itemStock[loopIndex]);
}//the for loop should end here, otherwise only the first record will be stored in the file InventoryItems.txt
saveFile.close();
pw.close();
System.out.println("Thank you for creating a new file");
}
}
Sample output:
InventoryItems.txt
Program 2
To output the file contents on to console:
import java.io.*;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.FileReader;
public class ReadInventoryList
{
public static void main(String args[]) throws Exception
{
//String itemName;
//double itemPrice, itemStock;
String fileName;
// Get user input for file selection
fileName = JOptionPane.showInputDialog("Please enter the name of the file you would like to display.");
//open input file.
FileReader fr = new FileReader (fileName);
BufferedReader br = new BufferedReader (fr);
String text;
//read each line in the InventoryItems.txt file using readLine()
while ((text=br.readLine())!= null) {
System.out.println(text);
}
fr.close();
System.exit(0);
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.