• Post Reply Bookmark Topic Watch Topic
  • New Topic

String class methods question  RSS feed

 
Angel Kal
Greenhorn
Posts: 17
Chrome Netbeans IDE Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hope this can be seen, stuck on this last exercise for my java2 class (not homework...exercise for our own benefit).

I don't know why im getting the array out of bounds exception. The instructions say that you can assume that there is at least 1 line terminated with a newline character. If the first line is null (in the test case), does that mean it may be their error? Im just not sure why this is wrong. Any tips or explanations would be much appreciated.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could add an extra check as the first line in the method to check for null pointers: "if (arg==null) return null;" or something like that. You can simplify your code; the firstLine variable is redundant, i.e. "return arg.split("\n")[0];". So basically your method will only be 2 lines long.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid screenshots, which are very difficult to read.

Splitting on \n may not work correctly on all systems; Windows® uses \r\n as its line terminator, so you end up with lines ending with \r. I think \n and \r should be expunged from beginners’ vocabulary. You can probably split with [\r\n]+ (which will lose completely blank lines) or (better) System.getProperty("line.separator").
I am surprised you are suffering an ArrayIndexOutOfBoundsException; I would have thought you would suffer a NullPointerException from the split() method. Are you sure the software you are submitting your work to is executing it correctly?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question says,
Assume that the argument contains at least one complete, newline-terminated line.
That means they are not allowed to test your method by passing a null to it.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there are two errors apart from their use of \n. One: they are not allowed to pass null. Two: they have the wrong kind of Exception.
You have yourself some dubious code style; that method should for example probably be static because it never interrogates nor alters any instance data. And you can reduce that to one lineNote that if you pass a single line without a \n, you will receive that line unchanged as your return value.
Beware of returning null.
 
Angel Kal
Greenhorn
Posts: 17
Chrome Netbeans IDE Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of the replies. I did notice also that the exception was noted as logical, which didn't make any sense to me. I thought that the null test case was an error on their part because they did say assume there is at least one line. Just not knowing the full meaning of null though I was unsure if maybe i had interpreted it incorrectly or if it was in fact an error on their part. I tinkered with it for the last few days still to no avail, so I sent them a letter this morning.

I truly appreciate all of the help/replies I have been getting on this forum, as I am still kind of a noob. Im pursuing a software engineering degree right now, so it means a lot to actually understand my errors, and not just get the correct answer. I also will avoid the screen shot next time, thanks for the tip ;)
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to write whole programs, not only individual methods like that.

And “you’re welcome”
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!