Stringtokenizer and file reading

J

Juggernaut

Hi
I have a that stores cd's in an array with title, artist, runtime etc.

Now I want to try and write it to file and that works just fine.

But when I read the file and place the variable into the array again, but im
not sure how to do this. I know I have to use stringtokenizer but I just
cant find a good example to explain this to me so I was wondering if anyone
could help

Lets say the file is like this:

--
Bill myers the album
Bill Myers
Rock
56

Kaizers orchestra
Johny J
Rock
74
--

Now these are 2 cd's..and I want to put it into the variables cd name,
artist, genre and runtime.
But how do I do this? It seperates with new line, but what when it comes to
the blank line before the next one?

Could anyone give me a working example of how to accomplish this?

Thanks in advance :)
 
R

Roedy Green

S

Stefan Waldmann

Juggernaut said:
Hi
I have a that stores cd's in an array with title, artist, runtime etc.
[snip]

Hello,

why would you want to store the data in an array? Since Java is an
object oriented language, create an Album class and use this instead of
the array!

public class Album
{
private String title;
private String artist
private String genre;
private int year

public Album(String _title, String _artist
String _genre, int _year)
{
this.title = _title;
this.artist = _artist
this.genre = _genre;
this.year = _year;
}

public String getTitle()
{
return this.title;
}

public int getYear()
{
return this.year;
}

// .. and so on, getter for each field

// and if you need: setter methods, f.i.:
public String setTitle(String _title)
{
this.title = _title;
}
}


Now you can create Album objects and work with them like

Album album = new Album("Bill Myers the album", "Bill Myers",
"Rock", 1956);

System.out.println(album.getTitle());

Doesn't this look much more readable than

System.out.println(myAlbumArray[0]);

???


Regards, Stefan
 
M

Martin Winkelbauer

Juggernaut said:
Hi
I have a that stores cd's in an array with title, artist, runtime etc.

Now I want to try and write it to file and that works just fine.

But when I read the file and place the variable into the array again, but im
not sure how to do this. I know I have to use stringtokenizer but I just
cant find a good example to explain this to me so I was wondering if anyone
could help

Lets say the file is like this:

--
Bill myers the album
Bill Myers
Rock
56

Kaizers orchestra
Johny J
Rock
74
--

Now these are 2 cd's..and I want to put it into the variables cd name,
artist, genre and runtime.
But how do I do this? It seperates with new line, but what when it comes to
the blank line before the next one?

Could anyone give me a working example of how to accomplish this?

Thanks in advance :)
I would prefer to organize the file like a database row for string-tokenizer
access like:

Bill myers the album|Bill Myers|Rock|56
Kaizers orchestra|Johny J|Rock|74

with some sort of delimiter between attributes.

now you read in line after line and create a StringTokenizer with it
(with delimiter whatever you choosed) and with the getNextToken - method you
should get the desired pieces.

But the class - approach would be more objectOriented of course.

hth
Martin
 
T

TechBookReport

Hi
I have a that stores cd's in an array with title, artist, runtime etc.

Now I want to try and write it to file and that works just fine.

But when I read the file and place the variable into the array again, but im
not sure how to do this. I know I have to use stringtokenizer but I just
cant find a good example to explain this to me so I was wondering if anyone
could help

Lets say the file is like this:

--
Bill myers the album
Bill Myers
Rock
56

Kaizers orchestra
Johny J
Rock
74

Create a CD class and then use serialization?

Pan
 
R

Roedy Green

Bill myers the album|Bill Myers|Rock|56
Kaizers orchestra|Johny J|Rock|74

I don't like that.
First you steal a character that you may want to use as data.

Next if these get long, you have to parse to find the field you are
after.

Next these can't be easily fed to a sort.

Next there is no ability to intern, saving 200 copies of the string
"Elvis".


You need to write extraction methods, why not just have String fields,
amount to pointers into the string. where each field starts.
 
A

Axl

Use of StringTokenizer is "discouraged" according to JDK 1.4.2.

Use String[] ary = str.split("delimiter goes here");

"StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split method of String
or the java.util.regex package instead."


Axl

Axl Weslowski
aweslowski_@_rpa.com
(remove_the_underscores)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top