【问题标题】:Java, Pattern matching and sorting from a fileJava,模式匹配和文件排序
【发布时间】:2023-04-02 02:13:01
【问题描述】:

我正在尝试通读给定的文本文件并使用基于给定地址的模式匹配对其进行排序,当阅读文件时,虽然我在第 45 条第 5 大道行遇到了一个奇怪的 NumberFormatException 错误,但我不明白这是什么错误意味着以及为什么它发生在这条线上,而不是像 22 百老汇这样的另一条打印出来的很好。我也应该为此使用扫描仪,还是缓冲阅读器可以用于该项目,以及如何存储非模式匹配行以便稍后在输出中作为不匹配的地址输出?

文本文件

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)
> 

【问题讨论】:

  • 你用逗号分割,你想要解析的值的好行在第二列,但坏行似乎没有对齐同一个 GIGO?
  • 此行格式不正确6578-FGH-9845 ,,45 5th Ave, 12.2,
  • 谢谢大家的建议!在调整文本文档修复了这个问题时,这个不正确的格式被给了我,所以我想知道是否有办法解决它并且它仍然能够正常运行,或者你认为这个给定的文本文档是唯一的这里有问题?

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


【解决方案1】:

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

【讨论】:

  • 好像没有跳过什么,值是第三列应该是Float
  • 双逗号,, 导致部分被跳过
  • 感谢您的帮助!虽然给了我这个错误全文,但我相信这可能在很大程度上解决了这个问题,但现在我在读取678-FGH-9846 ,45 5th Ave, 12.2 行之后得到一个java.lang.ArrayIndexOutOfBoundsException: 1 并且似乎无法继续通过完全空白的空间行即使当我到达那条线时我应该用String pkgId = result[0].trim().toUpperCase(); 剪掉那条线
  • 大部分错误如下java.lang.ArrayIndexOutOfBoundsException: 1 at csi311.HelloCsi311.readFile(HelloCsi311.java:51) at csi311.HelloCsi311.run(HelloCsi311.java:29) at csi311.HelloCsi311.main(HelloCsi311.java:87)
  • trim() 不会过滤掉它,您需要在 while ((line = br.readLine()) != null) 的顶部添加一个 if 语句检查。在您的 while 顶部添加 if (line.trim().isEmpty()) continue;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2017-01-08
  • 2017-12-09
  • 2021-10-07
相关资源
最近更新 更多