Java 如何在一个循环中读取多个数组?
以下是我的指示: 这个程序将使用两个数组——它们被称为并行数组。您将不会使用对象数组。 此应用程序中至少有6个方法(包括main()) inputData()-将数据文件输入到两个数组中-数据文件在下面,称之为“population.txt” 在将扫描仪对象关联到该文件之前,请记住检查该文件是否存在 displayCountries()-显示所有国家/地区-仅显示国家/地区 你能告诉我为什么这个不能运行吗?我需要将人口值和国家名称放在一起,以便以后可以将其写入表格中。所以我认为我需要将第一个值读入countryName,将第一个值读入populationNum,而不是同时将它们全部读入。我正在阅读的文本在代码下面。但是我不知道怎么做。我还想知道在实例化时是否需要[25]。它给了我这个错误:Java 如何在一个循环中读取多个数组?,java,arrays,parallels,Java,Arrays,Parallels,以下是我的指示: 这个程序将使用两个数组——它们被称为并行数组。您将不会使用对象数组。 此应用程序中至少有6个方法(包括main()) inputData()-将数据文件输入到两个数组中-数据文件在下面,称之为“population.txt” 在将扫描仪对象关联到该文件之前,请记住检查该文件是否存在 displayCountries()-显示所有国家/地区-仅显示国家/地区 你能告诉我为什么这个不能运行吗?我需要将人口值和国家名称放在一起,以便以后可以将其写入表格中。所以我认为我需要将第一个值读
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Population.inputData(Population.java:32)
at Population.main(Population.java:13)
这是我的代码:
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
import java.text.DecimalFormat;
public class Population{
public static void main(String [] args)throws IOException{
//Arrays
String [] countryNames = new String [25];
int [] populationNum = new int [25];
//Input data from file into the array
inputData(countryNames, populationNum);
//Displays and calculations
displayCountries(countryNames);
} //end main()
//this class gets the input for arrays from the file
public static void inputData(String [] countryNames, int [] populationNum) throws IOException{
File infile = new File("population.txt.");
int index = 0;
Scanner scan = new Scanner(infile);
while(scan.hasNext())
for(int i = 0; i < countryNames.length; i++)
countryNames[i] = scan.nextLine();
for(int i = 0; i < populationNum.length; i++)
populationNum[i] = scan.nextInt();
} //end inputData()
//this class displays the countries
public static void displayCountries(String [] countryNames) {
for(int i = 0; i < countryNames.length; i++)
System.out.println(countryNames[i]);
} //end displayCountries()
}//end class
Ghana
24333000
Brazil
193364000
Australia
23480970
Nigeria
170123000
Papua New Guinea
6888000
Mexico
108396211
Egypt
79221000
Iran
75078000
Myanmar
50496000
Belgium
10827519
Tuvalu
10000
russia
141927297
import java.io.*;
导入java.util.Scanner;
导入java.io.IOException;
导入java.text.DecimalFormat;
公共阶层人口{
公共静态void main(字符串[]args)引发IOException{
//阵列
String[]countryNames=新字符串[25];
int[]populationNum=新int[25];
//将数据从文件输入数组
输入数据(国家名称、人口数量);
//显示和计算
显示国家(国名);
}//结束main()
//此类从文件中获取数组的输入
公共静态void inputData(字符串[]CountryName,int[]populationNum)引发IOException{
File infle=新文件(“population.txt”);
int指数=0;
扫描仪扫描=新扫描仪(填充);
while(scan.hasNext())
for(int i=0;i
在两个for循环之后需要{after while(scan.hasNext())和closing}。发生的情况是,当循环扫描所有数据时,for循环尝试执行scan.next,此时扫描仪已在文件末尾。希望这有帮助您需要在同一个循环中读入两个数组,如下所示:
int i = 0;
while(scan.hasNext()) {
countryNames[i] = scan.nextLine();
if (scan.hasNext()) populationNum[i] = scan.nextInt();
if (scan.hasNext()) scan.nextLine(); // Go to the next line
i++;
}
while
中的两个for
循环不正确(更不用说第二个for
循环甚至不是while
的一部分,因为您省略了花括号)
输入的实际格式是什么?每个国家都在一条线上,人口在下一条线上吗?或者其中一些数据在同一行上?是的,每个输入都在不同的行上,我有24行,有12个数字和12个国家名称。最后一行不是应该是
scan.nextLine()
而不是scan.next()
?我以为scan.nextLine()
只会返回包含换行符的空格,但是scan.next()
会吃掉下一个国家的名称。。。我可能是错的,还没有测试过……我完全做到了,得到了同样的异常error@AndrewMarbl编辑后是否重试?我试过了,它应该是正确的,只是输出中将有很多行显示为“null”
——您需要找出如何处理这些行。@AndrewMarbl尝试在最后一行上添加一个行尾标记。@AndrewMarbl如果允许您使用ArrayList
s,请使用它们而不是数组。否则,从getInput
返回int
(从while
循环返回i
的最终值)。