Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi! This program is supposed to write to a file, then read it. It's reading randomized coordinates generated by the program.
Whenever I compile the program it gives me the error of:

File: F:\COMPSCI 12\J4.java  [line: 54]
Error: F:\COMPSCI 12\J4.java:54: '.class' expected


I'm not sure what the error means. How can I fix this? Thanks!

CODE:
Java
import java.io.*;
import java.util.Arrays;

public class J4
{
  public static void main (String [] args) throws IOException
  {
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100
    
    //arrays are initializewd and declared
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];
    
    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));
    
    writefile(lengthscale, locations, dimension, numpoints, length);
    
    
    for(int m=0; m <length; m++){//for loop
      fileOut.println(Arrays.toString(locations[m]) + ", ");//writes to file
    }
    fileOut.close ();//close file
    
    readfile();
  }//end main
  
  public static Double writefile(double lengthscale[], double locations[][], int dimension, int numpoints, int length)throws IOException
  {
    
    for (int a = 0; a < dimension; a++) {// for loop runs while a is less than dimension
      lengthscale[a] = length;// stores array
    }// end for loop
    
    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within 
        
      }//end nested for loop
    }//end for loop
    //return locations[x][y];
    //if program doesnt run through loop.. alternative return statement (but
    double b= 1;    
    return b;
  }//end writefile methos
  
  public static int readfile()
  {
    BufferedReader readFile = new BufferedReader (new FileReader ("arrayNumPoints.txt"));
    
    int readarray [] = new int [100];
    for (int counter=0; counter < 100; counter++){
      readarray[counter] = Integer.parseInt (readFile.readLine ());
    }
    return readarray [];
  
}//end readfile method
}//end class
Posted
Updated 23-Sep-13 14:48pm
v2
Comments
Sergey Alexandrovich Kryukov 23-Sep-13 20:47pm    
And where is that line 54?
—SA
Member 10292973 23-Sep-13 20:53pm    
line 54 is my return statement: return readarray [];

and sorry, im new to this site, but yeah ill make sure to do that next time!
Sergey Alexandrovich Kryukov 23-Sep-13 21:30pm    
First of all, comment this line to show where the error is. Use "return readarray;" and change return type to "int[]".
—SA
Sergey Alexandrovich Kryukov 23-Sep-13 20:49pm    
Use the attribute lang="Java" of the "pre" tag (I've done it for your, please do it next time).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900