cannot determine the cause of these test errors, debugger isnt functioning here, nor can i figure out how to resolve this

huangapple go评论74阅读模式
英文:

cannot determine the cause of these test errors, debugger isnt functioning here, nor can i figure out how to resolve this

问题

这里的任务是创建一个文件,创建一个名为Student的对象,该对象接受姓名、年龄和GPA。文件中填充的行大致如下:name=Jane Robinson,age=19,gpa=3.81。在读取每一行时,需要拆分“,”和“=”,然后检查数组的每个索引是否包含“name”、“age”、“gpa”,如果找到其中之一,则需要对字符串进行子串操作(substring),将其保存为变量,调用Student的setter方法设置变量,然后将新设置的Student对象加载到名为result的ArrayList中,最后将其返回。

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]);
  }
}

错误如下:

在代码中的错误 - testRead10Lines(StudentReaderTest):
开始 0,结束 4,长度 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. 在代码中的错误 - testReadSingleLine(StudentReaderTest):
开始 0,结束 4,长度 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. 在代码中的错误 - testFileNotExist(StudentReaderTest):
java.lang.NullPointerException
at StudentReader.readFromTextFile(StudentReader.java:32)
at StudentReaderTest.testFileNotExist(StudentReaderTest.java:113)

就像我在问题中所说的,由于某种原因,我的调试器不会显示我造成这种情况的原因,我无法确定为什么每个对象的长度仅为2,我没有其他信息可提供,因为我已经在顶部列出了整个任务。但是,有人能告诉我为什么会出现这些索引越界错误吗?

英文:

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&lt;Student&gt; result = new ArrayList&lt;Student&gt;();
String name =&quot; &quot;;
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(&quot;,&quot;);
String[] string2;
for (int i = 0; i &lt; string1.length; ++i)
{
string2 = string1[i].split(&quot;=&quot;);
if (string2[i].substring(0,4).equals(&quot;name&quot;))
{
name = string2[i].substring(4,string2[i].length());
}
if(string2[i].substring(0,3).equals(&quot;age&quot;))
{
age = Integer.parseInt(string2[i].substring(3,string2[i].length()));
}
if(string2[i].substring(0,3).equals(&quot;gpa&quot;))
{
gpa = Double.parseDouble(string2[i].substring(3,string2[i].length()));
}
}
} 
return result.toArray(new Student[0]);
}
}

errors here:

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)

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 don't have any other information to give here as I have the whole task listed in the top. but can anyone tell me why I'm running these index out of bounds errors

答案1

得分: 1

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

应为:

  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's the substring that's causing the issue.

It should be:

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

huangapple
  • 本文由 发表于 2020年10月5日 05:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定