Java,文件中的模式匹配和排序

Java, Pattern matching and sorting from a file

我正在尝试通读给定的文本文件并使用基于给定地址的模式匹配对其进行排序,在通读文件时虽然我在第 45 条第 5 条线上收到一个奇怪的 NumberFormatException 错误并且我不理解这个错误意味着什么以及为什么它从这条线发生而不是从另一条线发生,比如 22 broadway 打印出来的很好。我还应该为此使用扫描仪还是 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.run(Unknown Source)
    at sun.rmi.transport.Transport.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[=13=](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