Results 1 to 4 of 4
  1. #1
    althair is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Tokenizing textfile content into several Tables (Float[])

    Hi,

    I have a file (selected via JFileChooser), where content is
    ...<some unnecessary text>...
    A B C
    0.004 0.344 0.124
    5.003 0.412 8.299
    1.993 0.234 5.3333
    0.234 ...... ......
    .....
    .....<some unnecessary text>

    Number of whitespaces between various "numbers" (tokens) is NOT fixed but in each line I have 3 tokens.

    I want to create 2 tables:
    - Float[] tblA - containing all tokens from "A" column and
    - Float[] tblB - for "B" column.

    I was thnking about StreamTokenizer but also found out class Scanner and String method split(). Anyway, I cannot obtain the result I want in any of these methods.
    Can you tell me the most effective way to:
    - cut unnecessary part
    - convert tokens from file into 2 tables (parsed into Float)
    Last edited by althair; 12-29-2009 at 02:18 PM. Reason: cut last sentence

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    I would suggest to use a BufferedReader for obtaining the values. This would allow you to read in the lines of the text where you could skip the beginning unnecessary lines.

    You can then just split the lines with the floats using the String split command and parse the values from there.

    Pseudocode(Untested)
    Java Code:
    FileReader reader = new FileReader(file);
    BufferedReader br = new BufferedReader(reader);
    boolean eof = false;
    int lineNumber = 0;
    while(!eof) {
         String line = br.readLine();
         if (line == null) {
               break;
         }
         lineNumber++;
         if (lineNumber > NumberOfUnnecessaryLines) {
               String[] floatValues = line.split(" ");
               float a = Float.parseFloat(floatValues[0]);
               float b = Float.parseFloat(floatValues[1]);
               Add a to array(Or something similar)
               Add b to array(Or something similar)
         }
    }
    The above code currently will probably run deeper into the file then you want, you would need to think of a way to escape the while loop by setting the eof to true. This would be done when you reach the end of the float values and can be determined either by a line number or when the parsing or splitting throw an exception.

  3. #3
    althair is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Thanks for help :).

    I managed to cut the unnecessary lines (I used line.trim().startsWith("=") condition in order to cut unnecessary ending, which appeared just 1 line: "=======" :) ).

    Ufortunately, when I wrote
    Java Code:
     String[] floatValues = nextLine.split(" ");
                Float[] tmp1=null, tmp2=null;
                int k=0;
               float a = Float.parseFloat(floatValues[0]);
               float b = Float.parseFloat(floatValues[1]);
               tmp1[k]=a;
               tmp2[k]=b;
               k++;
    I obtain Exception (message from NetBeans): java.lang.NumberFormatException: empty String

    What is wrong here?

  4. #4
    althair is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    OK, the issue has been solved. I present the full code below:

    Java Code:
    import javax.swing.*;
    import java.io.*;
    import java.util.ArrayList;
    
    private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {                                        
                chooser = new JFileChooser();
                int returnVal = chooser.showOpenDialog(this);
                File file1 = chooser.getSelectedFile();
                String nextLine = "";
                String FileName = file1.getName();
    //I decided to use ArrayList here. I create 3 different arraylists
                ArrayList<Float> data1 = new ArrayList<Float>();
                ArrayList<Float> data2 = new ArrayList<Float>();
                ArrayList<Float> data3 = new ArrayList<Float>(); //data 3 is not necessary - it is more "Dummy" arraylist
       try {
        FileReader fr = new FileReader(file1);
        BufferedReader br = new BufferedReader(fr);
        while((nextLine = br.readLine()) != null) {
    
    //the following conditions correspond solely to my file; it would be great if somebody could write me more enhanced way of "expressing" it
         if ((nextLine.trim().startsWith("0")||
                nextLine.trim().startsWith("1")||
                nextLine.trim().startsWith("2")||
                nextLine.trim().startsWith("3")||
                nextLine.trim().startsWith("4")||
                nextLine.trim().startsWith("5")||
                nextLine.trim().startsWith("6")||
                nextLine.trim().startsWith("7")||
                nextLine.trim().startsWith("8")||
                nextLine.trim().startsWith("9"))&&
                (!nextLine.trim().startsWith("702 SM"))){
         String[] theline = nextLine.trim().split(" +");//(" +") means "1 or more white spaces"
         data1.add(Float.parseFloat(theline[0]));
         data2.add(Float.parseFloat(theline[1]));
         } }
    }
       catch(FileNotFoundException fN) {
        fN.printStackTrace();
       }
       catch(IOException e) {
        System.out.println(e);
    }
    
           System.out.println(data1);
           System.out.println(data2);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    lblPath.setText(FileName);
                }
             
    }

Similar Threads

  1. Replies: 2
    Last Post: 05-30-2009, 10:42 AM
  2. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 12:11 PM
  3. String tokenizing with Scanner
    By vijaygk in forum Advanced Java
    Replies: 2
    Last Post: 07-15-2008, 04:44 AM
  4. Tokenizing with Scanner
    By sireesha in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 08:44 PM
  5. Textfile and GUI problems
    By saytri in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 04:08 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •