Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
you're reading data from a file, so you want to create the array to keep the data
how can you create a 2D array without knowing how many rows it's going to be?
thanks

OK.
Step is
1. read size that you want to define 2D array from file
2. bring it in declare object array size//this example program read size 2D array from file "dat.txt"
import java.io.*;
class aarr{
public static void main (String[] asg) throws IOException{FileInputStream f = new FileInputStream("dat.txt");
InputStreamReader input = new InputStreamReader(f,"8859_1");
BufferedReader bufferR = new BufferedReader(input);
String s = bufferR.readLine().trim();
double d = Double.valueOf(s).doubleValue();
bufferR.close();
input.close();
f.close();int n = (int)d;
double [][] a = new double[n][n];
for(int i = 0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j] = 1.234;
}
System.out.println(i);
}
}
}

aves[i]=CalAves(grades, aves, numStu);
}//end main
-Calculate-------
static void CalAves(int grades[][], double aves[], int numStu){
double sc1, sc2, sc3;
for(int i=1; i<numStu; i++){
sc1=double(grades[i][0])/grades[0][0]*100/2;
sc2=0.0;
for(int j=1; j<4; j++)
sc2+=scores[i][j];
sc2=double (sc2/3/3);
sc3=0.0;
for(int j=4; j<7; j++)
sc3+=double (grades[i][j])/grades[0][j];
sc3=sc3*100/4/6;
aves[i]=sc1+sc2+sc3;
} //end for loop
} //end CalAvesI want to get the averages from the method, but these are the 3 errors:
GradeSystem.java:85: '.class' expected
sc1=double(grades[i][0])/grades[0][0]*100/2;
^
GradeSystem.java:89: '.class' expected
sc2=double (sc2/3/3);
^
GradeSystem.java:92: '.class' expected
sc3+=double (grades[i][j])/grades[0][j];What does it mean? do i need a new class????

because you declare variable wrong in syntexdouble sc1, sc2, sc3; // OK in c++ but No OK in java
Java must define value too;
double sc1 = 1.234;
double sc2 = 1.234;
double sc3 = 1.234;if not define value for variable when declare you must declare outside function
such asclass grade(){
double sc1,sc2,sc3; // OK not wrong
grade(){
...;
sc1 = ....; // OK not wrong
}
}otherwise you must define value for variable when declare
class grade(){
grade(){
...;
double sc1 = 1.234;
double sc2 = 1.234;
double sc3 = 1.234;
sc1 = ....;
}
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |