Note that there are some explanatory texts on larger screens.

plurals
  1. POReading Data from Multiple txt Files in Java
    primarykey
    data
    text
    <p>I'm a beginner taking Intro to Computer Science and I'm working with java. I get the following error in this program when trying to read from multiple txt files in the getLetterGrade method:</p> <p>Exception in thread "main" java.lang.IllegalStateException: Scanner closed</p> <pre><code>import java.util.Scanner; import java.io.*; public class Exam { private int grade; private float examAvg; private String name; private File exam1Data; private File exam2Data; private File namesData; private Scanner inFileExam1; private Scanner inFileExam2; private Scanner inFileName; //constructor public Exam() throws IOException { exam1Data = new File("exam1.txt"); exam2Data = new File("exam2.txt"); namesData = new File("names.txt"); inFileExam1 = new Scanner(exam1Data); inFileExam2 = new Scanner(exam2Data); inFileName = new Scanner(namesData); } public float getExam1Avg() { examAvg = 0; while (inFileExam1.hasNext()) { grade = inFileExam1.nextInt(); examAvg += grade; } inFileExam1.close(); examAvg = (float)(examAvg / 25.0); return examAvg; } public float getExam2Avg() { examAvg = 0; while (inFileExam2.hasNext()) { grade = inFileExam2.nextInt(); examAvg += grade; } inFileExam2.close(); examAvg = (float)(examAvg / 25.0); return examAvg; } //method that finds the letter grade given a student name and //the exam number public char getLetterGrade(String inputName, int examNum) { char letter; int count = 1; //inputName = inputName.toLowerCase(); do { if (inFileName.hasNext()){ name = inFileName.nextLine(); } count++; } while (inFileName.hasNext() &amp;&amp; !name.equalsIgnoreCase(inputName)); inFileName.close(); if (!name.equalsIgnoreCase(inputName)) { return 'x'; } if (examNum == 1) { for (int i = 1; i &lt;= count &amp;&amp; inFileExam1.hasNextInt(); i++) { grade = inFileExam1.nextInt(); } inFileExam1.close(); } else if (examNum == 2) { for (int i = 1; i &lt;= count &amp;&amp; inFileExam2.hasNextInt(); i++) { grade = inFileExam2.nextInt(); } inFileExam2.close(); } else { return 'x'; } if (grade &gt;= 90) { letter = 'A'; } else if (grade &lt; 90 &amp;&amp; grade &gt;= 80) { letter = 'B'; } else if (grade &lt; 80 &amp;&amp; grade &gt;= 70) { letter = 'C'; } else if (grade &lt; 70 &amp;&amp; grade &gt;= 60) { letter = 'D'; } else { letter = 'F'; } return letter; } } </code></pre> <p>The error occurs at the following line:</p> <pre><code>for (int i = 1; i &lt;= count &amp;&amp; inFileExam1.hasNextInt(); i++) { </code></pre> <p>Please help!!</p> <p>Here is the main class:</p> <pre><code>import java.util.Scanner; import java.io.*; public class ExamAverages { public static void main(String[] args) throws IOException { Exam exam = new Exam(); System.out.println(exam.getExam1Avg()); System.out.println(exam.getExam2Avg()); System.out.println(exam.getLetterGrade("name", 1)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload