第一部分
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;
}
}
dat文件
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
我收到一个错误,说outofboounds异常,并且不知道为什么或这意味着什么?我还没有完成toString那是问题还是其他的东西?如果是toString,我怎么格式化它所以它看起来像这个
Bob is related to John Tom
Dot is related to Chuck Fred Jason Tom
Elton is related to Linh
我无法让它工作
答案 0 :(得分:0)
如果文件没有两个以空格分隔的字符串,则以下代码将失败
String[] personRelative = line.split(" ");
String person = personRelative[0];
String relative = personRelative[1];
你有没有试过调试这个?
答案 1 :(得分: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教程,like these,或许。
答案 2 :(得分:0)
您的dat文件的最后一行不包含任何。因此,split方法返回一个大小仅为1的数组。在读取此行时,以下代码会导致OutOfBoundsException:
String[] personRelative = line.split(" ");
String person = personRelative[0];
String relative = personRelative[1];// This line causes ArrayIndexOutOfBoundsException