0

第一部分

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import static java.lang.System.*;

public class RelativesTester
{
    public static void main( String args[] ) throws IOException
    {


        Scanner in = new Scanner(new File("Relatives.dat"));

            int z = in.nextInt();

            for(int x = 0; x<z;x++)
            {
                String n = in.nextLine();
                Relatives a = new Relatives();
                a.setPersonRelative(n);
                System.out.println (a);
            }

    }
}

第二部分

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Scanner;
import static java.lang.System.*;

public class Relatives
{
    private Map<String,Set<String>> map;

    /**
     * Constructs a relatives object with an empty map
     */
    public Relatives()
    {
        map = new TreeMap<String,Set<String>>();
    }

    /**
     * adds a relationship to the map by either adding a relative to the
     * set of an existing key, or creating a new key within the map
     * @param line a string containing the key person and their relative
     */
    public void setPersonRelative(String line)
    {
        String[] personRelative = line.split(" ");
        String person = personRelative[0];
        String relative = personRelative[1];

        if(map.containsKey(person))
        {
            map.get(person).add(relative);
        }
        else
        {
            Set<String> relatives = new TreeSet<String>();
            relatives.add(relative);
            map.put(person,relatives);
        }
    }

    /**
     * Returns the String version of the set containing person's relatives
     * (see last line of sample output)
     * @param person the person whose relative set should be returned as a String
     * @param the string version of person's relative set
     */
    public String getRelatives(String person)
    {
        String s = "";
        s+=(person);
        s+=(" is related to ");

        for(String relative : map.get(person))
        {
            s+=(relative);
            s+=(' ');
        }
            return s;
        }


    /**
     * returns the String version of the entire map listing each key person and all of
     * their relatives
     * (see sample output except last line)
     */
    public String toString()
    {
        String output="";


        return output;
    }
}

数据文件

14
Jim Sally
Fred Alice
Jim Tom
Jim Tammy
Bob John
Dot Fred
Dot Tom
Dot Chuck
Bob Tom
Fred James
Timmy Amanda
Almas Brian
Elton Linh
Dot Jason
Dot

我收到一个错误提示超出范围异常并且不知道为什么或这意味着什么?我还没有完成 toString 是这个问题还是别的什么?如果它是 toString 我该如何格式化它看起来像这样

Bob is related to John Tom
Dot is related to Chuck Fred Jason Tom
Elton is related to Linh

我无法让它工作

4

3 回答 3

0

如果文件没有用空格分隔的两个字符串,则以下代码将失败

String[] personRelative = line.split(" ");
String person = personRelative[0];
String relative = personRelative[1];

您是否尝试过调试?

于 2013-09-27T02:10:25.257 回答
0

从您发布的代码中,我只能看到您会收到该错误的一个地方。(如果您也发布了确切的错误会很有帮助,因为它包含有关异常发生位置、行号、堆栈跟踪等的信息。)

String[] personRelative = line.split(" ");
String person = personRelative[0];
String relative = personRelative[1];

如果line.split()返回一个长度为 1 的数组,尝试访问索引 1 将导致 OOB 异常。

如果您知道应该split()返回一个长度为 2 的数组,请验证它。例如,

String[] personRelative = line.split(" ");
if(personRelative.length != 2){ throw new RuntimeException("Split Error!"); }
String person = personRelative[0];
String relative = personRelative[1];

作为旁注,我真的建议您阅读一些基本的 Java 教程,比如这些

于 2013-09-27T02:11:14.597 回答
0

dat 文件的最后一行不包含任何. 因此,split 方法返回一个大小仅为 1 的数组。在读取这一行时,以下代码会导致 OutOfBoundsException:

String[] personRelative = line.split(" ");
String person = personRelative[0];
String relative = personRelative[1];// This line causes ArrayIndexOutOfBoundsException
于 2013-09-27T02:36:19.630 回答