I\'ve been working on this lab for a a bit and I thought I was done but I keep g
ID: 3833597 • Letter: I
Question
I've been working on this lab for a a bit and I thought I was done but I keep getting this error when I try to run the program:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Lab09.main(Lab09.java:67)
I'm not sure what I'm doing wrong. The error line is in bold. Can anyone take a look and fix the error?
/Lab09
//this makes available all extra utilities from Java library including scanner
import java.util.*;
//this makes available all extras from Java library needed for files
import java.io.*; //needed for files
public class Lab09
{//start of class
public static void main(String[] args) throws FileNotFoundException
{//start of main method
//declare a variable of type Scanner
Scanner infileName = new Scanner(new FileReader("Lab09StudentFile.txt"));
PrintWriter outfileName = new PrintWriter ("Lab09Report.txt");
//variables and defined constants
String studentName; //need studentName for searchName variable
int stdId; //student id number
int[] scores = new int[10]; //test scores
int x = 0; //variable for a single instance of the student class
int student = 0; //student variable initialized to zero
Student[] arrayStudent = new Student[16]; //array of student class objects
StudentSort[] arrayStudentSort = new StudentSort[16];
// arrays to store their ids and names
int[] ids = new int[20];
String[] names = new String[20];
//create an instance of printwriter object
outfileName.println("Student Grade Report");
outfileName.println();
outfileName.println("ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg");
outfileName.println();
//read score until end of file
while(infileName.hasNext())
{//start while loop
stdId = infileName.nextInt();
for (int i = 0; i < scores.length; i++)
{
scores[i] = infileName.nextInt();
}
Student std = new Student(stdId, scores);
arrayStudent[x] = std;
//studentName = searchName(stdId); //assigning searchName to studentName
studentName = binarySearch(ids, names,std.getStudentId());
StudentSort stdObject = new StudentSort(x, studentName); //created StudentSort array, used x (stdId) to search for name
arrayStudentSort[x] = stdObject; //arrayStudentSort inserts stdObject into the array
x++;
student++;
}//end while
//loading name and id
int c = 0;
Scanner nameFileScanner = new Scanner (new FileReader("Lab09Names.txt"));
while (nameFileScanner.hasNext())
{//start while loop
int id = infileName.nextInt();
String name = infileName.nextLine().trim();
ids[c] = id;
names[c] = name;
c++;
}//end while
nameFileScanner.close();
//calls selectionSort method
selectionSort(arrayStudentSort);
for (int j = 0; j < arrayStudent.length; j++)
{//start for loop
StudentSort outObject = arrayStudentSort[j];
int k = outObject.getStudentSub();
Student std = arrayStudent[k];
outfileName.printf("%-5d", std.getStudentId());
scores = std.getStudentGradeArray();
outfileName.printf("%-20s", outObject.getStudentName());
for (int i = 0; i < scores.length; i++)
{
outfileName.printf("%-5d", scores[i]);
}
//write total, ajdusted total and adjusted average
outfileName.printf("%8.0f%8.0f%8.0f",
std.getTotal(),
std.getAdjustedtotal(),
std.getAdjustedAverage());
outfileName.println();
}//end for loop
outfileName.println();
outfileName.printf ("Total Students = %2d", student);
infileName.close();
outfileName.close();
}//end of main
private static void bubbleSort(Student[] arrayStudent)
{//begin method
for (int i = 0; i < arrayStudent.length; i++)
{
for (int j = 0; j < arrayStudent.length-1-i; j++)
{
if (arrayStudent[j].getAdjustedAverage() >arrayStudent[j+1].getAdjustedAverage())
{
Student temp = new Student();
//set arrayStudent at index=j to temp object
temp.setStudentId(arrayStudent[j].getStudentId());
temp.setStudentGradeArray(arrayStudent[j].getStudentGradeArray());
//set arrayStudent at index=j+1 to arrayStudent object
arrayStudent[j].setStudentId(arrayStudent[j+1].getStudentId());
arrayStudent[j].setStudentGradeArray(arrayStudent[j+1].getStudentGradeArray());
//set arrayStudent at index=j+1 to temp object
arrayStudent[j+1].setStudentId(temp.getStudentId());
arrayStudent[j+1].setStudentGradeArray(temp.getStudentGradeArray());
}
}
}
}//end method
public static String searchName(int searchNumber) throws FileNotFoundException
{//begin method
Scanner infileName = new Scanner (new FileReader("Lab09Names.txt"));
boolean found = false;
String returnName = "name not found";
while (infileName.hasNext() && found == false)
{//start while loop
int stdId = infileName.nextInt();
String searchName = infileName.nextLine().trim();
{
if(searchNumber == stdId)
{
returnName = searchName;
found = true;
}
}
}//end while
infileName.close();
return returnName;
}//end of method
//function to sort StudentSort array
public static void selectionSort(StudentSort[] sortStudent)
{//begin method
int n = sortStudent.length;
//One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
//Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (sortStudent[j].getStudentName().compareTo(sortStudent[min_idx].getStudentName()) < 0)
min_idx = j;
//Swap the found minimum element with the first element
StudentSort temp = sortStudent[min_idx];
sortStudent[min_idx] = sortStudent[i];
sortStudent[i] = temp;
}
}//end method
public static String binarySearch(int[] ids, String[] names, int id)
{
int start = 0;
int end = ids.length - 1;
String returnName = "name not found";
while (start <= end)
{
int mid = (start + end) / 2;
if (id == ids[mid])
{
return names[mid];
}
if (id < ids[mid])
{
end = mid - 1;
} else
{
start = mid + 1;
}
}
return returnName;
}
}//end of class
Explanation / Answer
Hi, I have foxed the issue. Please let me know in case of any issue.
Actuaalu , you have created: Scanner nameFileScanner = new Scanner (new FileReader("Lab06Names.txt"));
and you were using different scanner object: int id = infileName.nextInt();
//Lab09
//this makes available all extra utilities from Java library including scanner
import java.util.*;
//this makes available all extras from Java library needed for files
import java.io.*; //needed for files
public class Lab09
{//start of class
public static void main(String[] args) throws FileNotFoundException
{//start of main method
//declare a variable of type Scanner
Scanner infileName = new Scanner(new FileReader("Lab08StudentFile.txt"));
PrintWriter outfileName = new PrintWriter ("Lab08Report.txt");
//variables and defined constants
String studentName; //need studentName for searchName variable
int stdId; //student id number
int[] scores = new int[10]; //test scores
int x = 0; //variable for a single instance of the student class
int student = 0; //student variable initialized to zero
Student[] arrayStudent = new Student[16]; //array of student class objects
StudentSort[] arrayStudentSort = new StudentSort[16];
// arrays to store their ids and names
int[] ids = new int[20];
String[] names = new String[20];
//create an instance of printwriter object
outfileName.println("Student Grade Report");
outfileName.println();
outfileName.println("ID# Name /-------------------Test Scores----------------/ Total AdjTotal Avg");
outfileName.println();
//read score until end of file
while(infileName.hasNext())
{//start while loop
stdId = infileName.nextInt();
for (int i = 0; i < scores.length; i++)
{
scores[i] = infileName.nextInt();
}
Student std = new Student(stdId, scores);
arrayStudent[x] = std;
//studentName = searchName(stdId); //assigning searchName to studentName
studentName = binarySearch(ids, names,std.getStudentId());
StudentSort stdObject = new StudentSort(x, studentName); //created StudentSort array, used x (stdId) to search for name
arrayStudentSort[x] = stdObject; //arrayStudentSort inserts stdObject into the array
x++;
student++;
}//end while
// loading name and id
int c = 0;
Scanner nameFileScanner = new Scanner (new FileReader("Lab06Names.txt"));
while (nameFileScanner.hasNext())
{//start while loop
int id = nameFileScanner.nextInt();
String name = nameFileScanner.nextLine().trim();
ids[c] = id;
names[c] = name;
c++;
}//end while
nameFileScanner.close();
//calls selectionSort method
selectionSort(arrayStudentSort);
for (int j = 0; j < arrayStudent.length; j++)
{//start for loop
StudentSort outObject = arrayStudentSort[j];
int k = outObject.getStudentSub();
Student std = arrayStudent[k];
outfileName.printf("%-5d", std.getStudentId());
scores = std.getStudentGradeArray();
outfileName.printf("%-20s", outObject.getStudentName());
for (int i = 0; i < scores.length; i++)
{
outfileName.printf("%-5d", scores[i]);
}
//write total, ajdusted total and adjusted average
outfileName.printf("%8.0f%8.0f%8.0f",
std.getTotal(),
std.getAdjustedtotal(),
std.getAdjustedAverage());
outfileName.println();
}//end for loop
outfileName.println();
outfileName.printf ("Total Students = %2d", student);
infileName.close();
outfileName.close();
}//end of main
private static void bubbleSort(Student[] arrayStudent)
{//begin method
for (int i = 0; i < arrayStudent.length; i++)
{
for (int j = 0; j < arrayStudent.length-1-i; j++)
{
if (arrayStudent[j].getAdjustedAverage() >arrayStudent[j+1].getAdjustedAverage())
{
Student temp = new Student();
//set arrayStudent at index=j to temp object
temp.setStudentId(arrayStudent[j].getStudentId());
temp.setStudentGradeArray(arrayStudent[j].getStudentGradeArray());
//set arrayStudent at index=j+1 to arrayStudent object
arrayStudent[j].setStudentId(arrayStudent[j+1].getStudentId());
arrayStudent[j].setStudentGradeArray(arrayStudent[j+1].getStudentGradeArray());
//set arrayStudent at index=j+1 to temp object
arrayStudent[j+1].setStudentId(temp.getStudentId());
arrayStudent[j+1].setStudentGradeArray(temp.getStudentGradeArray());
}
}
}
}//end method
public static String searchName(int searchNumber) throws FileNotFoundException
{//begin method
Scanner infileName = new Scanner (new FileReader("Lab08Names.txt"));
boolean found = false;
String returnName = "name not found";
while (infileName.hasNext() && found == false)
{//start while loop
int stdId = infileName.nextInt();
String searchName = infileName.nextLine().trim();
{
if(searchNumber == stdId)
{
returnName = searchName;
found = true;
}
}
}//end while
infileName.close();
return returnName;
}//end of method
//function to sort StudentSort array
public static void selectionSort(StudentSort[] sortStudent)
{//begin method
int n = sortStudent.length;
//One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
//Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (sortStudent[j].getStudentName().compareTo(sortStudent[min_idx].getStudentName()) < 0)
min_idx = j;
//Swap the found minimum element with the first element
StudentSort temp = sortStudent[min_idx];
sortStudent[min_idx] = sortStudent[i];
sortStudent[i] = temp;
}
}//end method
public static String binarySearch(int[] ids, String[] names, int id) {
int start = 0;
int end = ids.length - 1;
String returnName = "name not found";
while (start <= end) {
int mid = (start + end) / 2;
if (id == ids[mid]) {
return names[mid];
}
if (id < ids[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
return returnName;
}
}//end of class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.