Need help with this practice java question
Hello guys,
Im having some problem with this question
Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character. (Recall that lines are terminated with the "\n" character.) Assume that the argument contains at least two complete, newline-terminated lines.
This is my code:
public String getSecondLine(String input)
{
return input.substring(input.indexOf("\n"),input.indexOf( input.substring(input.indexOf("\n")-1)));
}
I am getting this error message:
Exception java.lang.StringIndexOutOfBoundsException: String index out of range: -1 occurred
Can someone help me? Thanks!
Re: Need help with this practice java question
Quote:
Originally Posted by
ZenithX
Hello guys,
Im having some problem with this question
Write a method, getSecondLine, that is passed a String argument and that returns the second line, without its newline character. (Recall that lines are terminated with the "\n" character.) Assume that the argument contains at least two complete, newline-terminated lines.
This is my code:
Code Java:
public String getSecondLine(String input)
{
return input.substring(input.indexOf("\n"),input.indexOf(input.substring(input.indexOf("\n")-1)));
}
I am getting this error message:
Exception java.lang.StringIndexOutOfBoundsException: String index out of range: -1 occurred
Can someone help me? Thanks!
String API subString
look at the subString api your looking for the \n so
Code Java:
String str = "The dog ran through the fence \nafter the rabbit.\n\r"
you found the first section of the first string's last position, out put the substring(beginning) ;
where the beginning is the number you located.
your on the right track but just not quite there. What the error is telling you is that your position is larger than the String itself.
Re: Need help with this practice java question
So from what i understand, there is a problem in the bolded area? return input.substring(input.indexOf("\n"),input.indexOf(input.substring(input.indexOf("\n")-1)));
Also, how do i check if the string is null?
Re: Need help with this practice java question
Quote:
Originally Posted by
ZenithX
So from what i understand, there is a problem in the bolded area? return input.substring(input.indexOf("\n"),input.indexOf(input.substring(input.indexOf("\n")-1)));
Also, how do i check if the string is null?
you can check for null by string == null or better yet string.equal(null);
but yes the problem is in bold
if you did the review on the api's then you will see that you get the int back for position
create a new string for the second line with the value that was returned...
why try to save space and combine everything spread out for you can see what your doing
Code Java:
//shows that you don't have to place things on the same line this
//will only confuse yourself spread the wealth and readability
int value = input.substring(input.indexOf("\n");
String second = //... you know what goes here.
System.out.println("My next String "+second);
you think this can be created in to a method?? Also be recursive?
Re: Need help with this practice java question
Thanks William! i figured it out!
it makes more sense now.