无法确定这些测试错误的原因,调试器无法在此处运行,我也无法找出解决方法 [英] cannot determine the cause of these test errors, debugger isnt functioning here, nor can i figure out how to resolve this

查看:75
本文介绍了无法确定这些测试错误的原因,调试器无法在此处运行,我也无法找出解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的任务是创建一个文件,创建一个名为Student的对象,该对象的名称为age gpa。文件中填充了几行,看起来像是此名称=简·罗宾逊(Jane Robinson),年龄= 19,gpa = 3.81,同时读取了每一行以分割,。和 =然后检查数组的每个索引中的名称,年龄, gpa。如果找到其中之一,则子串(startpostion,其余字符串);并将其另存为变量,为每个变量调用Student setter并进行设置,然后将新设置的Student对象加载到名为result的arrayList中,然后我可以将其返回。

the task here is to create a file, create a object called Student that takes a name, age gpa. the file is filled with lines that look roughly like this name=Jane Robinson,age=19,gpa=3.81 while reading each line i am to split the "," and the "=" and then check each index of array for "name", "age", "gpa" if one of those are found, to then substring(startpostion, rest of string); and save that as a variable, call the Student setters for each variable and set them, then load the newly set up Student objects into a arrayList called result, that I may then return it.

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * 
 *
 *
 *
 */
public class StudentReader {
   
  public static Student[] readFromTextFile(String fileName) {
  ArrayList<Student> result = new ArrayList<Student>();
   String name =" ";
   int age = 0;
   double gpa = 0.0;
   String fill;
   
   File f = new File(fileName);
   Scanner n = null;
   try
   {
       n = new Scanner(f);
   }
  catch(FileNotFoundException ex)
   {
    ex.printStackTrace();
   }
   
   while (n.hasNextLine())
   {
     fill = n.nextLine();
     Student g =  new Student(name , age, gpa);
     String[] string1 = fill.split(",");
     
     String[] string2;
     for (int i = 0; i < string1.length; ++i)
     {
       
       string2 = string1[i].split("=");
      if (string2[i].substring(0,4).equals("name"))
        {
      name = string2[i].substring(4,string2[i].length());
        }
      if(string2[i].substring(0,3).equals("age"))
        {
      age = Integer.parseInt(string2[i].substring(3,string2[i].length()));
        }
      if(string2[i].substring(0,3).equals("gpa"))
        {
      gpa = Double.parseDouble(string2[i].substring(3,string2[i].length()));
        }
        
     }
   } 
    return result.toArray(new Student[0]);
  }
}

此处的错误:

ERROR IN YOUR CODE - testRead10Lines(StudentReaderTest):
     begin 0, end 4, length 2
     java.lang.StringIndexOutOfBoundsException
          at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3116)
          at java.base/java.lang.String.substring(String.java:1885)
          at StudentReader.readFromTextFile(StudentReader.java:43)
          at StudentReaderTest.testRead10Lines(StudentReaderTest.java:104)

2. ERROR IN YOUR CODE - testReadSingleLine(StudentReaderTest):
     begin 0, end 4, length 2
     java.lang.StringIndexOutOfBoundsException
          at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3116)
          at java.base/java.lang.String.substring(String.java:1885)
          at StudentReader.readFromTextFile(StudentReader.java:43)
          at StudentReaderTest.testReadSingleLine(StudentReaderTest.java:84)

3. ERROR IN YOUR CODE - testFileNotExist(StudentReaderTest):
     java.lang.NullPointerException
          at StudentReader.readFromTextFile(StudentReader.java:32)
          at StudentReaderTest.testFileNotExist(StudentReaderTest.java:113)

就像我在问题中说的那样,我的调试器不会告诉我原因在这里,我无法确定每个对象的长度为何只有2个的原因,因为我将整个任务列在顶部,所以我没有其他信息可提供。但是谁能告诉我为什么我无法运行这些索引错误

like i said in the question for some reason my debugger wont show me the cause here and i cant identify why the length of each object is only 2, i dont have any other information to give here as i have the whole task listed in the top. but can anyone tell me why im running these index out of bounds errors

推荐答案

这是导致问题的子字符串。

It's the substring that's causing the issue.

应为:

  if (string2[i].substring(0,3).equals("name"))
    {
  name = string2[i].substring(3,string2[i].length());
    }
  if(string2[i].substring(0,2).equals("age"))
    {
  age = Integer.parseInt(string2[i].substring(2,string2[i].length()));
    }
  if(string2[i].substring(0,2).equals("gpa"))
    {
  gpa = Double.parseDouble(string2[i].substring(2,string2[i].length()));
    }
    

这篇关于无法确定这些测试错误的原因,调试器无法在此处运行,我也无法找出解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