Results 1 to 11 of 11
Thread: Excel file read error
- 04-05-2011, 04:15 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
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:
I am getting an error on the while loop.. Here is what it says: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()); } } }
ExcelFileRead.java:16: illegal start of expression
while(strLine == br.readLine() && != null)
Any help will be much appreciated!
- 04-05-2011, 04:34 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 12
- Rep Power
- 0
while(strLine == br.readLine() && strLine != null) will fix the illegal start of expression.
- 04-05-2011, 04:38 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
It fixed the illegal start of expression error, but now it's saying
strLine may not be initialized
- 04-05-2011, 04:46 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 12
- Rep Power
- 0
Whenever you get that message just give the variable a starting value when you initialize it. So try "String strLine = null"
- 04-05-2011, 04:49 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
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:
I am using openoffice to open up the .csv file.. I don't have office installed on my laptop.. could that be the issue?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()); } } } }Last edited by Asvin; 04-10-2011 at 01:08 PM.
- 04-10-2011, 01:09 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Bumping for help
- 04-25-2011, 06:17 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Here is the updated code:
It compiles perfectly, but when I run it, it outputs: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(); } }
"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?
- 04-26-2011, 02:40 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Anyone? Please?
- 04-27-2011, 06:03 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Bump.. Anyone? Lol!
- 04-28-2011, 09:34 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Bump! Anyone??????????????????????
- 04-28-2011, 10:01 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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
-
How to read Excel file in java
By nagaraj in forum New To JavaReplies: 7Last Post: 03-18-2011, 05:09 AM -
how to read excel file through java program
By madhuragowda in forum New To JavaReplies: 2Last Post: 04-16-2010, 08:53 AM -
Read pasword protected Excel file
By vijay7539 in forum Advanced JavaReplies: 1Last Post: 11-27-2009, 05:13 PM -
How to Read Excel file??
By spalax in forum New To JavaReplies: 3Last Post: 08-15-2009, 05:06 AM -
How to read Excel file with java
By chetan-24 in forum New To JavaReplies: 5Last Post: 04-22-2009, 05:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks