Jump to content

Loading files?

- - - - -

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

#1
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
I have a text file I need to load in using a jFileChooser. It is a .txt file which contains data about different types of workers. The file always has the same format:

The first 3 lines are a certain type of worker. All the information on a particular worker are kept on one line, delimited by commas. Lines are seperated by a return.
The next 4 lines of the file are a different type of worker, with different information stored about them.
And the final 2 lines of the file similarly store information on a different type of worker, in the same manner as above.

The information is to be read in, split up and stored in different objects of the specific type of worker. i.e. there is a field in the object for each type of worker for each piece of information delimited by a string.

Would I use a jFileChooser to navigate to the file and then use a buffered reader in a for loop to process the relevant number of lines and then from there store the values in the objects?

Anyone have any tips on this? Not really too sure where to begin...

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Yes, use JFilechooser. It doesn't make much difference code-wise. without the filechooser you would write the filepath down into the code. And now the filechooser gives you the file

private JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));

int returnVal = fileChooser.showOpenDialog(frame);


        if(returnVal != JFileChooser.APPROVE_OPTION) 

            //user pressed cancel  or closed filechooser

        else

        File selectedFile = fileChooser.getSelectedFile();

There is a setting somewhere to limit the filechooser to only let the user pick txt-files. but i can't recall that at the moment.

Then i would use a bufferedreader and read lines
(string line = bufferedReader.readline() )
since lines are delimited by commas it's pretty easy.

String [] stringArray = line.split(",")

from java.sun.com:

Quote

The string "boo:and:foo", for example, yields the following results with these expressions:

Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
where Regex = the paremeter from the split method.
Then use the stringArray to fill the worker objects. (A view of the txt-file you're reading would be welcome if you need more help)

#3
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Thank you, I'll pm a file to you as I don't really want to post it.

#4
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
I have a new question unrelated to the original post but don't want to spam the forum with new threads lol...

Basically, is it possible to create new objects in a for loop?

i.e.

FOR 1-3
create new object
...rest of method...

so that each time the loop loops, a new object is created? i.e. here an object is created, the rest of the method occurs, then it loops round again and a second object is created?

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Yeah, no problem.
for (int iii = 0; iii < 10; ++iii)
{
    MyObject o = new MyObject(properties);
    // do stuff...
}

Wow I changed my sig!

#6
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
But wouldn't that give the name o to every new object?

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Objects only exist between the { } where they are made. so object o would exist only untill the next loop. (java's internal garbage collection deletes objects where nothing is referenced at i think.)
You would indeed give them all the name o. But there is no reference whatsoever to any of the objects so it doesn't matter.

You probably want to remember them, thus create an array outside the for loop.
WorkerA[] workerArrayA = new WorkerA[9]

for(int i=0 ; i<9 ; i++){
  workerArrayA[i] = new WorkerA(...)
}

Edited by wim DC, 11 January 2010 - 07:23 AM.
[/code] tag


#8
ZipOnTrousers

ZipOnTrousers

    Learning Programmer

  • Validating
  • PipPipPip
  • 94 posts
Thanks, if all goes to plan this should really help me finish this application...