954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with instance

Hi, I have a problem with my instance. I need to do a file read to extract all the numbers in the text file. However, when reading the file, I used String, and hence the file is read as a String. Later on I need to equate the output of the file read to a double. I have this error here, String and double cannot be equate to each other. May I know how to read the file using double instead of String?

The part where I read the file:

try
        {
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\RMSDscores.txt"));    //reading files in specified directory

<strong>String</strong> str;
            while ((str = in.readLine()) != null)    //file reading
            {
                            display = str;
                            System.out.print(display);
              }
            in.close();

}catch( IOException ioException ) {}


The bolded part is the part where String is used to read the file. However I need to read it as a double. May I know how to do it?

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Double.parseDouble(str)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thank you. I tried using it, but there seems to be a problem when I want to equate the output of the text file to my 2D array.

....
static double mydisplay;
static double[][] mydouble = new double [1000][1000];
int a, b;

try
        {
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\Serene\\Documents\\Major Project\\Alignment Algorithms\\RMSDscores.txt"));    //reading files in specified directory

String str;
            while ((str = in.readLine()) != null)    //file reading
            {
                            display = str;
                            mydisplay = Double.parseDouble(display);
                            mydouble[a][b] = mydisplay;
                            System.out.print(mydouble);
              }
            in.close();

}catch( IOException ioException ) {}
Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

What problem? I am not going to play compiler and JVM rolled into one to try and figure it out.

Edit: Which means, of course, that I haven't even looked at the code. Give me the complete exception and/or compiler warning/error or explain, in detail, the difference between the expected result and the actual result if there was no error.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This is the error message:

Exception in thread "main" java.lang.NumberFormatException: For input string: "{ 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0},"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at MatchApplet.init(DPTest.java:117)
at MatchApplet.main(DPTest.java:77)

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Well, of course. Why would you assume that parseDouble takes a list of doubles. If you actually look at the API you would see it takes a String representing a single double value, of course. Split the String first, of course, and call parse Double on each element.

I feel obliged to mention that another possibility is to use Scanner. You'll have to get someone else to help you with that though, as I don't use Scanner its performance is not "up to snuff" for what I do.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

P.S. why is this called "MatchApplet" (an eventual Applet I would assume) but has a main method? You also know, I assume, that you won't be able to read files on the client from an Applet, right? At least not without signing the Applet and/or security policy changes on the client.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

How do I split? You mean split the content of the text file? MatchApplet is just the name for the class.

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
How do I split?


See the API docs for String (see my hint about looking at the API docs for Double, and now, since you're talking about "splitting" aString it should have been logical to at least look at the API docs for String).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I really don't have any idea how to do it.

Sunshineserene
Junior Poster
187 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 
while ((str = in.readLine()) != null) //file reading { display = str; mydisplay = Double.parseDouble(display); mydouble[a][b] = mydisplay; System.out.print(mydouble); } in.close();

I can suggest a something here ...

1. Use the split() method to split/separate a string according to a given pattern (regular expression). split() returns an array.
Ex : str.split(",") splits the given string by commas

2. Now, you can do something like this to read those doubles :

try {
System.out.println(Double.parseDouble(splitArray[i]));
} catch(NumberFormatException f) {}

This works but it is not a very good programming practice to catch exceptions and not handle them.

However, if you want your program to work as it is, then change the input file to something like this :
1.0
2.0
3.0
.....

vasu d
Newbie Poster
8 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: