【发布时间】:2013-12-02 22:19:46
【问题描述】:
我需要有关如何查找用户选择的状态信息并将其显示到屏幕上的帮助。我有五个文件放入数组中。这五个数组包含一个州的信息(州名、州首府、州昵称、州花、州人口。)
我的程序应该允许程序的用户输入州名,并且应该在屏幕上显示有关该州的信息(昵称、首都、花卉和人口)。
每个输入文件都包含按州名字母顺序排列的信息。所以州名文件第1行是阿拉巴马州,大写文件第1行是蒙哥马利(阿拉巴马州首府),花卉文件第1行是阿拉巴马州的州花,昵称文件第1行是阿拉巴马州的昵称,并且人口文件的第 1 行是阿拉巴马州的人口。每个文件的第 2 行是阿拉斯加的信息,第 3 行是亚利桑那州等。
这是我目前的代码。
public static void main(String[] args) throws FileNotFoundException {
String capital, stateFlowers, stateNickName, statesNames;
int statePopulation;
Scanner keyboard = new Scanner (System.in);
String userStateChoise;
File capitals = new File("capitals.txt");
File flores = new File("flowers.txt");
File nickName = new File("nicknames.txt");
File populacion = new File("population.txt");
File state_Name = new File("stateNames.txt");
System.out.println("Enter a State Name");
userStateChoise = keyboard.nextLine();
if (!capitals.exists()) {
System.out.println("The capitals input file was not found");
System.exit(0);
}
if (!flores.exists()) {
System.out.println("The flores input file was not found");
System.exit(0);
}
if (!nickName.exists()) {
System.out.println("The nickName input file was not found");
System.exit(0);
}
if (!populacion.exists()) {
System.out.println("The populacion input file was not found");
System.exit(0);
}
if (!state_Name.exists()) {
System.out.println("The stateName input file was not found");
System.exit(0);
}
Scanner inputFile = new Scanner(capitals);
Scanner flower = new Scanner(flores);
Scanner nickNames = new Scanner(nickName);
Scanner populations = new Scanner(populacion);
Scanner names = new Scanner(state_Name);
// ArrayList myCapitals = new ArrayList();
// ArrayList<String> stateNames = new ArrayList();
while (inputFile.hasNext() && flower.hasNext() && nickNames.hasNext()
&& populations.hasNext() && names.hasNext())
{
// reading all states from the input file
//reading all cities from the input file
capital = inputFile.nextLine();
stateFlowers = flower.nextLine();
stateNickName = nickNames.nextLine();
statePopulation = populations.nextInt();
statesNames = names.nextLine();
String[] stateCapitalArray = {capital};
String[] stateflowersArray = {stateFlowers};
String[] stateNickNameArray = {stateNickName};
int [] statePopulationArray = {statePopulation};
String[] stateNamesArray = {statesNames};
// I used for loops to go through each item in the array and to display it to the screen
for (int index = 0; index < stateNamesArray.length; index++)
{
System.out.println("State name : " +stateNamesArray[index]);
}
for (int index = 0; index < stateflowersArray.length; index++)
{
System.out.println("State flower : " + stateflowersArray[index]);
}
for (int index = 0; index < stateCapitalArray.length; index++)
{
System.out.println("The capital : " + stateCapitalArray[index]);
}
for (int index = 0; index < stateNickNameArray.length; index++)
{
System.out.println("State nickName : " + stateNickNameArray[index]);
}
for (int index = 0; index < statePopulationArray.length; index++)
{
System.out.println("State pupoluation : " + statePopulationArray[index]);
System.out.println("\n\n\n");
}
}
inputFile.close();
flower.close();
nickNames.close();
// populations.close();
}
}
【问题讨论】:
-
您当前的代码有什么问题。你想解决什么问题?