Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java help! I keep getting this error Lab07StudentFile.txt 1221 100 100 100 100 1

ID: 3835950 • Letter: J

Question

java help!

I keep getting this error

Lab07StudentFile.txt

1221 100 100 100 100 100 100 100 100 100 100
1222 87 87 87 87 87 87 87 87 87 87
1223 80 80 80 80 80 80 80 80 80 80
1224 77 77 77 77 77 77 77 77 77 77
1225 70 70 70 70 70 70 70 70 70 70
1226 67 67 67 67 67 67 67 67 67 67
1227 60 60 60 60 60 60 60 60 60 60
1228 57 57 57 57 57 57 57 57 57 57
1343 90 85 80 65 75 95 85 75 80 94
1555 70 70 70 60 65 90 85 80 80 80
1856 60 0 45 68 89 67 60 50 75 80
1967 90 85 87 88 70 90 92 87 88 67
1987 68 68 68 68 68 68 68 68 68 100
1989 59 59 59 59 59 59 59 59 59 75
1991 100 90 75 85 90 88 79 88 91 90
1993 91 90 75 85 90 88 79 88 91 90

Lab07Names

1217 Carson, Joseph
1221 Korb, Barbara
1222 Schafer, Anne
1223 Kramer, Susan
1224 Smith, Lindy
1225 Caruso, Anthony
1226 Sokalski, Mark
1227 Nickerson, Philip
1228 Wydner, Peter
1256 Anderson, Beth
1277 Meyers, Henry
1343 Vickers, Jerry
1555 Collins, Richard
1602 Zimmer, Nancy
1856 Stanson, Luke
1976 Richman, Julie
1987 Lydon, Arthur
1989 Kelley, Nico
1991 Graham, Gary
1993 Parsons, Michael

----jGRASP exec: java Lab07
§Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
§ at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
§ at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2806)
§ at java.util.Formatter$FormatSpecifier.print(Formatter.java:2753)
§ at java.util.Formatter.format(Formatter.java:2520)
§ at java.io.PrintWriter.format(PrintWriter.java:905)
§ at java.io.PrintWriter.printf(PrintWriter.java:804)
§ at Lab07.main(Lab07.java:61)
§
§ ----jGRASP wedge: exit code for process is 1.
© ----jGRASP: operation complete.
¼¼

//Lab 7

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class Lab07 {
   public static void main(String[] args) throws FileNotFoundException
{
int[] scores = new int[10];
int id;
int studentCount = 0;
String studentName;
StudentGrades[] arrayStudent = new StudentGrades[16];

Scanner fileIn = new Scanner(new FileReader("Lab07StudentFile.txt"));
PrintWriter reportFile = new PrintWriter("Lab07Report.txt");

reportFile.printf("%s", "Student Grade Report" + " " + " ");
reportFile.printf("%-7s", "ID#");
reportFile.printf("%-20s", "Name");

String s = " TEST Scores ";

reportFile.printf("%50s%7s%7s%7s", s, "Total", "Adj Total", "Avg" + " " + " ");

while (fileIn.hasNextLine())
{

   id = fileIn.nextInt();
   for (int i = 0; i < 10; i++)
   {
scores[i] = fileIn.nextInt();

   }

   StudentGrades students = new StudentGrades(id, scores);

   arrayStudent[studentCount] = students;

   studentCount++;

}

BubbleSort(arrayStudent);

for (int i = 0; i < arrayStudent.length; i++)
{

  
   reportFile.printf("%-7d", arrayStudent[i].getStudentID());
   studentName = seqSearch(arrayStudent[i].getStudentID());
   reportFile.printf("%-20s", studentName);
   scores = arrayStudent[i].getGrades();
   for (int i1 = 0; i1 < 10; i1++)
   {
reportFile.printf("%5d", scores[i1]);
   }
   reportFile.printf("%7d%7d%7.0f", arrayStudent[i].getTotalGrade(), arrayStudent[i].getAdjTotal(),arrayStudent[i].getAverage());
   reportFile.println("");
}
reportFile.println("");
reportFile.printf("%s%d", "Total Students =", studentCount);
reportFile.println("");
reportFile.printf("%s", "Report by ");
fileIn.close();
reportFile.close();
   }

   public static String seqSearch(int t) throws FileNotFoundException
   {
Scanner fileArrayIn = new Scanner(new FileReader("Lab07Names.txt"));
String studentName = "Invalid Name";
Boolean found = false;

int id = 0;
String nNames;
while (fileArrayIn.hasNext() && !found)
{
   id = fileArrayIn.nextInt();
   nNames = fileArrayIn.nextLine();
   if (t == id)
   {

studentName = nNames;
found = true;
   }
  
}
fileArrayIn.close();
return studentName;

   }

   public static void BubbleSort(StudentGrades[] num)
   {
int max = 14;

while (max >= 0)
{
   int sub = 0;
   while (sub <= max)
   {
StudentGrades temp;
if (num[sub].getAverage() < num[sub + 1].getAverage())
{

   temp = num[sub];
   num[sub] = num[sub + 1];
   num[sub + 1] = temp;
}

sub++;

   }
   max--;

}
   }
}


