我目前正致力于游戏风险的实施。在构建国家,大陆及其邻接的Board类中,我正在阅读三个不同的文本文件。当我建造一个新的大陆时,它的“大陆”构造函数需要以下内容 (String name,int bonusArmies,ArrayList memberCountries)。 现在,我正在使用扫描仪从一个组织如下的文本文件中读取, 它拥有的名称,奖金军队以及每条线上的其余部分都是其成员国。
北美,5,阿拉斯加,艾伯塔省,中美洲,美国东部,格陵兰岛,西北地区,安大略省,魁北克省,美国西部 南美洲,2,阿根廷,巴西,委内瑞拉 欧洲,5,英国,冰岛,北欧,斯堪的那维亚,南欧,乌克兰,西欧 非洲,3,刚果,东非,埃及,马达加斯加,北非,南非 亚洲,7,阿富汗,中国,印度,伊尔库茨克,日本,堪察加半岛,中东,蒙古,暹罗,西伯利亚,乌拉尔,雅库茨克 澳大利亚,2,澳大利亚东部,印度尼西亚,LotR,新几内亚,西澳大利亚
我知道我需要为Scanner设置一个分隔符,我已尝试过多种方法,但无济于事。这是我尝试创建Continents散列图的示例。
public void createContinents()
{
Scanner inputTwo = new Scanner( new File( "continents.txt");
while( inputTwo.hasNext()) //continues to create and add countries until their are none next
{
String line = inputTwo.nextLine(); //stores the entire line
Scanner lineScan = new Scanner( line); //passes the entire line into Scanner
lineScan.useDelimiter(","); //sets the Scanner's delimiter pattern
String name = inputTwo.next(); //stores the first String which is the name of the current continent being created
System.out.println( " the name is" + name);
int bonusArmies = inputTwo.nextInt(); //stores the second String which is casted into an int for the continent bonus armies
System.out.println(" the number of bonus armies is" + bonusArmies);
ArrayList<Country> memberCountries = new ArrayList<Country>(); //creates an arraylist to hold the member countries of current continent
while( inputTwo.hasNext()) //goes through the rest of the line to add the member countries until it reaches the end of the line
{
memberCountries.add( countries.get(inputTwo.next()));//gets string name of country and pass it as key to store into temp arraylist of countries
System.out.println( "the member countries" + memberCountries);
}
continent = new Continent( name, bonusArmies, memberCountries); //creates continent
continents.put(name , continent); //associates a key to the current continent
}
inputTwo.close();
}
这是该图片中的文字。
PS C:\Users\repti_000\desktop\risk\homeworks2120\game> java BoardTester
the name is
SouthAmerica,2,Argentina,Brazil,Venezuela
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Board.createContinents(Board.java:66)
at Board.loadBoard(Board.java:141)
at Board.<init>(Board.java:27)
at BoardTester.main(BoardTester.java:7)
PS C:\Users\repti_000\desktop\risk\homeworks2120\game>
答案 0 :(得分:1)
您从inputTwo而不是lineScan获取数据。你应该在这里考虑更清晰的名字,这样可以更容易地发现这类问题。而不是inputTwo mapData或worldScanner而不是lineScan可能是continentData。
答案 1 :(得分:1)
我将扫描仪和一些变量重命名为更清晰,并添加了一些带有CHANGED的注释。我想知道为什么你的hashmap大陆看起来像大陆(名称,大陆),而你的商店是大陆的名称(当你用构造函数给它时)。使用continent.getName()似乎是一种更好的做法。
正如RotN爵士指出的那样,你使用了错误的扫描仪,你的名字确实不清楚。尽量让你的变量名更清晰一些,这应该有时间。
public void createContinents() {
// Scan the continents.txt file
Scanner worldScan = new Scanner( new File( "continents.txt");
// While worldScan has a next line, run loop
// CHANGED: not hasNext(), but hasNextLine()
while( worldScan.hasNextLine()) {
// Create a scanner for the selected line in worldScan
String continentLine = inputTwo.nextLine();
Scanner continentScan = new Scanner(continentLine);
continentScan.useDelimiter(",");
// Grab the continent from the scanner
String name = continentScan.next();
System.out.println( " the continent name is" + name);
// Grab the armies from the scanner
// CHANGED: you read it out as a string, not as an int
int bonusArmies = Integer.toString(continentScan.next());
System.out.println(" the number of bonus armies is" + bonusArmies);
// Instantiate loop variable countries
ArrayList<Country> memberCountries = new ArrayList<Country>();
while( continentScan.hasNext()) {
memberCountries.add(countries.get(continentScan.next()));
System.out.println( "the member countries" + memberCountries);
}
// Instantiate loop variable continent
Continent continent = new Continent( name, bonusArmies, memberCountries);
// Add continent to the hashmap
continents.put(name,continent);
continentScan.close();
}
worldScan.close();
}