Results 1 to 11 of 11
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Excel file read error

    I was working on a project that involves importing data from .csv file and then printing that on the screen!

    Here is what I have so far:

    Java Code:
    import java.io.*;
    
    class ExcelFileRead
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			FileInputStream fstream = new FileInputStream("AAPL.csv");
    			DataInputStream in = new DataInputStream(fstream);
    
    			BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
    			String strLine;
    
    			while(strLine == br.readLine() && != null)
    			{
    				String str2 = strLine.replace(',',' ');
    
    				System.out.println(str2);
    			}
    
    			in.close();
    		}
    
    		catch(Exception e)
    		{
    			System.err.println("Error: " + e.getMessage());
    		}
    	}
    }
    I am getting an error on the while loop.. Here is what it says:

    ExcelFileRead.java:16: illegal start of expression
    while(strLine == br.readLine() && != null)

    Any help will be much appreciated!

  2. #2
    erversteeg is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    while(strLine == br.readLine() && strLine != null) will fix the illegal start of expression.

  3. #3
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    It fixed the illegal start of expression error, but now it's saying

    strLine may not be initialized

  4. #4
    erversteeg is offline Member
    Join Date
    Dec 2010
    Posts
    12
    Rep Power
    0

    Default

    Whenever you get that message just give the variable a starting value when you initialize it. So try "String strLine = null"

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Now it's giving me the following errors when I compile:

    ExcelFileRead.java.61: 'catch' without 'try'
    catch(Exception e)
    ExcelFileRead.java.61: '}' expected
    catch(Exception e)
    ExcelFileRead.java.61: not a statement
    catch(Exception e)
    ExcelFileRead.java.61: ';' expected
    catch(Exception e)
    ExcelFileRead.java.24: 'try' without 'catch' or 'finally'
    try
    5 errors
    Press any key to continue . . .

    Here is the code I have right now:

    Java Code:
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class ExcelFileRead
    {
    		public static double convertJDN(String date)
    		{
    				String[] str = date.split("/");
    				int Y = Integer.parseInt(str[2]);
    				int M = Integer.parseInt(str[0]);
    				int D = Integer.parseInt(str[1]);
    
    				double JDN = (1461 * (Y + 4800 + (M - 14) / 12)) / 4 + (367 * (M - 2 - 12 * ((M - 14) / 12))) / 12 - (3 * ((Y + 4900 + (M - 14) / 12) / 100)) / 4 + D - 32075;
    
    				return JDN;
    		}
    
    		public static void main(String[] args)
    		{
    					try
    					{
    						FileInputStream fstream = new FileInputStream("AAPL.csv");
    						DataInputStream in = new DataInputStream(fstream);
    
    						BufferedReader br = new BufferedReader(new InputStreamReader(in));
    
    						String strLine = null;
    
    						while((strLine = br.readLine()) && strLine != null)
    						{
    								String strAy[] = strLine.split(",");
    
    								for(int i = 0; i < strAy.length; i++)
    								{
    										if(strAy[i].contains("/"))
    										{
    											double jdn = convertJDN(strAy[i]);
    
    											System.out.format("%-12s",jdn);
    										}
    
    										else
    										{
    											double d;
    
    
    												d = Double.parseDouble(strAy[i]);
    
    												System.out.format("$-12s",strAy[i]);
    
    										}
    								}
    
    								in.close();
    						}
    
    						catch(Exception e)
    						{
    							System.err.println("Error: " + e.getMessage());
    						}
    			}
    		}
    	}
    I am using openoffice to open up the .csv file.. I don't have office installed on my laptop.. could that be the issue?
    Last edited by Asvin; 04-10-2011 at 01:08 PM.

  6. #6
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Bumping for help

  7. #7
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Here is the updated code:

    Java Code:
    import java.io.*;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    
    public class ExcelFileRead1
    {
    
    	public static double convertJDN(String date)
    	{
    			String[] str = date.split("/");
    			int Y = Integer.parseInt(str[2]);
    			int M = Integer.parseInt(str[0]);
    			int D = Integer.parseInt(str[1]);
    
    			double JDN = (1461 * (Y + 4800 + (M - 14) / 12)) / 4 + (367 * (M - 2 - 12 * ((M - 14) / 12))) / 12 - (3 * ((Y + 4900 + (M - 14) / 12) / 100)) / 4 + D - 32075;
    
    			return JDN;
    	}
    
    	public static void main(String[] args)
    	{
    
    		String fileName = "AAPL.csv";
    
    		Scanner inputStream = null;
    
    		System.out.println("The file " + fileName + "contains the following values: ");
    
    		try
    		{
    			inputStream = new Scanner(new File(fileName));
    		}
    
    		catch(FileNotFoundException e)
    		{
    			System.out.println("Error opening the file " + fileName);
    
    			System.exit(0);
    		}
    
    		while(inputStream.hasNextLine())
    		{
    			String line = inputStream.nextLine();
    
    			System.out.println(line);
    		}
    
    		inputStream.close();
    	}
    }
    It compiles perfectly, but when I run it, it outputs:

    "Error opening the file AAPL.csv"

    ^ This is what it should output if the file is not reachable or openable.. The file, it's class file, and AAPL.csv are all in my flash drive.. I also use open office, instead of excel.. Could those be the causes for this error?

  8. #8
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Anyone? Please?

  9. #9
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Bump.. Anyone? Lol!

  10. #10
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default

    Bump! Anyone??????????????????????

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It compiles perfectly, but when I run it, it outputs:

    "Error opening the file AAPL.csv"

    ^ This is what it should output if the file is not reachable or openable.. The file, it's class file, and AAPL.csv are all in my flash drive.

    Insert some code so you can exactly what file the program is looking for when you get the error. Also it's a good idea to get as much information as you can from the exception itself.

    Java Code:
    try
    {
        [color=blue]File file = new File(fileName);
        System.out.println("About to open " + file.getAbsolutePath());
        inputStream = new Scanner(file);[/color]
    }
    
    catch(FileNotFoundException e)
    {
        System.out.println("Error opening the file " + fileName);
        [color=blue]e.printStackTrace();[/color]
        System.exit(0);
    }

Similar Threads

  1. How to read Excel file with java
    By chetan-24 in forum New To Java
    Replies: 6
    Last Post: 09-13-2014, 12:43 PM
  2. Read pasword protected Excel file
    By vijay7539 in forum Advanced Java
    Replies: 2
    Last Post: 08-01-2014, 03:46 PM
  3. How to read Excel file in java
    By nagaraj in forum New To Java
    Replies: 7
    Last Post: 03-18-2011, 05:09 AM
  4. how to read excel file through java program
    By madhuragowda in forum New To Java
    Replies: 2
    Last Post: 04-16-2010, 08:53 AM
  5. How to Read Excel file??
    By spalax in forum New To Java
    Replies: 3
    Last Post: 08-15-2009, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •