简体   繁体   中英

arrays of references throwing errors related to scanner

Here's the code in question:

 public class GradeBookRunner
 {
 public static void main( String args[] )
  {
    out.println("Welcome to the Class Stats program!");
    int num = 1;
    int count = 0;
    String stuGrades = "";
    String stuName = "";
    Scanner keyboard = new Scanner(System.in);
    out.print("What is the name of this class? ");
    String clsName = keyboard.nextLine();
    out.println("\n");
    out.print("How many students are in this class? ");
    int clsNum = keyboard.nextInt();
    Class CSAB = new Class(clsName, clsNum);
    out.println("\n");
    out.print("Enter the name of student 1 : ");
    stuName = keyboard.nextLine();
    keyboard.next();
    out.print("Enter the grades for " + stuName + "\nUse the format x - grades ( 2 - 100 100) : ");
    //count = keyboard.nextInt();
    //keyboard.next();
    //for(int i = 0; i < count; i++)
    //{
        stuGrades += keyboard.nextLine() + " ";
    //}

    Student add = new Student(stuName,stuGrades);
    CSAB.addStudent(num, add);
    stuGrades = "";
    num++;
    out.println();

The code for the classes that this utilizes can be found at the following links:

Grades.java: http://pastebin.com/ahYRS2WD

Student.java: http://pastebin.com/EBF4BBCb

Class.java: http://pastebin.com/A1C9fCL1

My issue happens after I run the code and give input for the class name, class size, student name, those are all fine, it's this next bit that throws me an error, when I input the grades for the student such as 3 - 70.2 65.3 45.1

EDIT Here's the error I'm getting:

Welcome to the Class Stats program! What is the name of this class? ap

How many students are in this class? 1

Enter the name of student 1 : hardly

Enter the grades for hardly

Use the format x - grades ( 2 - 100 100) : Exception in thread "main"

java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at lab19b.Grades.setGrades(Grades.java:31)

at lab19b.Grades.<init>(Grades.java:25)

at lab19b.Student.<init>(Student.java:27)

at lab19b.GradeBookRunner.main(GradeBookRunner.java:42)

EDIT2

I'm now thinking the issue is that I need stuGrades to read in the entire line such as 3 - 70.2 65.3 45.1 but I can't figure out which scanner statement I need to put the keyboard input to a string

EDIT3

Here's my evidence of a properly working grades.java :

package lab19b;

import java.util.Arrays;
import java.util.Scanner;
import static java.lang.System.*;
import static java.util.Arrays.*;

 public class GradesTester
 {
public static void main( String args[] )
 {
    Grades test = new Grades("5 - 90 85 95.5 77.5 88");
    out.println(test);
    out.println("sum = " + test.getSum());  
    out.println("num grades = " + test.getNumGrades());                                         
    out.println("low grade = " + test.getLowGrade());       
    out.println("high grade = " + test.getHighGrade()+ "\n\n");

    test.setGrades("9 - 10 70 90 92.5 85 95.5 77.5 88 100.0");
    out.println(test);
    out.println("sum = " + test.getSum());  
    out.println("num grades = " + test.getNumGrades());                                         
    out.println("low grade = " + test.getLowGrade());       
    out.println("high grade = " + test.getHighGrade());

with an output like this: 90.0 85.0 95.5 77.5 88.0

sum = 436.0

num grades = 5

low grade = 77.5

high grade = 95.5

10.0 70.0 90.0 92.5 85.0 95.5 77.5 88.0 100.0

sum = 708.5

num grades = 9

low grade = 10.0

high grade = 100.0

Your issue is with public void setGrades(String gradeList) function in Grades Line 27 This means that the entire gradeList is something like "abc". Convert this to array function and read individual String. [a],[b],[c]. Use split function of String to form an array and populate your grades[i]

This method in Scanner is causing the issue which means there is no input left to parse after scan.nextInt() ;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM