gpt4 book ai didi

java - 分解文件并用java重写它

转载 作者:太空宇宙 更新时间:2023-11-04 14:24:46 27 4
gpt4 key购买 nike

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

文件的布局是这样的:

纽约纽约 005 063 090-149

等等与其他州...(注意纽约是一个由两部分组成的名称,以及它的 3 位邮政编码为 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 {
}
}
}

关于java - 分解文件并用java重写它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com