
Hi Friend, I have a file class it lets me extract all the data from a csv file but I need to align the data properely, seperating into the proper rows and columns, as I said my code only allows me to extract all data but I need to use it.
My Code:
import java.util.Scanner; import java.io.File; import java.util.ArrayList; import java.io.FileNotFoundException; //import java.util.*;
public class ReadCSV{ public ReadCSV (){ } public static void ReadCSV(){ File csvFile = new File("Hotels.csv"); if(!csvFile.exists() ) { System.out.println("CSV file does not exist"); System.exit(1); }else { System.out.println("Found File"); } Scanner in = new Scanner(System.in);
try{
Scanner csvReader = new Scanner(csvFile);
csvReader.nextLine();
csvReader.nextLine();
/*String [] rows = new String[10];
String [][] cols = new String [10][12];
for(int i = 0; i<rows.length;rows++){
rows[i] = ("5 star");
for(i = 0; i<rows.length;rows++){
rows.split(",");
}
}*/
String [] x = new String[12];
String line="";
while(csvReader.hasNext()){
line = csvReader.nextLine();
// csvReader.useDelimeter("[,]");
x = line.split(",") ;
// Integer.parseInt(x[6]) ; converting string to int
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.