Question

I'm trying to read the ex2data2.txt which contains dataset with 3 columns. I want to store it in my x array and y array. but it is not working. it cannot read the text file. the location of text file is in my src folder along with my codes.

    double[][] x = new double[180][1];
    double[][] y = new double[180][0];
       try{
           BufferedReader br=new BufferedReader(new FileReader("ex2data2.txt"));
           String words[]=new String[2];
           String line=null;
           int i = 0;
           while((line=br.readLine())!=null){
               words=line.split(",");
               //double[i] y = Double.parseDouble(words);
                x[i][0] = Double.parseDouble(words[0]);
                x[i][1] = Double.parseDouble(words[1]);
                y[i][0] = Double.parseDouble(words[2]);
           i++;
           }
           br.close();
       }
       catch(Exception e){
           System.err.println("Error: Target File Cannot Be Read");
       }
Était-ce utile?

La solution

You should put the file in the top, parent project folder. not in src/.

Edit : I have run this, for proof...

Accessing file, read all the data.

java.lang.NumberFormatException: For input string: "43 323 33" this I have put in that file

at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
at java.lang.Double.parseDouble(Double.java:540)
at com.rem.so.Rem.main(Rem.java:18)

Next run

java.lang.ArrayIndexOutOfBoundsException: 1
    at com.rem.so.Rem.main(Rem.java:19)

After correcting this..

double[][] x = new double[180][2];
double[][] y = new double[180][1];

it is now done.

Please find the screenshot where I have put the file...

enter image description here

Autres conseils

double[][] y = new double[180][0];

That's probably not what you wanted. The second dimension is zero so you wont be able to put anything in that array. The second dimension needs to be at least 1.

catch(Exception e){
   System.err.println("Error: Target File Cannot Be Read");
}

Any developer writing this code should be immediately fired.

Never catch an Exception, but perform proper and specific error handling. Catch the most precise error to handle (e.g. FileNotFoundException), avoid catching errors that you do not know how to handle. If in doubt, rethrow the error, so that an outer error handler can process it. Always log the error, unless it is expected to happen.

Then you would be getting a more precise error message. But you opted to ignore the actual error e.

REALLY REALLY read a Java book or tutorial, in particular the section on proper exception handling. You are making your own live difficult by discarding the error the way you have been doing it.

An exception is almost never something you should ignore. You are required to handle them for a reason! (In fact, there are also "unchecked" exception, where you are not required to do so. Which makes it even more obvious that here, you need to handle them properly.)

Three Rules for Effective Exception Handling (java.net)


Your error most likely is an ArrayOutOfBoundsException (which you should not have caught!)

double[][] x = new double[180][1];
double[][] y = new double[180][0];

this is too small. you probably need

double[][] x = new double[180][2];
double[][] y = new double[180][1];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top