Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with this practice java question

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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!


  2. #2
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Need help with this practice java question

    Quote Originally Posted by ZenithX View Post
    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!
    String API subString

    look at the subString api your looking for the \n so
    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.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Member
    Join Date
    Jun 2011
    Location
    Rhode Island
    Posts
    69
    My Mood
    Bored
    Thanks
    11
    Thanked 7 Times in 6 Posts

    Default Re: Need help with this practice java question

    Quote Originally Posted by ZenithX View Post
    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
    //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?

  5. The Following User Says Thank You to william For This Useful Post:

    ZenithX (October 24th, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this practice java question

    Thanks William! i figured it out!
    it makes more sense now.

Similar Threads

  1. CS Practice Problems / Ideas for Prorams
    By Staticity in forum Java Theory & Questions
    Replies: 6
    Last Post: October 10th, 2011, 01:03 AM
  2. Event Handling - Best Practice?
    By ishtar in forum Java Theory & Questions
    Replies: 6
    Last Post: October 2nd, 2011, 08:52 AM
  3. Would the following be considered bad practice in Java?
    By Newbie_71 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 19th, 2011, 10:49 PM
  4. Looking for practice problems/assignments
    By tkgraham in forum Member Introductions
    Replies: 2
    Last Post: April 4th, 2011, 10:28 PM
  5. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM