Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.io.IOException;
- import javax.swing.*;
- import java.util.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.lang.IllegalStateException;
- import java.util.NoSuchElementException;
- public class JavaMultiArrays{
- public static void main (String[] args) throws Exception{
- //Define the arrays Name Rec and GPA
- char[][] Id= new char [5][20];
- String sname,ssn,sid, output1=" ";
- int [] [] [] Rec = new int [5][4][2];
- double [] Gpa = new double [5];
- int hsum, gpsum, Hs;
- //Setting up an external output file
- //Create an internal name for your file call it output the printwriter will be used to write on the file
- PrintWriter output;
- //Equate the internal name to an external file through the PrintWriter external name is MyOuput3.txt
- output=new PrintWriter(new File("MultiArrayOut.txt"));
- //Opening an External Input File
- try{
- Scanner input = new Scanner(new File("C:\\Users\\Carlos\\IdeaProjects\\GradesHelp\\src\\main\\java\\MultiArrayIn.Txt"));
- //Read in the student Name and SSN. Place them in a ID and store in the Char Array Name
- for(int Stu =0;Stu<=4;Stu++){
- //Read the name
- sname=input.next();
- //Now the SSN
- ssn=input.next();
- //Now form ID for the student name and SSN
- sid=sname+" "+ssn;
- Id[Stu]=sid.toCharArray();
- System.out.print(Id[Stu]);
- //Now read the remainder of the students records
- for(int Sem=0;Sem<=3;Sem++){
- //For Each semester read and store the hours in Rec [Stu][sem][0] and Gpts in Rec[stu][sem][1]
- Rec [Stu][Sem][0]=input.nextInt();
- Rec [Stu][Sem][1]=input.nextInt();
- output1=" "+Rec[Stu][Sem][0]+" "+Rec [Stu][Sem][1]+" ";
- System.out.print(" "+Rec[Stu][Sem][0]+" "+Rec[Stu][Sem][1]+" " );
- }
- System.out.println();
- }
- //Compute the GPA for each student an store in the Gpa [Stu]
- System.out.println("\n\n Student IDs and GPAs");
- for(int Stu=0;Stu<=4;Stu++){
- hsum=gpsum=0;
- //Sum the hours attempted and grade points for each semester across all semesters.
- for(int Sem=0;Sem<=3;Sem++){
- hsum+=Rec[Stu][Sem][0];
- gpsum+=Rec[Stu][Sem][1];
- }
- //Now calculate the GPA for this student
- Gpa[Stu]=(double)gpsum/hsum;
- //Round GPA to two decimal places
- Gpa[Stu]=(int)(Gpa[Stu]*100)/100.00;
- //Now output the student identifier and their GPA
- System.out.println(Stu);
- System.out.print(Id[Stu]);
- System.out.println("GPA is "+Gpa[Stu]);
- }
- //Find the highest GPA student
- Hs=FindHighStudent(Gpa,5);
- System.out.print("Student with Highest GPA is");
- System.out.print(Id[Hs]);
- System.out.print("GPA"+Gpa[Hs]);
- }
- catch (FileNotFoundException e){
- System.err.println("File Not Found");
- output.close();
- System.exit(11);
- }
- }
- public static int FindHighStudent(double []Gpa, int size){
- //This function looks at array GPA[] and returns the subscript of the highest GPA
- int HighS=0;
- double Hgpa=Gpa[0];
- for(int i=0; i<=size-1;i++){
- if(Gpa[i]>Hgpa){
- Hgpa=Gpa[i];
- HighS=i;
- }
- }
- return HighS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement