/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package arrayexample; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author jcmtiernan */ public class ArrayExample { /** * @param args the command line arguments */ public static void main(String[] args) { final int NAMES = 90; final int GRADES = 4; double[][] grades = new double[NAMES][GRADES]; String[] names = new String[NAMES]; // typically read this from a file File inFile = new File("Grades.txt"); File inName = new File("Names.txt"); Scanner grFile; Scanner nmFile; try { grFile = new Scanner(inFile); nmFile = new Scanner(inName); } catch (FileNotFoundException e) { grFile = new Scanner(System.in); nmFile = new Scanner(System.in); } int nmCount = 0; // number of names counted from file while (nmFile.hasNext() && (nmCount < NAMES) ) { names[nmCount++] = nmFile.nextLine(); } System.out.println("Read in names"); for( int i=0; i < nmCount; i++) { System.out.println(names[i]); } for (int row = 0; row < nmCount; row++) { for (int col = 0; col < GRADES; col++) { grades[row][col] = grFile.nextDouble(); } } System.out.println( "Grades: "); for (int row = 0; row < nmCount; row++) { printArray(grades[row], GRADES); /* for (int col = 0; col < GRADES; col++) { System.out.println(grades[row][col] ); } */ System.out.println(); } } public static void printArray(double[] vals, int max) { for (int col = 0; col < max; col++) { System.out.printf("%8.2f ", vals[col] ); } } }