在Txt文件中搜索和匹配(Java)

时间:2015-08-30 07:22:11

标签: java search matching

大家好,请问有人可以帮我解决这个问题吗?

编写一个程序,要求用户输入邮政编码并返回该城市 邮政编码。如果邮政编码不在列表中,那么它应该返回未找到的城市。 查找城市代码必须使用单独的方法findCity() 用户应该能够继续输入邮政编码,直到他们输入9999来表明它们 已完成(9999不应显示为“未找到城市”)

=============================================== = 在txt文件中:

丹德农3175 弗兰克斯通3199 贝里克3816 Cranbourne 3977 罗斯巴德3939

到目前为止我所做的一切。

import java.io.File;

import java.io.FileNotFoundException; import 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+ " ");
    }
}

2 个答案:

答案 0 :(得分:0)

这是一个直截了当的方法:

首先,您希望用户输入密码。如果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);
        }

searchCity()方法的工作方式如下:

public static String searchCity(int passcode) {
    Scanner citiesScanner = new Scanner(new File("Files\\cities.txt"));
    while(citiesScanner.hasNext()) {
      String city = citiesScanner.next();
      String pass = citiesScanner.next();
      if(Integer.parseInt(pass) == passcode) {
        return city;
      }
    }
    return "City not found";
}

试着将问题分解为子问题。在开始输入代码之前做一些文书工作。事情变得更加简单。

答案 1 :(得分:0)

        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");
        }