Java,模式匹配和文件排序

时间:2019-01-28 00:36:43

标签: java error-handling pattern-matching bufferedreader filereader

我试图读取给定的文本文件并使用基于给定地址的模式匹配对它进行排序,当我通过读取文件时尽管在第45条第5行上遇到了怪异的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)
> 

1 个答案:

答案 0 :(得分:0)

6578-FGH-9845 ,,45 5th Ave, 12.2,代码的关键部分被,分隔,您不小心在一个地方有两个逗号。这将导致它跳过一个节,并尝试将其格式化为数字。使其引发异常。更正后的行看起来像6578-FGH-9845, 45 5th Ave, 12.2