Question: I am trying to get this code to read a CSV file instead of a normal txt file. I believe I implemented the code to

I am trying to get this code to read a CSV file instead of a normal txt file. I believe I implemented the code to read the CSV file wrong. Any help would be great, as well as the code being fixed.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
// Item class
public class HW4{
// Arrays to store data
public int[] itemCode;
public String[] itemName;
public float[] unitPrice;
// Item constructor
HW4(){
String[] data = new String[0];
// Reading file
data = this.readFile(data);
// Getting data
itemCode = new int[data.length];
itemName = new String[data.length];
unitPrice = new float[data.length];
for(int i =0;i data.length;i++){
String[] dataDivided = data[i].split(",");
itemCode[i]= Integer.parseInt(dataDivided[0]);
itemName[i]= dataDivided[1];
unitPrice[i]= Float.parseFloat(dataDivided[2]);
}
}
// Reading files from the source provided
List> records = new ArrayList>();
try (BufferedReader br = new BufferedReader(new FileReader("book.csv"))){
String line;
while ((line = br.readLine())!= null){
String[] values = line.split(COMMA_DELIMITER);
records.add(Arrays.asList(values));
}
}
// GetName function
public String getName(int code){
return itemName[code-1];
}
// GetPrice function
public double getPrice(int code){
return unitPrice[code -1];
}
}
// Driver's Class
public class OnlineSale {
public static void main(String[] args){
// creating scanner class object
Scanner sc = new Scanner(System.in);
// Variables to store data
String[] data = new String[100];
int count =0;
int code;
double totalDataCode =0;
double sales =0;
System.out.println("Beginning a new sale (Y/N)");
String ans = sc.next();
while(ans.toLowerCase(Locale.ROOT).equals("y")){
if (ans.toLowerCase(Locale.ROOT).equals("y")){
do {
HW4 n1= new HW4();
System.out.print("Enter product code: ");
String value1= sc.next();
// Checking whether input is correct or not
while(value1.charAt(0)48|| value1.charAt(0)>57){
System.out.println("!!!Invalid product code");
System.out.print("Enter product code: ");
value1= sc.next();
}
code = Integer.parseInt(value1);
if (code ==-1){
continue;
}
System.out.println(" item name: "+ n1.getName(code));
String itemName = n1.getName(code);
double price = n1.getPrice(code);
System.out.print("Enter quantity: ");
value1= sc.next();
while(value1.charAt(0)48|| value1.charAt(0)>57){
System.out.println("!!!Invalid quantity :");
System.out.print("Enter quantity code: ");
value1= sc.next();
}
int quantity = Integer.parseInt(value1);
double total = quantity * price;
System.out.println("Item Total : $ "+ total);
totalDataCode = totalDataCode + total;
data[count]= quantity +""+ itemName +" $"+ String.format("%.2f", total);
count++;
} while (code !=-1);
System.out.println("---------------------------");
System.out.println("Item List :");
for (String datum : data){
if(datum!=null){
System.out.println(datum);
}
}
System.out.println("SubTotal $"+ totalDataCode);
double totalWithTax = totalDataCode + totalDataCode *6/100;
System.out.println("Total with tax "+ String.format("%.2f", totalWithTax));
System.out.print("Tendered Amount : $ ");
double amountPay = Double.parseDouble(sc.next());
while (true){
if (totalWithTax - amountPay >0){
System.out.println("Enter tendered amount again.");
I am trying to get this code to read a CSV file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!