Advertisement
Luninariel

MultiArrayHelp

Jan 29th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.IOException;
  3. import javax.swing.*;
  4. import java.util.*;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.lang.IllegalStateException;
  8. import java.util.NoSuchElementException;
  9.  
  10. public class JavaMultiArrays{
  11. public static void main (String[] args) throws Exception{
  12. //Define the arrays Name Rec and GPA
  13. char[][] Id= new char [5][20];
  14. String sname,ssn,sid, output1=" ";
  15. int [] [] [] Rec = new int [5][4][2];
  16. double [] Gpa = new double [5];
  17. int hsum, gpsum, Hs;
  18.  
  19.  
  20. //Setting up an external output file
  21. //Create an internal name for your file call it output the printwriter will be used to write on the file
  22.  
  23. PrintWriter output;
  24. //Equate the internal name to an external file through the PrintWriter external name is MyOuput3.txt
  25. output=new PrintWriter(new File("MultiArrayOut.txt"));
  26.  
  27. //Opening an External Input File
  28.  
  29. try{
  30. Scanner input = new Scanner(new File("C:\\Users\\Carlos\\IdeaProjects\\GradesHelp\\src\\main\\java\\MultiArrayIn.Txt"));
  31.  
  32. //Read in the student Name and SSN. Place them in a ID and store in the Char Array Name
  33. for(int Stu =0;Stu<=4;Stu++){
  34. //Read the name
  35. sname=input.next();
  36. //Now the SSN
  37. ssn=input.next();
  38. //Now form ID for the student name and SSN
  39. sid=sname+" "+ssn;
  40. Id[Stu]=sid.toCharArray();
  41. System.out.print(Id[Stu]);
  42. //Now read the remainder of the students records
  43. for(int Sem=0;Sem<=3;Sem++){
  44. //For Each semester read and store the hours in Rec [Stu][sem][0] and Gpts in Rec[stu][sem][1]
  45. Rec [Stu][Sem][0]=input.nextInt();
  46. Rec [Stu][Sem][1]=input.nextInt();
  47. output1=" "+Rec[Stu][Sem][0]+" "+Rec [Stu][Sem][1]+" ";
  48. System.out.print(" "+Rec[Stu][Sem][0]+" "+Rec[Stu][Sem][1]+" " );
  49. }
  50. System.out.println();
  51. }
  52. //Compute the GPA for each student an store in the Gpa [Stu]
  53. System.out.println("\n\n Student IDs and GPAs");
  54. for(int Stu=0;Stu<=4;Stu++){
  55. hsum=gpsum=0;
  56. //Sum the hours attempted and grade points for each semester across all semesters.
  57. for(int Sem=0;Sem<=3;Sem++){
  58. hsum+=Rec[Stu][Sem][0];
  59. gpsum+=Rec[Stu][Sem][1];
  60. }
  61. //Now calculate the GPA for this student
  62. Gpa[Stu]=(double)gpsum/hsum;
  63. //Round GPA to two decimal places
  64. Gpa[Stu]=(int)(Gpa[Stu]*100)/100.00;
  65. //Now output the student identifier and their GPA
  66. System.out.println(Stu);
  67. System.out.print(Id[Stu]);
  68. System.out.println("GPA is "+Gpa[Stu]);
  69. }
  70. //Find the highest GPA student
  71. Hs=FindHighStudent(Gpa,5);
  72. System.out.print("Student with Highest GPA is");
  73. System.out.print(Id[Hs]);
  74. System.out.print("GPA"+Gpa[Hs]);
  75. }
  76. catch (FileNotFoundException e){
  77. System.err.println("File Not Found");
  78. output.close();
  79. System.exit(11);
  80. }
  81. }
  82. public static int FindHighStudent(double []Gpa, int size){
  83. //This function looks at array GPA[] and returns the subscript of the highest GPA
  84.  
  85. int HighS=0;
  86. double Hgpa=Gpa[0];
  87. for(int i=0; i<=size-1;i++){
  88. if(Gpa[i]>Hgpa){
  89. Hgpa=Gpa[i];
  90. HighS=i;
  91. }
  92. }
  93. return HighS;
  94.  
  95.  
  96.  
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement