Code is asking the user to enter itemName, price, and quantity. I see it creates a file but no string inside? Is there something wrong with the buffer? Thanks in advance,
userInput: Enter item name: item1 Enter item price $14 Enter item quantity: 3 Do you want another item? Yes(y) or No(n)n
code:
while(userInput.charAt(0)!='n'){
System.out.print("Enter item name: ");
itemName = kybd.nextLine();
PrintWriter out0 = new PrintWriter(new BufferedWriter(new FileWriter("registryCopy.txt", true)));
System.out.print("Enter item price $");
price = kybd.nextLine();
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("registryCopy.txt", true)));
System.out.print("Enter item quantity: ");
quantity = kybd.nextLine();
PrintWriter out2 = new PrintWriter(new BufferedWriter(new FileWriter("registryCopy.txt", true)));
System.out.print("Do you want another item? Yes(y) or No(n)");
userInput= kybd.next();
}//end of whileLoop
after editing still the same - no string inside the file:
while(userInput.charAt(0)!='n'){
PrintWriter out0 = new PrintWriter(new BufferedWriter(new FileWriter("registryCopy.txt", true)));
System.out.print("Enter item name: ");
itemName = kybd.nextLine();
out0.println(itemName);
update2: ok so it was the flush at the end out0.flush(); my output is: item1 //nextline, price // nextline, quantity // nextLine... how would I make it stay on the same line so I could later do some manipulations with the hasNext?
Update3: Getting indexOutOfBound. After sucessfull file save. I open the file to perform the following tasks: find the total costPerItem, totalCount and sum. Don't know why I'm getting out of bound. Error points out to totalCost =+ Double.parseDouble(index[1].trim()); Source code:
Scanner data = new Scanner(new File("registryCopy.txt"));
while(data.hasNext()){
String line = data.nextLine();
System.out.println(line);
String[] index = line.split("\\t");
totalCost += Double.parseDouble(index[1].trim());
totalCount += Integer.parseInt(index[2].trim());
sum = totalCost/totalCount;
}