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

[B]String[/B] 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?

Recommended Answers

All 10 Replies

Double.parseDouble(str)

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 ) {}

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.

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)

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.

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.

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

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" a String it should have been logical to at least look at the API docs for String).

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

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 :

ex:

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
.....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.