I Need Some Help After I Enter the Data The Data Is Suppose To Save To The item.
ID: 3673793 • Letter: I
Question
I Need Some Help After I Enter the Data The Data Is Suppose To Save To The item.txt But For Some Reason It is Not Saving.
import java.io.*;
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
//local constants
//local variables
String fileName = "items.txt";
Scanner scanner = null;
ItemsList itemsList = new ItemsList(5);
int i = 0;
int choice = 0;
String itemName;
String name;
double price;
int qty;
String cho = "y";
Scanner scan = new Scanner(System.in);
//****************************************************************************
do
{
//Ask the user the following
System.out.println("Menu");
System.out.println("1. Add item");
System.out.println("2. Display items");
System.out.println("3. Exit");
System.out.println("Enter your choice");
choice = scan.nextInt();
switch(choice)
{
//Add an item to the itemsList
case 1:
//Ask user for to enter information
System.out.println(" Enter item name : ");
name=scan.next();
System.out.println(" Enter price : ");
price = scan.nextDouble();
System.out.println(" Enter quantity : ");
qty= scan.nextInt();
//Add the OneItem to the itemsList
itemsList.addItem(new OneItem(name, price, qty));
break;
case 2:
//print the list
//print heading with specific formatter
System.out.printf("%-10s%-10s%-10s ", "Item","Price","Quantity");
System.out.println(itemsList.toString());
break;
case 3:
//Terminate the program
System.out.println("Terminate the program.");
break;
default:
System.out.println("Incorrect option is selected.");
break;
}
//Ask user if they would like to continue
System.out.print(" Would to like to continue <(Y or N)> : ");
cho = scan.next();
System.out.println();
writeToFile(itemsList);
}
while(cho.equalsIgnoreCase("Y"));
//open the file and catch the exception if file not found
try
{
//create an instance of scanner
scan = new Scanner(new File(fileName));
//read file items until end of file
while(scan.hasNext())
{
itemName = scan.next();
price = scan.nextDouble();
qty=scan.nextInt();
//Add the OneItem object to the itemList
itemsList.addItem(new OneItem(itemName, price, qty));
i++;
}
//close the file object
scan.close();
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
private static void writeToFile(ItemsList itemsList)
{
//Create a file name called items.txt
String filename="items.txt";
//Create a variable of Class PrintWriter
PrintWriter filewriter=null;
try
{
//create an instance of PrintWriter
filewriter=new PrintWriter(new File(filename));
//close the file writer
filewriter.close();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}//end Driver class
====================================
public class ItemsList
{
//declare variables
private OneItem items[];
private int size;
private int count;
//constructor to set items, size and count to zero
public ItemsList()
{
items = null;
size = 0;
count = 0;
}
//Parameter constructor
public ItemsList(int size)
{
items = new OneItem[size];
for (int i = 0; i < items.length; i++)
{
items[i] = new OneItem();
}
this.size = size;
count = 0;
}
//Add OneItem to the itemlist
public void addItem(OneItem item)
{
if(items.length == count)
{
resize();
items[count] = item;
count++;
}
else
{
items[count] = item;
count++;
}
}
//Resize
private void resize()
{
int oldsize = size;
count = oldsize;
int newsize = 2 * this.size;
size = newsize;
OneItem[] tempList = new OneItem[size];
for (int i = 0; i < oldsize; i++)
tempList[i] = items[i];
items = new OneItem[size];
items = tempList;
}
//getSize
public int getSize()
{
return count;
}
//toString
public String toString()
{
String description = "";
for (int i = 0; i <count; i++)
{
description += items[i].toString();
}
return description;
}
}
===============================
public class OneItem
{
//declare a variables
private String name;
private double price;
private int quantity;
//default constructor
public OneItem()
{
name = "";
price = 0;
quantity = 0;
}
//parameter constructor
public OneItem(String name, double price, int quantity)
{
this.name = name;
this.price = price;
this.quantity = quantity;
}
//toString
public String toString()
{
return String.format("%-10s%-10.2f%-10d ", name,price,quantity);
}
}//end of the OneItem class
Explanation / Answer
// I Need Some Help After I Enter the Data The Data Is Suppose To Save To The item.txt But For Some Reason It is Not Saving.
import java.awt.event.ItemListener;
import java.io.*;
import java.util.Iterator;
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
// local constants
// local variables
String fileName = "items.txt";
Scanner scanner = null;
ItemsList itemsList = new ItemsList(5);
int i = 0;
int choice = 0;
String itemName;
String name;
double price;
int qty;
String cho = "y";
Scanner scan = new Scanner(System.in);
// ****************************************************************************
do {
// Ask the user the following
System.out.println("Menu");
System.out.println("1. Add item");
System.out.println("2. Display items");
System.out.println("3. Exit");
System.out.print("Enter your choice:");
choice = scan.nextInt();
switch (choice) {
// Add an item to the itemsList
case 1:
// Ask user for to enter information
System.out.print(" Enter item name : ");
name = scan.next();
System.out.print(" Enter price : ");
price = scan.nextDouble();
System.out.print(" Enter quantity : ");
qty = scan.nextInt();
// Add the OneItem to the itemsList
itemsList.addItem(new OneItem(name, price, qty));
break;
case 2:
// print the list
// print heading with specific formatter
System.out.printf("%-10s%-10s%-10s ", "Item", "Price",
"Quantity");
System.out.println(itemsList.toString());
break;
case 3:
// Terminate the program
System.out.println("Terminate the program.");
break;
default:
System.out.println("Incorrect option is selected.");
break;
}
// Ask user if they would like to continue
System.out.print(" Would to like to continue <(Y or N)> : ");
cho = scan.next();
System.out.println();
writeToFile(itemsList);
} while (cho.equalsIgnoreCase("Y"));
// open the file and catch the exception if file not found
try {
// create an instance of scanner
scan = new Scanner(new File(fileName));
// read file items until end of file
while (scan.hasNext()) {
itemName = scan.next();
price = scan.nextDouble();
qty = scan.nextInt();
// Add the OneItem object to the itemList
itemsList.addItem(new OneItem(itemName, price, qty));
i++;
}
// close the file object
scan.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
private static void writeToFile(ItemsList itemsList) {
// Create a file name called items.txt
String filename = "items.txt";
// Create a variable of Class PrintWriter
PrintWriter filewriter = null;
try {
// create an instance of PrintWriter
File file = new File(filename);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
OneItem items[] = itemsList.getItems();
for (int i = 0; i < itemsList.getCount(); i++) {
bw.write(items[i].toString());
}
bw.close();
// close the file writer
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}// end Driver class
class ItemsList {
// declare variables
private OneItem items[];
private int size;
private int count;
// constructor to set items, size and count to zero
public ItemsList() {
items = null;
size = 0;
count = 0;
}
public OneItem[] getItems() {
return items;
}
public int getCount() {
return count;
}
// Parameter constructor
public ItemsList(int size) {
items = new OneItem[size];
for (int i = 0; i < items.length; i++) {
items[i] = new OneItem();
}
this.size = size;
count = 0;
}
// Add OneItem to the itemlist
public void addItem(OneItem item) {
if (items.length == count) {
resize();
items[count] = item;
count++;
} else {
items[count] = item;
count++;
}
}
// Resize
private void resize() {
int oldsize = size;
count = oldsize;
int newsize = 2 * this.size;
size = newsize;
OneItem[] tempList = new OneItem[size];
for (int i = 0; i < oldsize; i++)
tempList[i] = items[i];
items = new OneItem[size];
items = tempList;
}
// getSize
public int getSize() {
return count;
}
// toString
public String toString() {
String description = "";
for (int i = 0; i < count; i++) {
description += items[i].toString();
}
return description;
}
}
class OneItem {
// declare a variables
private String name;
private double price;
private int quantity;
// default constructor
public OneItem() {
name = "";
price = 0;
quantity = 0;
}
// parameter constructor
public OneItem(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}
// toString
public String toString() {
return String.format("%-10s%-10.2f%-10d ", name, price, quantity);
}
}// end of the OneItem class
OUTPUT:
items.txt
item1 52.00 5
Item2 87.00 6
Console :
Menu
1. Add item
2. Display items
3. Exit
Enter your choice:1
Enter item name : item1
Enter price : 52
Enter quantity : 5
Would to like to continue <(Y or N)> : y
Menu
1. Add item
2. Display items
3. Exit
Enter your choice:2
Item Price Quantity
item1 52.00 5
Would to like to continue <(Y or N)> : y
Menu
1. Add item
2. Display items
3. Exit
Enter your choice:1
Enter item name : Item2
Enter price : 87
Enter quantity : 6
Would to like to continue <(Y or N)> : y
Menu
1. Add item
2. Display items
3. Exit
Enter your choice:2
Item Price Quantity
item1 52.00 5
Item2 87.00 6
Would to like to continue <(Y or N)> : n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.