【问题标题】:arrays of references throwing errors related to scanner引发与扫描仪相关的错误的引用数组
【发布时间】:2013-02-28 03:24:06
【问题描述】:

这是有问题的代码:

 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();

使用的类的代码可以在以下链接中找到:

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

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

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

我的问题是在我运行代码并输入班级名称、班级人数、学生姓名后发生的,这些都很好,当我输入学生的成绩时,下一点会引发错误,例如3 - 70.2 65.3 45.1

编辑 这是我得到的错误:

欢迎来到班级统计计划! 这个班级的名字是什么?应用

这个班有多少学生? 1

输入学生 1 的姓名:几乎没有

输入几乎没有的成绩

使用格式 x - 等级 (2 - 100 100):线程“主”中的异常

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

我现在认为问题是我需要 stuGrades 读取整行,例如 3 - 70.2 65.3 45.1 但我不知道需要哪个扫描仪语句将键盘输入放入字符串

EDIT3

这是我证明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());

输出如下: 90.0 85.0 95.5 77.5 88.0

总和 = 436.0

成绩数 = 5

低等级 = 77.5

高等级 = 95.5

10.0 70.0 90.0 92.5 85.0 95.5 77.5 88.0 100.0

总和 = 708.5

年级数 = 9

低等级 = 10.0

高等级 = 100.0

【问题讨论】:

  • 抛出什么错误?
  • 请发布您收到的错误。如果是异常,请发布堆栈跟踪。如果捕获到异常,修改代码以打印堆栈跟踪(exception.printStackTrace())
  • 您只需要发布代码的相关部分,不要链接到外部文件,人们可能没有时间浏览您的所有代码,人们可能会跳过你的问题。
  • 这实际上是我将它们放在 PasteBin 上的原因,所以我的问题不会占用太多空间
  • @tech_geek23,看看我的回答。如果这能解决您的问题,请告诉我

标签: java arrays eclipse reference error-handling


【解决方案1】:

您的问题是成绩中的 public void setGrades(String gradeList) 函数Line 27 这意味着整个gradeList 类似于“a b c”。将此转换为数组函数并读取单个字符串。 [a],[b],[c]。使用 String 的split 函数形成一个数组并填充您的grades[i]

Scanner 中的这个方法导致了这个问题,这意味着在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     }

【讨论】:

  • 那么为什么成绩、学生和班级都可以在他们的运行文件中正常工作,但是在引用 class.java 时,最终会引用所有其他文件,我会出错吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2017-04-27
  • 2018-08-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多