Computing.Net > Forums > Programming > array (java)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

array (java)

Reply to Message Icon

Name: quyen
Date: November 19, 2003 at 19:14:18 Pacific
OS: 98
CPU/Ram: unix
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: tvara
Date: November 20, 2003 at 03:54:32 Pacific
Reply:

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



0

Response Number 2
Name: quyen
Date: November 20, 2003 at 15:04:13 Pacific
Reply:

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 CalAves

I 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????



0

Response Number 3
Name: tvara
Date: November 21, 2003 at 04:42:04 Pacific
Reply:


because you declare variable wrong in syntex

double 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 as

class 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 = ....;
}
}


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: array (java)

cannot Sort String Array in JAVA www.computing.net/answers/programming/cannot-sort-string-array-in-java-/8805.html

why 2d array java limited only this www.computing.net/answers/programming/why-2d-array-java-limited-only-this/8627.html

Java Script menu source www.computing.net/answers/programming/java-script-menu-source/1486.html