Search code examples
javaerror-handlingpattern-matchingbufferedreaderfilereader

Java, Pattern matching and sorting from a file


I'm attempting to read through a given text file and sort through it using pattern matching based on address given, When reading through the file though im getting a weird NumberFormatException error on the 45 5th ave lines and im not understanding what this error means and why its occuring from this line but not from another like 22 broadway which prints out just fine. Also should i be using scanner for this instead or is bufferedreader okay to be using for this project and how could nonpattern matching lines be stored to be outputted later in the output as addresses that didnt match?

TEXT FILE

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

CODE SO FAR

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();
    }
   }

}

ERROR Im Recieving

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

Solution

  • 6578-FGH-9845 ,,45 5th Ave, 12.2, A crucial part of your code is splitting by , and you accidentally have 2 commas in one place. This causes it to skip a section and try to format it as a number; making it throw the exception. The corrected line would look like 6578-FGH-9845, 45 5th Ave, 12.2