I was wondering how you can read a excel file such as ("delayed.csv ") in java. I believe that I have to use java.io.file, but there are 10 columns of data to read from. How can I pick apart that information and use the information that I need?
Thanks
15 Replies - 332 Views - Last Post: 21 September 2011 - 08:34 PM
Topic Sponsor:
Replies To: Reading file from Excel
#2
Re: Reading file from Excel
Posted 21 September 2011 - 12:21 PM
You can use the String split() method to break apart each line into a String[], one cell per element.
#3
Re: Reading file from Excel
Posted 21 September 2011 - 12:36 PM
The String split() method, as macosxnerd101 said. CSV files are just text files. Columns are separated by comma's, rows separated by newlines.
#4
Re: Reading file from Excel
Posted 21 September 2011 - 12:42 PM
Viske, on 21 September 2011 - 12:36 PM, said:
The String split() method, as macosxnerd101 said. CSV files are just text files. Columns are separated by comma's, rows separated by newlines.
How does it know which data is which in the excel file? Lets say I wanted to only grab some information from the file and not all of it?
#5
Re: Reading file from Excel
Posted 21 September 2011 - 12:45 PM
It doesn't, you have to read the whole file and then take the data you want.
#6
Re: Reading file from Excel
Posted 21 September 2011 - 12:46 PM
You can read the File line-by-line, and split() apart each line. It's up to you to parse out the tokens, and ignore the ones you don't want.
The data is there. Java "knows" it's there because you are reading from that file.
Quote
How does it know which data is which in the excel file?
The data is there. Java "knows" it's there because you are reading from that file.
#7
Re: Reading file from Excel
Posted 21 September 2011 - 12:46 PM
import java.util.Scanner;
public class delayed
{
public static void main(String args[])
{
Scanner myscan= new Scanner (System.in);
java.io.File file = ("delayed.csv");
Scanner s = new Scanner(input).useDelimiter(",");
Would I have to use a delimter to read certain columns? such as this?
}
}
#8
Re: Reading file from Excel
Posted 21 September 2011 - 12:51 PM
That's one way to go about it. I would encourage reading line-by-line, then using the String split() method to break them apart. It's a little cleaner that way.
#9
Re: Reading file from Excel
Posted 21 September 2011 - 01:45 PM
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class delayed
{
public static void main(String[] args) {
File file = new File("delayed.csv");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
I changed my code and it compiles this way, but how would I use the string split in order to read each column that I specify? Lets say I want to read only columns 1,2,3,4 but there are more that are useless to me?
#10
Re: Reading file from Excel
Posted 21 September 2011 - 01:48 PM
Then you would assign the whole line (row) to an array, and just read the first 4 elements.
// Pseudo-Code. The whole row will now be in array.
String[] array = line.split(",");
//Access the first 4 elements as usual..
#11
Re: Reading file from Excel
Posted 21 September 2011 - 02:16 PM
Viske, on 21 September 2011 - 01:48 PM, said:
Then you would assign the whole line (row) to an array, and just read the first 4 elements.
// Pseudo-Code. The whole row will now be in array.
String[] array = line.split(",");
//Access the first 4 elements as usual..
Where would this code go in to fit? How can it bring up the data?
#13
Re: Reading file from Excel
Posted 21 September 2011 - 03:48 PM
macosxnerd101, on 21 September 2011 - 02:20 PM, said:
Are you familiar on how to work with arrays?
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class hw1
{
public static void main (String[] args) throws Exception
{
int i;
int p=0;
String carrier;
java.io.File file = new File ("delayed.csv");
Scanner scan = new Scanner(file).useDelimiter(",");
String Airline[];
Airline = new String[14];
Airline[0]= new String ("AirTran");
Airline[1]= new String ("American");
Airline[2]= new String ("American Eagle");
Airline[3]= new String ("Atlantic Southeast");
Airline[4]= new String ("Continental");
Airline[5]= new String ("Delta");
Airline[6]= new String ("ExpressJet");
Airline[7]= new String ("Frontier");
Airline[8]= new String ("JetBlue");
Airline[9]= new String ("Mesa");
Airline[10]= new String ("SkyWest");
Airline[11]= new String ("Southwest");
Airline[12]= new String ("United");
Airline[13]= new String ("US Airways");
while(p==0)
{
scan.next();
carrier=scan.next();
}
}
}
Is this the correct way to create an array?
#14
Re: Reading file from Excel
Posted 21 September 2011 - 04:17 PM
Airline[0] = "AirTran"; is sufficient.
By convention, classes are named with capital letters (Hw1), fields and methods begin with lower-case letters (e.g. airline[]).
By convention, classes are named with capital letters (Hw1), fields and methods begin with lower-case letters (e.g. airline[]).
#15
Re: Reading file from Excel
Posted 21 September 2011 - 06:22 PM
It's actually not good practice to instantiate a new String("somestring");, as using String literals allows them to be cached in the String pool, which saves some memory. Instantiating a String explicitly is guaranteed to create a new String in memory, even if one exists in the String pool.
So long story short, just use String myStr = "stringvalue"; over String myStr = new String("someValue"); when instantiating a String.
So long story short, just use String myStr = "stringvalue"; over String myStr = new String("someValue"); when instantiating a String.
|
|

New Topic/Question
Reply



MultiQuote






|