By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

desperate help needed! java.util.Scanner weird error.

P: 57
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the class: (one I added all the asteriss to.



// If we are at the end of input then NoSuchElement;
// If there is still input left then InputMismatch
private void throwFor() {
skipped = false;
if ((sourceClosed) && (position == buf.limit()))
throw new NoSuchElementException(); ****************
else
throw new InputMismatchException();
}




My code is also below. Please ignore the Class for the student objects and assume all is working there.


import java.util.*;
import java.lang.*;




/*----------------------------------------------------------------------------*/


public class StudentGrade
{

final static int STUD_MAX = 100;
final static int MAX_INT = Integer.MAX_VALUE;

private static int studCount = -1;

private static Scanner keyBd = new Scanner(System.in);



public static void main(String [] args)
{
int option = 0;
double av = 0.0;

boolean func = false;

studentInfo();

Student[] Students = new Student[STUD_MAX];
for(int index = 0; index < STUD_MAX; index++)
{
Students[index] = new Student(0);
}

do
{
option = dispMenu();

switch (option)
{
case 1:
System.out.println("Program Exiting...");
break;

case 2:
func = true;
createNewStud(Students, func);
break;

case 3:
printAll(Students);
break;

case 4:
av = calcAvMark(Students);
break;

case 5:
av = calcAvMark(Students);
dispDivMarks(Students, av);
break;

case 6:
dispDistGrades(Students);
break;

case 7:
findByStudNum(Students);
break;

case 8:
findByStudName(Students);
break;

case 9:
func = false;
createNewStud(Students, func);
break;

case 10:
checkTopGrades(Students);
break;

case 11:
sortStudsNum(Students);
break;

case 12:
sortStudsAlpha(Students);
break;

default:
System.out.println("Incorrect input. Try again.");
}



}while(option != 1);

return;
}



/*------------------------------------------------------------------------*/



public static void studentInfo()
{
System.out.println("Name: N/A.");
System.out.println("Student Number: N/A.");
System.out.println("Enrolment mode: Internal.");
System.out.println("Tutorial Day: Tuesday.");
System.out.println("Time: 4:30pm.");
System.out.println("Tutor: Dr Piara Dhilon.");
System.out.println("");

return;
}



/*------------------------------------------------------------------------*/



public static int dispMenu()
{
int option = 0;

System.out.print("Please select from one of the following options ");
System.out.println("by typing the corresponding number.");
System.out.println("");

System.out.println("1) Quit Program.");
System.out.println("2) Add a new Student and determine their grade.");
System.out.println("3) View information on all students entered.");
System.out.println("4) View the average mark of all students.");
System.out.print("5) Display the quantities of students above ");
System.out.println("or below the average mark.");
System.out.println("6) Display the distribution of all student's grades.");
System.out.print("7) View all details of a student chosen with their ");
System.out.println("student identification number.");
System.out.print("8) View all details of a student chosen with their ");
System.out.println("full name.");
System.out.println("9) Add a new student and determine if they already exist.");
System.out.println("10) Find the students with the top two marks.");
System.out.println("11) Sort students by student ID and view new order.");
System.out.print("12) Sort students into ascending alphabetical order ");
System.out.println("and view new order.");
System.out.println("");

option = keyBd.nextInt();

return option;
}



/*------------------------------------------------------------------------*/



public static void createNewStud(Student[] Students, boolean func)
{
studCount++;

setStudID(Students);

setStudName(Students);

setPracMark(Students);

setAssigMark(Students, 1);
setAssigMark(Students, 2);

setExamMark(Students);

Students[studCount].setFinMark();
Students[studCount].setFinGrade();

if (func == true)
{
System.out.println("Students grade is: " + Students[studCount].getFinGrade());
}
else
{
detStudExist(Students);
}

return;
}



/*------------------------------------------------------------------------*/



public static void detStudExist(Student[] Students)
{
int checkCount = 0;

for (int index = 0; index < studCount; index++)
{
if((Students[index].checkIfSame(Students[studCount])) && (checkCount < 1))
{
System.out.println("Student already exists! ");
studCount--;
}
}

return;
}



/*------------------------------------------------------------------------*/


public static void setStudID(Student[] Students)
{
int studID = 0;

do
{
System.out.println("Please enter the new students ID number: ");
studID = keyBd.nextInt();

if (studID <= 0)
{
System.out.println("Error, not a valid Student ID");
}
}while(studID <= 0);

Students[studCount].setID(studID);

return;
}


/*------------------------------------------------------------------------*/



public static void setStudName(Student[] Students)
{
System.out.println("Enter student's first name: ");
Students[studCount].setFirstName(keyBd.next());

System.out.println("Enter student's second name: ");
Students[studCount].setLastName(keyBd.next());

return;
}


/*------------------------------------------------------------------------*/


public static void setPracMark(Student[] Students)
{
int mark = -1;

do
{
System.out.println("Please enter student practical mark between 0 and 100: ");
mark = keyBd.nextInt();

}while((mark < 0) ||(mark > 100));

Students[studCount].setPracGrade(mark);

return;
}



/*------------------------------------------------------------------------*/


public static void setAssigMark(Student[] Students, int assigNum)
{
int mark = -1;

do
{
System.out.print("Please enter mark between 0 and 100 for Assignment ");
System.out.println(assigNum + ": ");
mark = keyBd.nextInt();

}while((mark < 0) ||(mark > 100));

switch (assigNum)
{
case 1:
Students[studCount].setAssig1Mark(mark);
break;
case 2:
Students[studCount].setAssig2Mark(mark);
break;
}
return;
}


/*------------------------------------------------------------------------*/


public static void setExamMark(Student[] Students)
{
int mark = -1;

do
{
System.out.println("Please enter mark between 0 and 100 the exam: ");
mark = keyBd.nextInt();

}while((mark < 0) ||(mark > 100));

Students[studCount].setExamGrade(mark);

return;
}



/*------------------------------------------------------------------------*/


public static void printAll(Student[] Students)
{
if (studCount < 0)
{
System.out.println("There are no students currently to display.");
}
else
{
for (int index = 0; index <= studCount; index++)
{
Students[index].outputStudData();
System.out.println("");
System.out.println("");
}
}

return;
}



/*------------------------------------------------------------------------*/



public static double calcAvMark(Student[] Students)
{
int totMark = 0;
double av = 0.0;

if (studCount < 0)
{
System.out.println("There is no students to calculate the average of. ");
}
else
{
for (int index = 0; index <= studCount; index++)
{
totMark += Students[index].getFinMark();
}
av = totMark / (studCount + 1);

System.out.print("The average mark of all students is: ");
System.out.println(av);
}
return av;
}



/*------------------------------------------------------------------------*/




public static void dispDivMarks(Student[] Students, double av)
{
int above = 0; // Above includes 'equal to'.
int below = 0;
double mark = 0.0;

if (av != 0)
{
for (int index = 0; index <= studCount; index++)
{
mark = Students[index].getFinMark();

if (mark >= av)
{
above++;
}
else
{
below++;
}
}
}
System.out.println(above + " Students above or equal to the average grade.");
System.out.println(below + " Students below the average grade.");

return;
}



/*------------------------------------------------------------------------*/



public static void dispDistGrades(Student[] Students)
{
int hd_num = 0;
int d_num = 0;
int c_num = 0;
int p_num = 0;
int n_num = 0;

if(studCount < 0)
{
System.out.println("There are no students to display record of.");
}
else
{
for(int index = 0; index <= studCount; index++)
{
if(Students[index].getFinGrade().equalsIgnoreCase("hd"))
{
hd_num++;
}
else
{
if(Students[index].getFinGrade().equalsIgnoreCase("d"))
{
d_num++;
}
else
{
if(Students[index].getFinGrade().equalsIgnoreCase("c"))
{
c_num++;
}
else
{
if(Students[index].getFinGrade().equalsIgnoreCase("p"))
{
p_num++;
}
else
{
n_num++;
}
}
}
}
}
}

printDist(hd_num, d_num, c_num, p_num, n_num);

return;
}



/*------------------------------------------------------------------------*/



public static void printDist(int hd_num, int d_num, int c_num, int p_num, int n_num)
{
System.out.println("Number of HDs: " + hd_num);
System.out.println("Number of Ds: " + d_num);
System.out.println("Number of Cs: " + c_num);
System.out.println("Number of Ps: " + p_num);
System.out.println("Number of Ns: " + n_num);
System.out.println("");

return;
}



/*------------------------------------------------------------------------*/




public static void findByStudNum(Student[] Students)
{
int IDCheck = 0;
boolean IDExist = false;

if(studCount < 0)
{
System.out.println("There are no records to search");
}
else
{
System.out.println("Please enter Student ID Number for search: ");
IDCheck = keyBd.nextInt();

for(int index = 0; index <= studCount; index++)
{
if(Students[index].getID() == IDCheck)
{
IDExist = true;

Students[index].outputStudData();
}
}

if(IDExist != true)
{
System.out.println("No records exist under the number " + IDCheck);
}
}
return;
}



/*------------------------------------------------------------------------*/



public static void findByStudName(Student[] Students)
{
String fName = "";
String lName = "";

boolean studExist = false;

if(studCount < 0)
{
System.out.println("There are no records to search");
}
else
{
System.out.println("Please enter Student first name: ");
fName = keyBd.next();

System.out.println("Enter Student last name: ");
lName = keyBd.next();

for(int index = 0; index <= studCount; index++)
{
if((Students[index].getFirstName() == fName) && (Students[index].getLastName() == lName))
{
studExist = true;

Students[index].outputStudData();
}
}

if (studExist != true)
{
System.out.print("No records exist under the given name: ");
System.out.println(fName + " " + lName);
}
}
return;
}



/*------------------------------------------------------------------------*/



public static void checkTopGrades(Student[] Students)
{
double highest = 0;
double second = 0;

String topStud = "";
String secStud = "";

if (studCount < 0)
{
System.out.println("No records to search through!");
}
else
{
for(int index = 0; index <= studCount; index++)
{
if (Students[index].getFinMark() > highest)
{
highest = Students[index].getFinMark();
topStud = "" + Students[index].getFirstName() + " " + Students[index].getLastName();
}
}

for(int index1 = 0; index1 <= studCount; index1++)
{
if ((Students[index1].getFinMark() > second) && (Students[index1].getFinMark() != highest))
{
second = Students[index1].getFinMark();
secStud = "" + Students[index1].getFirstName() + " " + Students[index1].getLastName();
}
}

System.out.println("Top Mark was: " + highest + "%");
System.out.println("From Student: " + topStud);
System.out.println("");

System.out.println("Second highest mark is: " + second + "%");
System.out.println("From Student " + secStud);
System.out.println("");
}
return;
}



/*------------------------------------------------------------------------*/



public static void sortStudsNum(Student[] Students)
{
int lowest = MAX_INT;
int newLoc = 0;

if(studCount < 0)
{
System.out.println("There is no data to sort.");
}
else
{
Student[] Students2 = new Student[STUD_MAX];
for(int index = 0; index < STUD_MAX; index++)
{
Students2[index] = new Student(0);
}

System.out.println("Sorted student ID numbers: ");

for(int index1 = 0; index1 <= studCount; index1++)
{
for (int index2 = 0; index2 <= studCount; index2++)
{
if(Students[index2].getID() < lowest)
{
lowest = Students[index2].getID();
newLoc = index2;
}
}

System.out.println(lowest);
Students2[index1] = Students[newLoc];
}
}
return;
}



/*------------------------------------------------------------------------*/



public static void sortStudsAlpha(Student[] Students)
{
String lName = "";
String lastName = "";

int equiv = 0;
int index = 0;
int index2 = 0;

Student temp = new Student(0);

if (studCount < 0)
{
System.out.println("No student entries to sort.");
}
else
{
lastName = Students[index2].getLastName();

for(index = 0; index <= studCount; index++)
{

for (index2 = 0; index2 <= studCount; index2++)
{
lName = Students[index2].getLastName().toLowerCase();
equiv = lastName.compareTo(lName);

if (equiv < 0)
{
temp = Students[index2];
Students[index2] = Students[index2 -1];
Students[index2 -1] = temp;
}
}
}
for(int index3 = 0; index3 <= studCount; index3++)
{
System.out.println(Students[index3].getLastName());
}
}
return;
}




Please, please, please please please can someone help me!? Im usually very good at java but this one has me stumped.
Nov 2 '07 #1
Share this Question
Share on Google+
3 Replies


RedSon
Expert 5K+
P: 5,000
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the class: (one I added all the asteriss to.

// If we are at the end of input then NoSuchElement;
// If there is still input left then InputMismatch
private void throwFor() {
skipped = false;
if ((sourceClosed) && (position == buf.limit()))
throw new NoSuchElementException(); ****************
else
throw new InputMismatchException();
}

Please, please, please please please can someone help me!? Im usually very good at java but this one has me stumped.
I searched your code and did not find a method called throwFor, except for where you pasted it up top. Who calls throwFor() where is it in your code?
Nov 2 '07 #2

Expert 10K+
P: 11,448
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the class: (one I added all the asteriss to.
That was simply too much code to dig through. What was the stack trace when
that Exception was thrown?

kind regards,

Jos
Nov 2 '07 #3

RedSon
Expert 5K+
P: 5,000
That was simply too much code to dig through. What was the stack trace when
that Exception was thrown?

kind regards,

Jos
Yes, perhaps you can edit down your code to only show us what is important since we do not care about the rest.
Nov 2 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.