dimanche 8 mars 2015

File class and array in java


Vote count:

0




I am stuck when writing a java program, I have to calculate the grade of 121 students and store in an array for later use. But the array I created does not have the values from the for loop. Please help me. This is what I got now. Thank you. public class GradeCalculator{



public static void main(String[] args){
File grade = fileName();
int lines = getNumEntries(grade);
print(grade, lines);
}
public static File fileName(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the grade file");
String fileName = scan.nextLine();
File file = new File(fileName);
while(!file.exists()){
System.out.println("your input file does not exist");
fileName = scan.nextLine();
file = new File(fileName);
}return file;
}

public static int getNumEntries(File fln){
File file = fln;
int count = 0;
try{
Scanner input = new Scanner (fln);
while(input.hasNextLine()){
count++;
input.nextLine();
}
}catch(FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}return count;
}

public static void print(File fln, int values)throws NoSuchElementException{
File file = fln;
int []a = new int [values];
try{
Scanner input = new Scanner(file);
for(int i=0; i<=a.length; i++){
while(input.hasNextDouble()){
double Lab = input.nextDouble();
double A1 = input.nextDouble()/10*4;
double A2 = input.nextDouble()/10*4;
double A3 = input.nextDouble()/10*4;
double A4 = input.nextDouble()/10*4;
double A5 = input.nextDouble()/10*4;
double A6 = input.nextDouble()/10*4;
double A7 = input.nextDouble()/10*4;
double Midterm1 = input.nextDouble()/20*15;
double Midterm2 = input.nextDouble()/35*15;
double Final = input.nextDouble()/110*37;
a[i]= (int)(Lab+A1+A2+A3+A4+A5+A6+A7+Midterm1+Midterm2+Final);
System.out.println(a[i]); //If I put the println here the values are correct
}
}
}catch(FileNotFoundException e){
System.out.println("File not found");
}System.out.println(a[i]); //If I put the println here the values are different
}


asked 1 min ago







File class and array in java

Aucun commentaire:

Enregistrer un commentaire