472,791 Members | 1,086 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

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

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
3 2324
RedSon
5,000 Expert 4TB
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
JosAH
11,448 Expert 8TB
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
5,000 Expert 4TB
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

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

Similar topics

3
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
9
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
5
by: Tank | last post by:
I have had this post up here when i was trying to figure out how to make leading zeros and have been able to fudge that to work. I am now have trouble getting the loop that makes the folders to...
24
by: Ministry Of Jute | last post by:
I returned home from work today to find an Airborne Express Letter Express mailer leaning up against my apartment door. The return addressee was Microsoft Suite 300 1165 Eastlake Avenue E...
2
by: Arthur | last post by:
Given the coordinates of a point on the screen (such as 185,185), how do you get the color of said point using VB.NET 2003. I have looked EVERYWHERE for this, but to no avail. All the examples...
2
by: Chris M. Gamble | last post by:
I am trying to perform what I best understand as Multi-master asynchronous replication for postgres 7.3.3 servers. After researching, I tried the pgReplication project (and made the simple...
21
by: Peter | last post by:
Good luck learning quite a new language, good luck with your 21MB+ programs, good luck with the distribution of your applications, good luck when Microsoft releases another new version of...
12
by: Rene | last post by:
Anyone knows what in the world do I have to do to prevent a form from ever getting the focus? I tried using the IMessageFilter interface like this.: public bool PreFilterMessage(ref Message m)...
7
by: Ladysniper | last post by:
DESPERATE doesn't begin to describe it. Can someone PLEASE tell me what is WRONG with this code? Now..a bit of background. It is a search results page for http://www.azsoma.info/directory.htm....
0
by: BLUE | last post by:
Psion WorkAbout Pro with Windows CE .NET 4.2 Dealer gave me PowerAPIOn.exe and PowerAPIOff.exe to turn on/off tag reader, but I want to do it programmatically and do the same with WiFi and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.