在Txt文件中搜索和匹配(Java)
大家好,请问有人能帮我完成这个计划吗 编写一个程序,要求用户输入邮政编码,并返回该地区的城市 邮政编码如果中的邮政编码不在列表中,则应返回city not found。 查找城市代码必须位于单独的方法findCity()中 用户应该能够继续输入邮政编码,直到他们输入9999以表示他们已经输入了邮政编码 已完成(9999不应显示为“未找到城市”) ================================================ 在txt文件中: 丹德农3175 弗兰克斯通3199 伯威克3816 克兰伯恩3977 玫瑰花蕾3939在Txt文件中搜索和匹配(Java),java,search,matching,Java,Search,Matching,大家好,请问有人能帮我完成这个计划吗 编写一个程序,要求用户输入邮政编码,并返回该地区的城市 邮政编码如果中的邮政编码不在列表中,则应返回city not found。 查找城市代码必须位于单独的方法findCity()中 用户应该能够继续输入邮政编码,直到他们输入9999以表示他们已经输入了邮政编码 已完成(9999不应显示为“未找到城市”) ================================================ 在txt文件中: 丹德农3175 弗兰克斯通3199
到目前为止我就是这么做的
import java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner
public class test2 {
public static void main(String[] args) throws FileNotFoundException
{
try
{
File f = new File("Files\\cities.txt");
Scanner input = new Scanner(f);
String text;
while(input.hasNextLine())
{
text = input.nextLine();
process(text);
}
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
}
public static void process(String text)
{ String name = null;
int id;
Scanner code = new Scanner(System.in);
System.out.println("enter the postcode");
id = code.nextInt();
Scanner data = new Scanner(text);
if(code.equals(0))System.out.println(name);
name = data.next();
id = data.nextInt();
while(data.hasNextDouble())
{
}
System.out.println(name+ " ");
// System.out.println(id+ " ");
}
}
这里有一个直截了当的方法: 首先,您希望用户输入密码。如果
passcode
小于9999,则需要搜索文本文件以查找具有该密码的城市。这件事可以实现为:
int passcode = 5; // Suppose passcode is 5. You may suppose any value lesser than 9999
Scanner input = new Scanner(System.in);
// Ask user to enter a passcode. If user enters 9999 the while loop is exited
while(passcode < 9999)
{
System.out.println("Enter passcode: ");
passcode = input.nextInt();
// donot search if passcode is greater than or equal to 9999
if(passcode < 9999)
searchCity(passcode);
}
试着把你的问题分解成子问题。在开始输入代码之前做一些书面工作。通过这种方式,事情变得简单多了。filef=newfile(“d:\\cities.txt”);
File f = new File("d:\\cities.txt");
Scanner input = new Scanner(f);
Map<Integer,String> cityCode = new HashMap<Integer,String>();
String text;
while(input.hasNextLine())
{
text = input.nextLine();
Scanner data = new Scanner(text);
String name = data.next();
int id2 = data.nextInt();
cityCode.put(id2, name);
}
System.out.println("enter the postcode");
Scanner code = new Scanner(System.in);
int id = code.nextInt();
if(cityCode.containsKey(id)) {
System.out.println(cityCode.get(id));
} else {
System.out.println("City Not found");
}
扫描仪输入=新扫描仪(f);
Map cityCode=new HashMap();
字符串文本;
while(input.hasNextLine())
{
text=input.nextLine();
扫描仪数据=新扫描仪(文本);
字符串名称=data.next();
int id2=data.nextInt();
cityCode.put(id2,名称);
}
System.out.println(“输入邮政编码”);
扫描仪代码=新扫描仪(System.in);
int id=code.nextInt();
if(城市代码containsKey(id)){
System.out.println(cityCode.get(id));
}否则{
System.out.println(“未找到城市”);
}
您需要什么帮助?你被困在哪里?代码似乎不完整您使用相同的变量(id
)来获取用户输入,然后获取扫描仪数据非常感谢,是的,我需要处理这个问题,因为我在开始之前不做书面工作。
File f = new File("d:\\cities.txt");
Scanner input = new Scanner(f);
Map<Integer,String> cityCode = new HashMap<Integer,String>();
String text;
while(input.hasNextLine())
{
text = input.nextLine();
Scanner data = new Scanner(text);
String name = data.next();
int id2 = data.nextInt();
cityCode.put(id2, name);
}
System.out.println("enter the postcode");
Scanner code = new Scanner(System.in);
int id = code.nextInt();
if(cityCode.containsKey(id)) {
System.out.println(cityCode.get(id));
} else {
System.out.println("City Not found");
}