*
The moose likes Beginning Java and the fly likes split string doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "split string doesn Watch "split string doesn New topic
Author

split string doesn't work

John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Please check the below program

It creates a text file and some text printed to it. Then it's read and split method is run on each line to separate the words and to store it as instance variable values.




Keeping the above 3 lines in comments works fine and i get the below output:
>backstreet<
>love<
>5<
>jennifer<
>luqie<
>4<
>micheal<
>goodfight<
>3<
>bonjovi<
>itsmylife<
>5<
Oops...2

why is the exception thrown here?

Now when i run it without commenting.... i get:
output:
>backstreet<
Oops...2

Please help. It would be something silly but i'm not able to find it.
Thanks.
[ November 25, 2008: Message edited by: Maneesh Godbole ]

www.cs-repository.info
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3419
    
  12
Instead of printing Oops, try printing the exception's stack trace (e.printStackTrace()). It will be a lot more helpful.


Joanne
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

"john77eipe"
Welcome to the ranch. We are happy to have you here
On your way in, looks like you have missed out on ournaming policy.
Could you please adjust your screen name to comply to this policy?
You can do it by clicking here

Also, whenever you post your code, please do use the code tags. I have edited your original post by using the code tags. As you can see it makes the code much more readable and easier to understand for others.
[ November 25, 2008: Message edited by: Maneesh Godbole ]

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Originally posted by john77eipe:

String s[]= new String[4];
int i=0;
while((s[i]=br.readLine())!=null){
i++;
}



There is your problem...firstly I would stop hiding the actual exception from yourself by printing the stack trace.

Next ...why did you decide to use an array of strings to hold the current line from the text? You do not use it again. Think through the logic above you will see where your error lies.

For the second error consider:

music [] mc = new music[4];
mc[i].artist = t;

When you created the Array you created one object of type Array that has references to Objects of type music. Where are the music objects that you are now setting the artist property on?
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Originally posted by Joanne Neal:
Instead of printing Oops, try printing the exception's stack trace (e.printStackTrace()). It will be a lot more helpful.


Yup you are right. I was just practicing on Java so added those.... anyway as you pointed out it is important that I put something sensible over there.
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
Originally posted by Paul Yule:

When you created the Array you created one object of type Array that has references to Objects of type music. Where are the music objects that you are now setting the artist property on?


Thanks Paul. The problem was with Object initialization. Changing it to
music [] mc = { new music(), new music(), new music() };
works fine.
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
no problem. Glad I could help.

Also, if you need to keep the current line in a String array it will need to be 1 size bigger than the lines of text in your document with the last index of the array being set to null. This is due to the assignment coming before the comparison.

I.E. s[i]=br.readLine()!=null
[ December 01, 2008: Message edited by: Paul Yule ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: split string doesn't work
 
Similar Threads
sting tokenizer in a loop(vv urgent)
Head First Java HashSet
move file A to file B
Text File Help
Collections.sort() on Array List