Reading file from Excel

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 332 Views - Last Post: 21 September 2011 - 08:34 PM Rate Topic: -----

Topic Sponsor:

#1 Javahelp1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-September 11

Reading file from Excel

Posted 21 September 2011 - 12:19 PM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Reading file from Excel

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7498
  • View blog
  • Posts: 28,845
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 63
  • Joined: 07-June 11

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.
Was This Post Helpful? 0
  • +
  • -

#4 Javahelp1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-September 11

Re: Reading file from Excel

Posted 21 September 2011 - 12:42 PM

View PostViske, 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?
Was This Post Helpful? 0
  • +
  • -

#5 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 63
  • Joined: 07-June 11

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.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7498
  • View blog
  • Posts: 28,845
  • Joined: 27-December 08

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.

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.
Was This Post Helpful? 0
  • +
  • -

#7 Javahelp1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-September 11

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?







}

}
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7498
  • View blog
  • Posts: 28,845
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#9 Javahelp1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-September 11

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?
Was This Post Helpful? 0
  • +
  • -

#10 Viske  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 63
  • Joined: 07-June 11

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..


Was This Post Helpful? 0
  • +
  • -

#11 Javahelp1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-September 11

Re: Reading file from Excel

Posted 21 September 2011 - 02:16 PM

View PostViske, 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?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7498
  • View blog
  • Posts: 28,845
  • Joined: 27-December 08

Re: Reading file from Excel

Posted 21 September 2011 - 02:20 PM

Are you familiar on how to work with arrays?
Was This Post Helpful? 0
  • +
  • -

#13 Javahelp1234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-September 11

Re: Reading file from Excel

Posted 21 September 2011 - 03:48 PM

View Postmacosxnerd101, 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?
Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

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[]).
Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7498
  • View blog
  • Posts: 28,845
  • Joined: 27-December 08

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. :)
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2