【发布时间】:2013-12-02 04:00:22
【问题描述】:
抱歉有任何错误,我尝试进行一些更正,以便更容易理解我在做什么以及我需要帮助的地方。我对 java 和这个网站很陌生。
我正在尝试编写一个程序,它将 5 个单独文件中的州数据(州名、州昵称、州人口、州花和州首府)组织到五个单独的并行数组中。代码将要求输入并向用户提供以逗号格式化的人口数据。我还在创建一种将所有信息写入输出文件的方法。
我遇到了一些问题。
我想知道是否可以在一些问题上寻求帮助?
首先,运行代码后state_data.txt文件为空。
其次,如果你在 askState 方法中输入了一个无效的选择而不是要求一个新的输入,我的程序就会崩溃
我将不胜感激!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
*
*/
public class Project_3a {
/**
* @param args the command line arguments
*/
static final int NUM_STATES = 50;
public static void main(String[] args) throws FileNotFoundException {
String[] stateNames = importFile("stateNames.txt");
String[] nicknames = importFile("nicknames.txt");
String[] populations = importFile("population.txt");
String[] flowers = importFile("flowers.txt");
String[] capital = importFile("capitals.txt");
for (int x = 0; x < NUM_STATES; x++)
{
createFile(stateNames, nicknames, capital, flowers, populations);
char getWantsToContinue = 'y';
while (getWantsToContinue == 'y' || getWantsToContinue == 'Y')
{
System.out.println(stateNames);
int index = askState(stateNames);
String unformattedPopulation = populations[index];
String formattedPopulation = formatPopulation(unformattedPopulation);
outPut(stateNames,nicknames, capital, flowers, formattedPopulation,index);
}
getWantsToContinue = again();
}
public static String[] importFile(String fileName) throws
FileNotFoundException {
String[] array = new String[NUM_STATES];
File inputFile = new File(fileName);
Scanner scanner = new Scanner(inputFile);
for (int i = 0; i < NUM_STATES; i++) {
array[i] = scanner.nextLine();
}
return array;
}
public static int askState(String[] stateNames) {
Scanner keyboard = new Scanner(System.in);
String state;
int statePosition = -1;
System.out.println("Please enter a state that you would like to search:");
state = keyboard.nextLine();
{
for (int i = 0; i < NUM_STATES; i++) {
if (state.equals(stateNames[i])) {
statePosition = i;
return statePosition;
}
}
System.out.println("Please enter a valid state:");
}
return statePosition;
}
public static String formatPopulation(String population) {
String formattedPopulations = "";
{
for (int i = population.length() - 1; i >= 0; i--) {
formattedPopulations = population.charAt(i) + formattedPopulations;
if (i % 3 == 2 && (i + 1 != population.length())) {
formattedPopulations = "," + formattedPopulations;
}
}
return formattedPopulations;
}
}
public static void createFile(String[] stateNames, String[] nicknames,
String[] capitals, String[] flowers, String[] populations) throws FileNotFoundException {
PrintWriter stateData = new PrintWriter("state_data.txt");
for (int i = 0; i < NUM_STATES; i++) {
stateData.println(stateNames[i] + "\t" + nicknames[i] + "\t" + capitals[i]
+ "\t" + flowers[i] + "\t" + formatPopulation(populations[i]) + "\n");
}
stateData.close();
}
public static void outPut(String[] stateNames, String[] nicknames,
String[] capitals, String[] flowers, String formattedPopulation,
int index) {
System.out.println("State Name: " + stateNames[index]);
System.out.println("Nickname: " + nicknames[index]);
System.out.println("Capital: " + capitals[index]);
System.out.println("Flower: " + flowers[index]);
System.out.println("Population: " + formattedPopulation);
}
public static char again() {
Scanner keyboard = new Scanner(System.in);
char getWantsToContinue;
System.out.println("Would you like to search another state?");
System.out.println("Please enter Y for yes or N for no.");
getWantsToContinue = keyboard.next().charAt(0);
while (getWantsToContinue != 'y' && getWantsToContinue != 'Y'
&& getWantsToContinue != 'n' && getWantsToContinue != 'N') {
System.out.println("Please enter a valid option (Yor N).");
getWantsToContinue = keyboard.next().charAt(0);
}
if (getWantsToContinue == 'n' || getWantsToContinue == 'N') {
System.out.println("Goodbye!");
}
return getWantsToContinue;
}
}
【问题讨论】:
-
“我将不胜感激并提供帮助!” 如有任何问题,我们将不胜感激!你有问题吗?你有什么问题?
-
我的问题是如何解决我上面提到的两个问题。
-
问题应该以'?'结尾。为了引起那些搜索“?”的人的注意。在文本中,如果没有,请立即转到下一个帖子,edit the question 在适当的位置添加它们!
-
使用
while循环为用户输入添加条件语句。广告让您的代码更具可读性以获得更好的帮助 -
我尝试进行一些更改,以便读者更好地理解我的要求。