Java 写入文件和搜索数组时出现问题
对于任何错误,我都很抱歉,我试着做了一些更正,以便更容易理解我在做什么以及我需要什么帮助。我对java和这个网站非常陌生 我正试图编写一个程序,将在5个独立文件中找到的州(州名、州昵称、州人口、州花和州首府)数据组织到5个独立的并行数组中。代码将请求输入并向用户提供带有逗号格式的填充数据。我还创建了一个将所有信息写入输出文件的方法 我有一些问题 我想知道我是否能在一些问题上得到帮助 首先,运行代码后,文件状态_data.txt为空 其次,如果在askState方法中输入无效的选项而不是请求新的输入,我的程序就会崩溃 我将感谢任何帮助Java 写入文件和搜索数组时出现问题,java,arrays,file,loops,Java,Arrays,File,Loops,对于任何错误,我都很抱歉,我试着做了一些更正,以便更容易理解我在做什么以及我需要什么帮助。我对java和这个网站非常陌生 我正试图编写一个程序,将在5个独立文件中找到的州(州名、州昵称、州人口、州花和州首府)数据组织到5个独立的并行数组中。代码将请求输入并向用户提供带有逗号格式的填充数据。我还创建了一个将所有信息写入输出文件的方法 我有一些问题 我想知道我是否能在一些问题上得到帮助 首先,运行代码后,文件状态_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;
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.PrintWriter;
导入java.util.Scanner;
/**
*
*/
公共类项目(3a){
/**
*@param指定命令行参数
*/
静态最终整数状态=50;
公共静态void main(字符串[]args)引发FileNotFoundException{
字符串[]stateNames=importFile(“stateNames.txt”);
字符串[]昵称=导入文件(“昵称.txt”);
String[]populations=importFile(“population.txt”);
字符串[]flowers=importFile(“flowers.txt”);
字符串[]大写=导入文件(“capitals.txt”);
对于(int x=0;x=0;i--){
格式化总体=总体。字符(i)+格式化总体;
如果(i%3==2&&(i+1!=population.length()){
formattedPopulations=“,”+formattedPopulations;
}
}
返回原始种群;
}
}
公共静态void createFile(字符串[]状态名,字符串[]昵称,
字符串[]大写字母、字符串[]花朵、字符串[]人口)引发FileNotFoundException{
PrintWriter stateData=新的PrintWriter(“state_data.txt”);
对于(int i=0;i
我只想补充一下我在您的代码中发现的问题
(1) 。我发现的第一个问题是在函数importFile
中
for (int i = 0; i < NUM_STATES; i++) {
array[i] = scanner.nextLine();
}
(2) .我在createFile
方法中找到了第二个问题
您正在创建一个新文件,并从所有文件中插入第一个字符。然后是第二个字符。然后是第三个字符…直到第50个字符
如果相应的文件在第50个字符之前没有任何数据该怎么办?这将在结果中输入错误的数据
(a). Read one character at a time.
(b). If you are not sure about the size of each file, iterate through it till the EOF.
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);
}
while(scanner.hasNext()){
scanner.next();
}
while(scanner.hasNextLine()){
scanner.nextLine();
}
while(invalidState(state)){
System.out.println("Please enter input");
state = keyboard.nextLine();
}
for (int x = 0; x < NUM_STATES; x++)
{
createFile(stateNames, nicknames, capital, flowers, populations);