我的代码没有错误,但我不明白为什么我运行时会有错误。以下是完整的例外情况:
java.util.NoSuchElementException
Initializing population...
at java.util.StringTokenizer.nextToken(Unknown Source)
at Input.takeinput(Input.java:44)
at schedule.main(schedule.java:33)
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Unknown Source)
at schedule.createpopulation(schedule.java:244)
at schedule.main(schedule.java:37)异常到底发生在哪里?
对于第一个错误,代码如下(Input.java:44)
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
//StringTokenizer st = new StringTokenizer(line, ";");
//input student and sv
if(line.equals("student")){
nostud = 0;
while(!(line=scanner.nextLine()).equals("examiner")){
StudData[nostud] = new Student();
StringTokenizer st = new StringTokenizer(line, ";");
StudData[nostud].setName(st.nextToken());
StudData[nostud].setCode(st.nextToken());
StudData[nostud].setSvName(st.nextToken());
StudData[nostud].setSvCode(st.nextToken());
nostud++;
}
}和(schedule.java:33)
input.takeinput();对于第二个错误。(schedule.java:44)
while(!flag){
int ex = r.nextInt(noexm);和(schedule.java:37)
createpopulation();发布于 2016-01-17 19:08:22
第一个异常意味着您调用nextToken的次数超过了必要的次数。您要标记的字符串上没有“下一个令牌”。例如,对于字符串a;b;c,您只能调用nextToken 3次,第4次调用将抛出一个异常:NoSuchElementException Second Exception意味着您的noexm变量是一个负整数,这是不好的。尝试调试并找出noexm的值,并找出它为负的原因。
https://stackoverflow.com/questions/34837390
复制相似问题