I am trying to read in CSV data. "test.scv"'s contents are:
1,2,3
4,5,6
7,8,9
My code is:
import java.io.*;
import com.Ostermiller.util.*;
class csvtest {
public static void main(String[] args) {
String[][] data = (new CSVParser(new
FileReader("test.csv"))).getAllValues();
System.out.println(data);
}}
The errors are:
unreported exception java.io.FileNotFoundException; must be caught or
declared to be thrown
String[][] data = (new CSVParser(new
FileReader("test.csv"))).getAllValues();
Note: .\com\Ostermiller\util\CSVParser.java uses unchecked or unsafe
operations.
Thank you for your help
Rob
opalpa@gmail.com opalinski from opalpaweb - 30 Jan 2006 20:28 GMT
For this sample you can alter the line with main on it to:
public static void main(String[] args) throws
java.io.FileNotFoundException {
Making a FileReader can cause a problem, if the file does not exist,
during the creation of FileReader. If there is a problem a signal,
called an exception, is sent back. You can catch it or have a method
it throw it to its own caller.
Here is a doc that explains exceptions:
http://mindprod.com/jgloss/exception.html
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
Monique Y. Mudama - 30 Jan 2006 20:30 GMT
> I am trying to read in CSV data. "test.scv"'s contents are:
>
[quoted text clipped - 26 lines]
>
> Rob
You need to put a try block around the line that invokes the
FileReader constructor. Then you need to put a catch block right
below the try block that catches the exception that the FileReader
constructor can throw.
Do you understand exceptions and how to work with them? The
FileReader constructor can throw an exception if it doesn't find the
file you specify. Because it can throw that exception, you need to
catch it and figure out what to do about it.

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 30 Jan 2006 22:17 GMT
>I am trying to read in CSV data. "test.scv"'s contents are:
there are lots of CSV readers. If you can't solve this try another. I
wrote one too. See http://mindprod.com/jgloss/csv.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 30 Jan 2006 22:19 GMT
>unreported exception java.io.FileNotFoundException; must be caught or
>declared to be thrown
[quoted text clipped - 3 lines]
>Note: .\com\Ostermiller\util\CSVParser.java uses unchecked or unsafe
>operations.
Your FileReader could blow up with a FileNotFoundException. You need
to to catch that or at least put a throws on main to let Java deal
with it.
His code was written to work prior to JDK 1.5, thus Javac will
complain about it not using 1.5 generics. It is nothing to worry
about.
you could modify his code to generify it.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Rob - 31 Jan 2006 13:12 GMT
Thank you all for you help. I am trying to teach my self java with
background in Mathematica. This transition is turning out to be a much
larger undertaking that I initially thought. It seems obvious that I
need to learn a lot more before I can start useful programs. I am a
mechanical engineer and my end goal is to import well formatted CSV
test data and automate mathematical operations returning material
properties. What resources (packages, books, resourses) would all you
suggest? Thanks.
Rob
Thomas Weidenfeller - 31 Jan 2006 13:35 GMT
> I am a
> mechanical engineer and my end goal is to import well formatted CSV
> test data and automate mathematical operations returning material
> properties.
Is there a particular reason you picked Java for the job? I don't want
to distract you from learning Java, but there might be easier/quicker
ways to do things if you just want to get things done. Digging through
test data is something where I consider using tools like Perl.
What resources (packages, books, resourses) would all you
> suggest?
http://java.sun.com/docs/books/tutorial/
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Rob - 31 Jan 2006 16:39 GMT
I picked Java because I have other projects that will be more Java
friendly such as GUIs and I did not think that there would be much
difficulty. For resources I was hoping for ones more geared towards
engineering programmers specifically N-dimensional matrices and
performing mathematical operations on them.
Michael Redlich - 31 Jan 2006 18:11 GMT
> I picked Java because I have other projects that will be more Java
> friendly such as GUIs and I did not think that there would be much
> difficulty. For resources I was hoping for ones more geared towards
> engineering programmers specifically N-dimensional matrices and
> performing mathematical operations on them.
Rob:
Have you considered Matlab? In case you're not familiar with the
product, it is a high performance numerical analysis package that is
perfect for performing operations on matrices. You can import CSV
files to generate vectors or matrices, perform calculations, and plot
as necessary. I use it all the time for performing calculations that
the vendor software can't do on the rheometer and FTIR instruments that
I use at work.
There are also built-in APIs for Java and C++.
For more information, check out http://www.mathworks.com/.
Sincerely,
Mike.
--- ACGNJ Java Users Group (http://www.javasig.org/)
Thomas Weidenfeller - 01 Feb 2006 09:06 GMT
> I picked Java because I have other projects that will be more Java
> friendly such as GUIs and I did not think that there would be much
> difficulty. For resources I was hoping for ones more geared towards
> engineering programmers specifically N-dimensional matrices and
> performing mathematical operations on them.
Java is a general-purpose language for application programming. Compared
to other languages it is relatively simple. It is not specifically good
or bad at numeric processing or number crunching.
Regarding number crunching in Java, ask THE government :-)
http://math.nist.gov/javanumerics/
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Rob - 01 Feb 2006 13:43 GMT
Thank you all for your replies.
In response to why I chose Java instead of Matlab, Maple, or
Mathematica: Ideally I would simply use Mathematica to perform this
task because I could write the code with little effort. However, my
new company does not have this and due to the large list of resources
that I am requesting I wanted to use free software.
In response to resources: I have looked through most of the resources
that you suggested. I was hoping to find some sample code that is
similar to what I need to do but did not find it. Could one of you
provide some code, fix mine or explain whats wrong with mine in simple
terms so that I can use it as a starting point and build from there?
Thank you again for your help.
Rob
Ian Mills - 04 Feb 2006 10:54 GMT
> Thank you all for your replies.
>
[quoted text clipped - 12 lines]
> Thank you again for your help.
> Rob
To put it as simply as possible some methods (such as creating a new
FileReader) throw exceptions which must either caught in a try/catch
block or 'thrown on'. You mafind it usefull to look at the sun Java
tutorial on exceptions at
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html
to fix your code you would need to cahnge it to:
try
{
String[][] data = (new CSVParser(new
FileReader("test.csv"))).getAllValues();
}
catch (FileNotFoundException ex)
{
//put some code here to process the error
}
Rob - 06 Feb 2006 13:24 GMT
Thank you for all your replies. You have been very helpful.
Rob
Roedy Green - 31 Jan 2006 13:54 GMT
>What resources (packages, books, resourses) would all you
>suggest?
to read/write your CSV data, see http://mindprod.com/jgloss/csv.html
See http://mindprod.com/jgloss/gettingstarted.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.