我正在尝试分解包含状态缩写,状态名称和邮政编码的文件。某些邮政编码仅为3位邮政编码,并且出于格式化目的,必须重写(例如005应该为005-005)。我需要帮助的是将州名和缩写与邮政编码分开,以便可以将3位邮政编码格式化为6位邮政编码。

文件的布局是这样的:

纽约纽约005063090-149

等等,以及其他州/省/自治区/直辖市/自治区/直辖市/自治区。(请注意,纽约是一个两部分名称,并且它的三位数邮政编码是005和063。需要重写为005-005和063-063)

这是我的代码:

    public class ZipsReader {
public static void main(String[] args){

    //Gets the file name and reads it
    try {
        //Prompts user for an input file
        Scanner console = new Scanner(System.in);
        System.out.println("Input file: ");
        String inputFileName = console.next();

        //Prompts user for an output file
        //System.out.println("Output file: ");
        //String outputFileName = console.next();
        //PrintWriter out = new PrintWriter(outputFileName);

        //Reads the selected file line for line
        File selectedFile = new File(inputFileName);
        Scanner in = new Scanner(selectedFile);
        while (in.hasNextLine()) {
            String line = in.nextLine();
            Scanner in2 = new Scanner(line);

            //Reads the selected file word for word
            while (in2.hasNext()){
                String state = in2.isLetter();
                String word = in2.next();
                    if (word.matches("\\d{3}-\\d{3}")){
                        System.out.println(word);
                    }
                        if (word.matches("\\d{3}")){
                            System.out.println(word + "-" + word);
                        }
            }
            in2.close();//closes the word scanner
        }
        console.close();//closes the file opener scanner
        in.close();//closes the line scanner
        //out.close();//closes the print writer
    }

    //Prints out message if file cant be found
    catch (FileNotFoundException e) {
        System.out.println("Sorry the file could not be found.");
    }

    //Needed to compile
    finally {
    }
}


}

.matches String方法可用于获取邮政编码,但是我不确定如何选择状态缩写。和名称与邮政编码分开。

现在,出于节省时间的原因,我现在只是在控制台上执行此操作,但是当我弄清楚了这一点后,我将对其进行修改以写入另一个文件。

我在这里先向您的帮助表示感谢

最佳答案

您可以尝试以下方法:

public class ZipReader {

public static void main(String[] args) {

    //Gets the file name and reads it
    try {
        //Prompts user for an input file
        Scanner console = new Scanner(System.in);
        System.out.println("Input file: ");
        String inputFileName = "G:\\test.txt";

    //Prompts user for an output file
        //System.out.println("Output file: ");
        //String outputFileName = console.next();
        //PrintWriter out = new PrintWriter(outputFileName);
        //Reads the selected file line for line
        File selectedFile = new File(inputFileName);
        Scanner in = new Scanner(selectedFile);
        String states="";
        while (in.hasNextLine()) {
            String line = in.nextLine();
            Scanner in2 = new Scanner(line);

            //Reads the selected file word for word
            while (in2.hasNext()) {
                //String state = in2.isLetter();
                String word = in2.next();
                if (word.matches("\\d{3}-\\d{3}")) {
                    System.out.println(word);
                }
                else if (word.matches("\\d{3}")) {
                    System.out.println(word + "-" + word);
                }
                else if(word.matches("[A-Z]{2}")){
                    System.out.println(word);
                }

                else{
                    states=states+word+" ";
                }
            }
            System.out.println(states+"\n");
            states="";
            in2.close();//closes the word scanner
        }
        console.close();//closes the file opener scanner
        in.close();//closes the line scanner
        //out.close();//closes the print writer
    } //Prints out message if file cant be found
    catch (FileNotFoundException e) {
        System.out.println("Sorry the file could not be found.");
    } //Needed to compile
    finally {
    }
  }
}

08-25 06:43