This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split string doesn't work

 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of printing Oops, try printing the exception's stack trace (e.printStackTrace()). It will be a lot more helpful.
 
Maneesh Godbole
Saloon Keeper
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic