so for my assignment I have to create an input file consisting of a grocery order, then create an output file that prints the input file into a table with columns. While also calculating the total price of each item by multiplying the quantity by the unit price, then adding up all the total prices together. My problem is that I'm stuck on how im suppose to make a formatted table that evenly distributes the data from the input file. As well as how im suppose to incorporate the multiple mixed datas for my while loop.
I tried using hasnext()
, followed by nextline()
,nextInt()
, and nextdouble()
to read my data but had no success. Here's my code for reference.
import java.util.Scanner;
import java.io.*;
public class Assignment4 {
public static void main(String[] args) throws Exception {
PrintWriter input = new PrintWriter("groceryorder.txt");`
String food = "Oranges";
input.println(food);
int quantity = 30;
input.println(quantity);
double unitprice = 1.47;
input.println(unitprice);
food = "Cheese Block";
input.println(food);
quantity = 5;
input.println(quantity);
unitprice = 3.25;
input.println(unitprice);
food = "Crackers";
input.println(food);
quantity = 1;
input.println(quantity);
unitprice = 5.25;
input.println(unitprice);
food = "Grapes";
input.println(food);
quantity = 1;
input.println(quantity);
unitprice = 4.35;
input.println(unitprice);
food ="Onions";
input.println(food);
quantity = 10;
input.println(quantity);
unitprice = 1.25;
input.println(unitprice);
food = "Oatmeal";
input.println(food);
quantity = 2;
input.println(quantity);
unitprice = 5.67;
input.println(unitprice);
input.close();
File infile = new File("groceryorder.txt");
Scanner scanfile = new Scanner(infile);
PrintWriter output = new PrintWriter("groceryoutput.txt");
System.out.print("Item " + " Quantity " + " Unit Price" + "Total Price");
output.print("Item " + " Quantity " + " Unit Price" + "Total Price");
while (scanfile.hasNext()){
food = scanfile.nextLine();
quantity = scanfile.nextInt();
unitprice = scanfile.nextDouble();
double totalprice = (quantity * unitprice);
totalprice = scanfile.nextDouble();
}
output.print(food + quantity + unitprice + totalprice);
output.close();
}
}