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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dameon99
    New Member
    • Jun 2007
    • 57

    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 NoSuchElementEx ception(); *************** *
    else
    throw new InputMismatchEx ception();
    }




    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_VAL UE;

    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.prin tln("Program Exiting...");
    break;

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

    case 3:
    printAll(Studen ts);
    break;

    case 4:
    av = calcAvMark(Stud ents);
    break;

    case 5:
    av = calcAvMark(Stud ents);
    dispDivMarks(St udents, av);
    break;

    case 6:
    dispDistGrades( Students);
    break;

    case 7:
    findByStudNum(S tudents);
    break;

    case 8:
    findByStudName( Students);
    break;

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

    case 10:
    checkTopGrades( Students);
    break;

    case 11:
    sortStudsNum(St udents);
    break;

    case 12:
    sortStudsAlpha( Students);
    break;

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



    }while(option != 1);

    return;
    }



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



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

    return;
    }



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



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

    System.out.prin t("Please select from one of the following options ");
    System.out.prin tln("by typing the corresponding number.");
    System.out.prin tln("");

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

    option = keyBd.nextInt() ;

    return option;
    }



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



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

    setStudID(Stude nts);

    setStudName(Stu dents);

    setPracMark(Stu dents);

    setAssigMark(St udents, 1);
    setAssigMark(St udents, 2);

    setExamMark(Stu dents);

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

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

    return;
    }



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



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

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

    return;
    }



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


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

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

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

    Students[studCount].setID(studID);

    return;
    }


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



    public static void setStudName(Stu dent[] Students)
    {
    System.out.prin tln("Enter student's first name: ");
    Students[studCount].setFirstName(k eyBd.next());

    System.out.prin tln("Enter student's second name: ");
    Students[studCount].setLastName(ke yBd.next());

    return;
    }


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


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

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

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

    Students[studCount].setPracGrade(m ark);

    return;
    }



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


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

    do
    {
    System.out.prin t("Please enter mark between 0 and 100 for Assignment ");
    System.out.prin tln(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(Stu dent[] Students)
    {
    int mark = -1;

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

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

    Students[studCount].setExamGrade(m ark);

    return;
    }



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


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

    return;
    }



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



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

    if (studCount < 0)
    {
    System.out.prin tln("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.prin t("The average mark of all students is: ");
    System.out.prin tln(av);
    }
    return av;
    }



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




    public static void dispDivMarks(St udent[] 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.prin tln(above + " Students above or equal to the average grade.");
    System.out.prin tln(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.prin tln("There are no students to display record of.");
    }
    else
    {
    for(int index = 0; index <= studCount; index++)
    {
    if(Students[index].getFinGrade(). equalsIgnoreCas e("hd"))
    {
    hd_num++;
    }
    else
    {
    if(Students[index].getFinGrade(). equalsIgnoreCas e("d"))
    {
    d_num++;
    }
    else
    {
    if(Students[index].getFinGrade(). equalsIgnoreCas e("c"))
    {
    c_num++;
    }
    else
    {
    if(Students[index].getFinGrade(). equalsIgnoreCas e("p"))
    {
    p_num++;
    }
    else
    {
    n_num++;
    }
    }
    }
    }
    }
    }

    printDist(hd_nu m, 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.prin tln("Number of HDs: " + hd_num);
    System.out.prin tln("Number of Ds: " + d_num);
    System.out.prin tln("Number of Cs: " + c_num);
    System.out.prin tln("Number of Ps: " + p_num);
    System.out.prin tln("Number of Ns: " + n_num);
    System.out.prin tln("");

    return;
    }



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




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

    if(studCount < 0)
    {
    System.out.prin tln("There are no records to search");
    }
    else
    {
    System.out.prin tln("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.prin tln("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.prin tln("There are no records to search");
    }
    else
    {
    System.out.prin tln("Please enter Student first name: ");
    fName = keyBd.next();

    System.out.prin tln("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.prin t("No records exist under the given name: ");
    System.out.prin tln(fName + " " + lName);
    }
    }
    return;
    }



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



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

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

    if (studCount < 0)
    {
    System.out.prin tln("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.prin tln("Top Mark was: " + highest + "%");
    System.out.prin tln("From Student: " + topStud);
    System.out.prin tln("");

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



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



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

    if(studCount < 0)
    {
    System.out.prin tln("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.prin tln("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.prin tln(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.prin tln("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.compar eTo(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.prin tln(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.
    Last edited by r035198x; Nov 2 '07, 07:12 AM. Reason: Removed code tags believe it or not
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by Dameon99
    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 NoSuchElementEx ception(); *************** *
    else
    throw new InputMismatchEx ception();
    }

    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?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Dameon99
      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

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by JosAH
        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.

        Comment

        Working...