我正在尝试读取给定的文本文件,并使用基于给定地址的模式匹配对其进行排序,但在读取文件时,我在第 45 条第五大道线上收到了一个奇怪的 NumberFormatException 错误,并且我不明白此错误的含义以及为什么它发生在这一行,而不是从另一行(例如 22 百老汇)打印出来。另外,我应该使用扫描仪来代替这个项目,还是可以使用 bufferedreader 来完成这个项目,以及如何存储非模式匹配行以便稍后在输出中作为不匹配的地址输出?
文本文件
123-ABC-4567, 15 W. 15th St., 50.1
456-BGT-9876,22 Broadway,24
QAZ-456-QWER, 100 East 20th Street,50
Q2Z-457-QWER, 200 East 20th Street, 49
6578-FGH-9845 ,,45 5th Ave, 12.2,
678-FGH-9846 ,45 5th Ave, 12.2
123-ABC-9999, 46 Foo Bar, 220.0
347-poy-3465, 101 B'way,24
到目前为止的代码
package csi311;
// Import some standard Java libraries.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloCsi311 {
/**
* Class construtor.
*/
public HelloCsi311() {
}
/**
* @param filename the name of a file to read in
* @throws Exception on anything bad happening
*/
public void run(String filename) throws Exception {
System.out.println("Hello world");
if (filename != null) {
readFile(filename);
}
}
/**
* @param filename the name of a file to read in
* @throws Exception on anything bad happening
*/
private void readFile(String filename) throws Exception {
System.out.println("Dumping file " + filename);
// Open the file and connect it to a buffered reader.
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = null;
String pattern = "^\\d\\d\\d-[A-Z][A-Z][A-Z]-\\d\\d\\d\\d";
String address = "\\d{1,3}\\s\\[A-Za-z]{2,20}";
// Get lines from the file one at a time until there are no more.
while ((line = br.readLine()) != null) {
String[] result = line.split(",");
for(String str : result)
{
String pkgId = result[0].trim().toUpperCase();
String pkgAddr = result[1];
Float f = Float.valueOf(result[2]);
if (!pkgId.matches(pattern)) {
}
else
System.out.println(str);
}
}
// Close the buffer and the underlying file.
br.close();
}
/**
* @param args filename
*/
public static void main(String[] args) {
// Make an instance of the class.
HelloCsi311 theApp = new HelloCsi311();
String filename = null;
// If a command line argument was given, use it as the filename.
if (args.length > 0) {
filename = args[0];
}
try {
// Run the run(), passing in the filename, null if not specified.
theApp.run(filename);
}
catch (Exception e) {
// If anything bad happens, report it.
System.out.println("Something bad happened!");
e.printStackTrace();
}
}
}
接收错误
java.lang.NumberFormatException: For input string: "45 5th Ave"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseFloat(Unknown Source)
at java.lang.Float.parseFloat(Unknown Source)
at java.lang.Float.valueOf(Unknown Source)
at csi311.HelloCsi311.readFile(HelloCsi311.java:52)
at csi311.HelloCsi311.run(HelloCsi311.java:29)
at csi311.HelloCsi311.main(HelloCsi311.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:362)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:121)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.evaluateSequence(StatementEvaluator.java:66)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:77)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:249)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:222)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
>
最佳答案
6578-FGH-9845 ,,45 5th Ave, 12.2,
代码的关键部分是用 ,
分割的,并且您不小心在一个地方有 2 个逗号。这会导致它跳过一个部分并尝试将其格式化为数字;使其抛出异常。更正后的线路将类似于 6578-FGH-9845, 45 5th Ave, 12.2
关于Java,从文件中进行模式匹配和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394231/