Jump to content

splitting up a textfile

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
I have a series of textfiles that I want to split up

the textfiles look like this

I have a class called result and it has following fields:

String name (the name of the pilot, ex: mika hakkinen )
String team (the name of the team, ex: mclaren-mercedes)
String time (the time of the pilot, ex: 5m 11.783s or +3.423s)

I want to add object of this class to an ArrayList, reading it from the file

loop{
new result(name,team,time);
}

what methods and functions for splitting and reading the file would be best to use?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Given that it looks like you have fixed field widths in your source file, you should be able to use the substring and trim methods to extract the data you want.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Hello,

do you plan to use the text-file for something else aswell?
When i created a little game with highscores i simply wrote the highscore to a file. Note that it was a binary file and thus can't be edited by opening it in notepad or something. So you could make an arrayList of results. And then simply write the whole arrayList with 2 lines of code. So you won't be adding it to the files line by line.
outputStream = new ObjectOutputStream(new FileOutputStream("highscore.dat"));
outputStream.writeObject(highscores);
(you'll need to add a try{} and some catches...)
Here 'highscores' is the name of the arrayList, 'highscore.dat' is the name of the (binary) file. I never tested if it would work with txt-files. But why wouldn't it? ^^

Only "problem" you may have is. Since it writes it away as an object you can't manually edit the file in notepad.
So you must make it manually in java.

Back to the code. Once it's written away you can read it back like this:
try
        {
            inputStream = new ObjectInputStream(new FileInputStream("highscore.dat"));
            highscores = (ArrayList<String>) inputStream.readObject();
        }
        catch (FileNotFoundException e)
        {
            resetHighScore();
        }
        catch (IOException e)
        {
            System.err.println ("Fout bij het lezen");
            resetHighScore();
        }
         catch(ClassNotFoundException e){
            resetHighScore();
            System.err.println ("Kan file nietlezen");
        }
        finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            }
             catch (IOException e)
            {
            System.err.println ("Kan file niet openen / lezen");
            }
        }
(error println is in dutch^^)
the 2 lines in the try{} are most important.
note: instead of
highscores = (ArrayList<String>) inputStream.readObject();
you will have to cast it into ArrayList<Result> instead of String. (you can probably reduce the number of catches by using catch (Exception e) and remove the others.


It's also possible by reading the Strings of the txt file, line by line, trimming them etc. But this is waaay easier :p

#4
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
I did a highscoresystem ones that is very similar to yours, so that's not a problem

my problem is splitting my file up so I can use it and make some statistics of all files I put into the program (there are multiple textfiles like the one I gave as an example)

I have succeeded to load my file into a string, but now the trim function isn't working :s

this is my code:

package resultmanagement;

import javax.swing.*;
import java.io.*;

public class ResultProcessor extends JFrame {

    private String location = "C:\\Games\\Grand Prix 4\\TEXT.txt";
    private JFileChooser chooser;

    public ResultProcessor() {        
        chooser = new JFileChooser();
        split();
    }

    public void split(){
        String text = openFile();

        String gpname = "";

        gpname = text.substring(254, 334);
        gpname.replaceAll(" ", "");

        System.out.println("gpname: " + gpname);
    }

    private String openFile() {
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal != JFileChooser.APPROVE_OPTION) {
            return "fail";  // cancelled
        }
        File bestand = chooser.getSelectedFile();

        BufferedReader reader = null;
        String text = "";
        try {
            reader = new BufferedReader(new FileReader(bestand));
            String line = reader.readLine();
            while (line != null) {
                text += line;
                line = reader.readLine();
            }
        } catch (FileNotFoundException fne) {
            System.out.println("Kan de file " + bestand.getName() + "niet openen: " + fne.getMessage());
        } catch (IOException ioe) {
            System.out.println("Problemen bij het lezen van de file: " + ioe.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ioe) {
                    System.out.println("Problemen bij het sluiten van de file: " + ioe.getMessage());
                }
            }
        }
        return text;
    }
}

when I run it on the file I used in the first post I get:

gpname:                 Australia - Albert Park                                         
(with spaces)

but I want it to be

gpname: Australia - Albert Park
(without spaces)

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You have to break the string into two pieces, then trim, then reassemble. Alternatively, you could repeatedly replace " " with " ".
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
I don't realy get it, normaly the trim should delete all spaces in front and afther the characters

"gpname: " is only added in the printline so it doesn't affect the trimming
also the spaces afther "Australia - Albert Park" are still present, but the forum seems to delete them

#7
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I don't see a call to trim anywhere in your code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#8
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
oh sorry, my fault, I have tried trim, but since that didn't work I used:

gpname.replaceAll(" ", "");

but that also didn't work

#9
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Probably because you have to make

gpname = gpname.replaceAll(" ","");

because replaceAll will return a string, and you have to save that returned value.

#10
Shaddix

Shaddix

    Programmer

  • Members
  • PipPipPipPip
  • 102 posts
you where correct, noobish mistake from me

thanx for helping!

#11
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
No problem.. that happened to me too :) when I was started learning Java