Question
JAVA CODE READING CSV FILE INTO ARRAY AND DISPLAYING IN PROVIDED FORMAT Hello! I was trying to build a java code that reads in a
JAVA CODE READING CSV FILE INTO ARRAY AND DISPLAYING IN PROVIDED FORMAT
Hello! I was trying to build a java code that reads in a csv file into an array and displays them like below:
Cruise Ship Ship Name: Aegean Spirit Year Built: 2003 Max Passengers: 4,500 Standard Ship
Ship Name: Classica
Year Built: 1990 Cargo Ship Ship Name: Edith Maersk Year Built: 2007 Cargo Capacity: 170,794 Cargo Ship Ship Name: Emma Maersk Year Built: 2006 Cargo Capacity: 156,907 Cruise Ship Ship Name: Enchanted Capri Year Built: 1970 Max Passengers: 7,000 Cargo Ship Ship Name: Estelle Maersk Year Built: 2006 Cargo Capacity: 151,687 Cruise Ship Ship Name: Explorer of the Seas Year Built: 2001 Max Passengers: 9,500 Standard Ship Ship Name: MV Delight Year Built: 1985 Cargo Ship Ship Name: Ocean Glory Year Built: 2006 Cargo Capacity: 161,788 Cruise Ship Ship Name: Our Lady Year Built: 1960 Max Passengers: 3,500 Standard Ship Ship Name: Provence Year Built: 1995 Standard Ship Ship Name: SS Desabla Year Built: 1913 Cruise Ship Ship Name: Symphony Year Built: 2001 Max Passengers: 10,000
I've gone as far as
Scanner inputFile = new Scanner (new File ("Ships.csv"));
while (inputFile.hasNext()) { String line = inputFile.next(); String [] cells = line.split(","); for (String cell: cells) { //System.out.println(Integer.parseInt(cell)); StringTokenizer st = new StringTokenizer (line, ","); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } } inputFile.close(); }
So, the question is how do I get the tokens in the format while passing in those tokens to their appropriate class constructors and toString() method.
Help is greatly appreciated! I can explain more in detail if need be.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started