import java.io.*;
import java.util.*;
public class ClassGrades
{
static final int NUM_STUDENTS = 50;
public static void main(String[] args) throws FileNotFoundException
{
int sum = 0;
double average = 0;
int maxIndex = 0;
int lowestIndex = 100;
Scanner inFile = new Scanner(new FileReader("testScores.txt"));
String[] students = new String[NUM_STUDENTS];
int[] grades = new int[NUM_STUDENTS];
for (int i = 0; i < students.length; i++)
{
students[i] = inFile.next();
grades[i] = inFile.nextInt();
sum = sum + grades[i];
if (students.length != 0)
{
average = sum / students.length;
}
}
15 Replies - 339 Views - Last Post: 04 February 2012 - 02:41 PM
Topic Sponsor:
#1
Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:25 PM
I am very new to Java programming and I am not sure why this error is happening, and what is really causing it. I am trying to import names and grades from a file into 2 separate arrays. I the file does exist and is in the correct place, but I do not know what the problem it. I am getting the error in the title of this post. Let me know if you see where I am going wrong.
Replies To: Exception in thread "main" java.util.NoSuchElementException
#2
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:28 PM
Good first post. You figured out code tags and that's nice. The next thing you can do that's helpful is to post the exact error message as it appears, copied and pasted into your post. If you can't edit your existing post, add a new reply with the error and its trace.
#3
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:30 PM
This is the error messages I am getting.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ClassGrades.main(ClassGrades.java:24)
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at ClassGrades.main(ClassGrades.java:24)
#4
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:42 PM
Just an advice, before reading anything try to check if a scanner has next item to read. You can use hasNext(), hasNextInt()... an so to check according to what you want to read.
Also can we see what the file contents looks like?
Also can we see what the file contents looks like?
#5
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:42 PM
Is your file empty? I think your error is because what you've programmed to be read from the file isn't there. If the file does have something in it, then what's there does not match what your program statements are expecting.
#6
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:48 PM
Here is the files contents I am using for input
Mark 88
Chelsea 80
Richard 89
Jordan 73
Albert 90
Seth 93
It is in a file named "testScores.txt"
And since I am using eclipse it is located in the project folder.
Mark 88
Chelsea 80
Richard 89
Jordan 73
Albert 90
Seth 93
It is in a file named "testScores.txt"
And since I am using eclipse it is located in the project folder.
#7
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:55 PM
That is why I said you need to look if there is any next item to read before reading. Look at scanner class documentation for hasNext..() methods.
Your file has 6 lines and you try to loop 50 times!!! That is why you ge that error in the sixth iteration because there is no next item to read!!
Your file has 6 lines and you try to loop 50 times!!! That is why you ge that error in the sixth iteration because there is no next item to read!!
#8
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 12:57 PM
But the question I am working on tells me to have a max of 50 students. Where would that go if not where I have it at the beginning as my static variable?
#9
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 01:06 PM
I am not complaining about your fifty max value, I am complaining about trying to read 50 students from a file of 6! this is what this loop is doing:
For any reason, you have to check for the next item before reading from file...
for (int i = 0; i < students.length; i++) // because the size of array is 50
For any reason, you have to check for the next item before reading from file...
#10
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 01:16 PM
So with my code, I would need to use the "hasNext()" method to check to see if there is anything left in the file after each input? I am not sure how I would go about this though because like I said, I am just starting out with Java Programming. If this is not how I would do it, can you explain how I would have to do it? (Just a brief overview)
#11
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 01:25 PM
You're thinking correctly. If you're unsure how to use the pearls smohd has given you, look at the Scanner API, Scanner.hasNext() in particular, look for examples where Scanner is used to read from a file (they should all use hasNext() ), and/or check out the Java file I/O tutorials. Try to code what you learn from those places and come back when you need more help.
#12
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 01:26 PM
Alright thank you both for the help. It was greatly appreciated.
#13
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 01:55 PM
After taking your advice and actually reading the book I own about programming, I have been able to get my program to search for the file and see if there is another String or number to retrieve. Unfortunately, I have run into another problem. My sum and average lines of code are not calculating as I need them to. After playing with them for a little bit I wrote the code according to my textbook. Every time I run the program, I get an average of 0.0, which is not the correct average. The file I am using for input is as follows:
Mark 88
Chelsea 80
Richard 89
Jordan 73
Albert 90
Seth 93
I follow my code and I must be missing something because I can not determine the problem even after moving several lines of code around. Maybe I am missing something. Any insight would be much appreciated.
Mark 88
Chelsea 80
Richard 89
Jordan 73
Albert 90
Seth 93
I follow my code and I must be missing something because I can not determine the problem even after moving several lines of code around. Maybe I am missing something. Any insight would be much appreciated.
import java.io.*;
import java.util.*;
public class ClassGrades
{
static final int NUM_STUDENTS = 50;
public static void main(String[] args) throws FileNotFoundException
{
int sum = 0;
double average = 0;
int maxIndex = 0;
int lowestIndex = 0;
Scanner inFile = new Scanner(new FileReader("testScores.txt"));
String[] students = new String[NUM_STUDENTS];
int[] grades = new int[NUM_STUDENTS];
for (int i = 0; i < students.length; i++)
{
grades[maxIndex] = 0;
grades[lowestIndex] = 100;
sum = sum + grades[i];
if (students.length != 0)
{
average = sum / students.length;
}
while (inFile.hasNext())
{
students[i] = inFile.next();
}
while (inFile.hasNextInt())
{
grades[i] = inFile.nextInt();
}
if (grades[maxIndex] < grades[i])
{
maxIndex = i;
}
if (grades[lowestIndex] > grades[i])
{
lowestIndex = i;
}
}
for (int i = 0; i < students.length; i++)
{
}
System.out.println("Class Average: " + average);
inFile.close();
}
}
#14
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 02:01 PM
Two words: Integer math. You can't get a fraction by dividing one integer by another. If you cast one of them to double before the division or use a decimal constant in the equation ( like 20.0 ), it will work - as long as you're not assigning the result to another integer.
#15
Re: Exception in thread "main" java.util.NoSuchElementException
Posted 04 February 2012 - 02:08 PM
Even with a simple print statement, it is saying the my sum is equaling 100, I think something else might be wrong with my code?
|
|

New Topic/Question
Reply



MultiQuote





|