Java 如何将数据文件中的数据加载到数组中,请求用户输入,然后根据用户输入查找并打印数据?
我有一个项目,我们必须使用一个文件名zipcodes.txt并加载到一个数组中。文件格式类似于08001,Alloway,Salem县,其中每个项目本身位于不同的行上 目标是将数据加载到邮政编码数组中。打印邮政编码列表——包括每个邮政编码、城镇和州,并在单独的行上显示每个邮政编码的数据。一种方法,要求用户输入邮政编码,然后显示该邮政编码的数据,或者说在列表中找不到该邮政编码。这就是需要做的,我在这里画一个空白。有什么想法吗 因为必须有一个主类、一个存储数组的ziplist类和一个存储变量的zipcode类,所以我设法将文件加载到一个数组中,并在main方法中调用它。但我似乎不知道如何在代码中实现用户,并且有一个类似布尔的条件来比较输入和文件中的数据,然后打印zip和zip的数据,比如乡镇和州 文件是这样的Java 如何将数据文件中的数据加载到数组中,请求用户输入,然后根据用户输入查找并打印数据?,java,Java,我有一个项目,我们必须使用一个文件名zipcodes.txt并加载到一个数组中。文件格式类似于08001,Alloway,Salem县,其中每个项目本身位于不同的行上 目标是将数据加载到邮政编码数组中。打印邮政编码列表——包括每个邮政编码、城镇和州,并在单独的行上显示每个邮政编码的数据。一种方法,要求用户输入邮政编码,然后显示该邮政编码的数据,或者说在列表中找不到该邮政编码。这就是需要做的,我在这里画一个空白。有什么想法吗 因为必须有一个主类、一个存储数组的ziplist类和一个存储变量的zip
08001
Alloway
Salem County
08002
Cherry Hill
Camden County
08003
用他们自己的数据。比如08001在第1行,Alloway在第2行
公共类zipcodes项目{
//类的主要方法
公共静态void main(字符串[]args)引发FileNotFoundException{
布尔值isPresent=false;
整数索引=null;
扫描仪输入=新扫描仪(系统输入);
while(true){
System.out.println(“请输入邮政编码:”);
字符串输入=in.nextLine();
ZipList zipcodesObject=新ZipList();
zipcodesObject.ZipsandProperties();
整数=整数.valueOf(输入);
}
}
}
包ZipCodes项目;
导入java.io.FileNotFoundException;
导入java.io.File;
导入java.util.Scanner;
公共类拉链{
public void ZipsandProperties()引发FileNotFoundException{
File zipcodes=新文件(“zipcodes.txt”);
扫描仪填充=新扫描仪(zipcodes);
字符串[]文件数据=新字符串[4000];
整数计数=0;
while(infle.hasNextLine()){
fileData[++count]=infle.nextLine();
System.out.println(fileData[count]);
}
}
}
包ZipCodes项目;
公共类ZipCodes{
int-zip;
弦乡;
字符串状态;
ZipCodes(整数z、字符串tship、字符串ste){
zip=z;
乡镇=tship;
状态=ste;
}
}
您可以先将从文件中获取的字符串解析为zip、town和state,然后创建ZipCodes类型的数组,而不是创建名为fileData的字符串数组
public void ZipsandProperties() throws FileNotFoundException{
File zipcodes = new File("zipcodes.txt");
Scanner inFile = new Scanner(zipcodes);
ZipCodes[] fileData= new ZipCodes[4000];
int count=0;
while (inFile.hasNextLine()){
String line = inFile.nextLine();
String [] temp = line.split(" ");//I assumed that your file content is like "<zip code> <township> <state>"
ZipCodes zc = new ZipCodes(Integer.parseInt(temp[0]),temp[1],temp[2]));
fileData[++count] = zc;
}
}
public void ZipsandProperties()引发FileNotFoundException{
File zipcodes=新文件(“zipcodes.txt”);
扫描仪填充=新扫描仪(zipcodes);
ZipCodes[]fileData=新ZipCodes[4000];
整数计数=0;
while(infle.hasNextLine()){
字符串行=infle.nextLine();
String[]temp=line.split(“”;//我假设您的文件内容如下“”
ZipCodes zc=新的ZipCodes(Integer.parseInt(temp[0]),temp[1],temp[2]);
fileData[++count]=zc;
}
}
然后,可以使用比字符串数组更灵活的数组。您可以使用给定的输入对其进行迭代 我不知道这对你来说是否足够好,或者至少它能引导你找到你需要的解决方案: ZipCodeProject.java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ZipCodeProject {
public static void main(String[] args) {
String zipFileNamePath = "path/to/your/file/zipcodes.txt";
try {
List<ZipCode> zipCodes = loadZipFile(zipFileNamePath);
int option = -1;
Scanner optionScanner = new Scanner(System.in);
while(option != 4) {
printMenu();
option = optionScanner.nextInt();
switch (option) {
case 1:
for (ZipCode zipCode : zipCodes) {
System.out.println(zipCode.toString());
}
break;
case 2:
// logic to find a zip code
break;
case 3:
System.out.println("operation not supported");
break;
case 4:
System.out.println("Byeee");
break;
default:
System.out.println("error, select a number from the menu");
}
}
} catch (FileNotFoundException exception) {
System.out.println("File not found: " + zipFileNamePath);
}
}
public static void printMenu() {
System.out.println("Menu");
System.out.println("1 - Print all zip codes.");
System.out.println("2 - Find a zip code.");
System.out.println("3 - Add new zip code.");
System.out.println("4 - Quit.");
System.out.println("Select the menu number");
}
public static List<ZipCode> loadZipFile(String zipFileName) throws FileNotFoundException {
File zipFile = new File(zipFileName);
Scanner inFile = new Scanner(zipFile).useDelimiter("\n");
List<ZipCode> zipCodes = new ArrayList<>();
while (inFile.hasNextLine()) {
String[] zipCodeProperties = new String[3];
int count = 0;
while (count < 3) {
zipCodeProperties[count] = inFile.next();
count++;
}
ZipCode zipCode = new ZipCode(Integer.parseInt(zipCodeProperties[0]), zipCodeProperties[1], zipCodeProperties[2]);
zipCodes.add(zipCode);
}
return zipCodes;
}
}
zipcodes.txt:
08001
Alloway
Salem County
08002
Cherry Hill
Camden County
执行:
Menu
1 - Print all zip codes.
2 - Find a zip code.
3 - Add new zip code.
4 - Quit.
Select the menu number
1
ZipCode{zip=8001, town='Alloway', state='Salem County'}
ZipCode{zip=8002, town='Cherry Hill', state='Camden County'}
Menu
1 - Print all zip codes.
2 - Find a zip code.
3 - Add new zip code.
4 - Quit.
Select the menu number
4
Byeee
我希望这会有帮助,如果有任何问题,就直接回答:)
ps:查看回复,我注意到邮政编码是一个int,在您的描述中,它以0开头,int中的前导0被删除。如果您需要邮政编码与前导0一起使用,那么您应该将该变量作为字符串,如果它只是数字,并且前导0不需要注册,您可以保持原样:D您允许重写
equals()
并使用它吗?我想可以。我只需要确保代码正常工作。该项目的主要目的是拥有多个类文件。这就是全部。我们如何使代码工作取决于我们自己。thnxI似乎对ZipCodes zc=……产生了错误,文件的数据是它们自己的行。例如键入一个数字,然后按enter键并写入另一个数字
Menu
1 - Print all zip codes.
2 - Find a zip code.
3 - Add new zip code.
4 - Quit.
Select the menu number
1
ZipCode{zip=8001, town='Alloway', state='Salem County'}
ZipCode{zip=8002, town='Cherry Hill', state='Camden County'}
Menu
1 - Print all zip codes.
2 - Find a zip code.
3 - Add new zip code.
4 - Quit.
Select the menu number
4
Byeee