public class StudentGrades
{
public StudentGrades(int studentid, int[] grades)
{
   setStudentid(studentid);
   setGrades(grades);
}

public StudentGrades ()
{
   setStudentid(0);
   setGrades(null);
}

public int getStudentID()
{
   return studentid;
}

private int studentid;
private int[] grades = new int[10];
public int getStudentid()
{
   return studentid;
}
public void setStudentid(int studentid)
{
   this.studentid = studentid;
}
public int[] getGrades()
{
   return grades;
}
public void setGrades(int[] grades)
{
   this.grades = grades;
}

public int getTotalGrade()
{
   int total = 0;
   for(int i = 0; i < grades.length; i++)
{
total += grades[i];
}
return total;
}

public int getAdjTotal()
{
int min = grades[0];
int max = grades[0];
for(int i = 1; i < grades.length; i++)
{
if (min > grades[i])
{
   min = grades[i];
}
if (max < grades[i])
{
   max = grades[i];
}
}
int total = getTotalGrade() - min + max;
return total;
}

public int getAverage()
{
int total = getAdjTotal();
double avg = total/grades.length;
return (int)Math.rint(avg);
}
}

Explanation / Answer

Hi, issue was on line 54 in Lab07.java.

You were doing something like this

reportFile.printf("%7d%7d%7.0f", arrayStudent[i].getTotalGrade(), arrayStudent[i].getAdjTotal(),
                   arrayStudent[i].getAverage());

But getTotalGrade(), getAdjTotal() and getAverage() all are returning integer values, So you cannot use &7.0f here.

You need to change above line as follows: -

reportFile.printf("%7d%7d%7d", arrayStudent[i].getTotalGrade(), arrayStudent[i].getAdjTotal(),
                   arrayStudent[i].getAverage());

This works and output in the text file is as follows: -

Lab07Report.txt

Student Grade Report

ID# Name TEST Scores TotalAdj Total Avg

1221 Korb, Barbara 91 90 75 85 90 88 79 88 91 90 867 883 88
1222 Schafer, Anne 91 90 75 85 90 88 79 88 91 90 867 883 88
1223 Kramer, Susan 91 90 75 85 90 88 79 88 91 90 867 883 88
1224 Smith, Lindy 91 90 75 85 90 88 79 88 91 90 867 883 88
1225 Caruso, Anthony 91 90 75 85 90 88 79 88 91 90 867 883 88
1226 Sokalski, Mark 91 90 75 85 90 88 79 88 91 90 867 883 88
1227 Nickerson, Philip 91 90 75 85 90 88 79 88 91 90 867 883 88
1228 Wydner, Peter 91 90 75 85 90 88 79 88 91 90 867 883 88
1343 Vickers, Jerry 91 90 75 85 90 88 79 88 91 90 867 883 88
1555 Collins, Richard 91 90 75 85 90 88 79 88 91 90 867 883 88
1856 Stanson, Luke 91 90 75 85 90 88 79 88 91 90 867 883 88
1967 Invalid Name 91 90 75 85 90 88 79 88 91 90 867 883 88
1987 Lydon, Arthur 91 90 75 85 90 88 79 88 91 90 867 883 88
1989 Kelley, Nico 91 90 75 85 90 88 79 88 91 90 867 883 88
1991 Graham, Gary 91 90 75 85 90 88 79 88 91 90 867 883 88
1993 Parsons, Michael 91 90 75 85 90 88 79 88 91 90 867 883 88

Total Students =16
Report by