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 6 of 6

Thread: Reversing a String

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Reversing a String

    In this code, I ask the user to input their name. For example, "John Smith". I would first reverse their name as "Smith John" That part works. Now, to make this "program" better, I added a couple of lines:
    String newFirst2= "";
        	for (int i = newFirst.length()-1; i >=0; i--)
        	{
        		newFirst2 = newFirst2+newFirst.charAt(i);
        	}

    and

    String newLast2= "";
        for (int i = newLast.length()-1; i>=0; i--)
        {
        	newLast2=newLast2+newLast.charAt(i);
        }

    If everything works, the output should be htimS nhoJ, but I am getting an error. I can compile, but cannot run (Error after imputing name) Also, I know that I could have done the 2 flips in one method, but I wanted more practice with Methods.

    import java.util.Scanner;
     
    public class Application {
     
    	public static void main(String[] args) {
    		Scanner scanner2 = new Scanner(System.in);
    		System.out.print("Enter your name here: ");
    		String a = scanner2.next();
    		Flip(a);
    	}
        public static void Flip(String name)
        {
        	int a = name.indexOf(' ');
        	String newFirst = (name.substring (a+1, name.length()));
        	String newFirst2= "";
        	for (int i = newFirst.length()-1; i >=0; i--)
        	{
        		newFirst2 = newFirst2+newFirst.charAt(i);
        	}
        	Flip2(newFirst, name, newFirst2);
        }
        public static void Flip2(String newFirst, String name, String newFirst2)
        {
        int a = name.indexOf(' ');
        String newLast = (name.substring (0, a));
        String newLast2= "";
        for (int i = newLast.length()-1; i>=0; i--)
        {
        	newLast2=newLast2+newLast.charAt(i);
        }
        System.out.println ("Your name is: " + newFirst + " " + newLast);
        System.out.println ("Your second name is: " + newFirst2 + " " + newLast2);
        }
    }

    Error:
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1911)
    at Application.Flip2(Application.java:25)
    at Application.Flip(Application.java:20)
    at Application.main(Application.java:9)
    Best Output (Intended Output):
    Your name is Smith John
    Your second name is htimS nhoJ

    Thanks for the help!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reversing a String

    The error is telling you that the second line of:

    int a = name.indexOf(' ');
    String newLast = (name.substring (0, a));

    is being fed an invalid index, namely '-1'. The invalid index is 'a', so why does the first line assign -1 to 'a'? Review the API to determine when indexOf() returns a -1.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    hkfrenchtoast (February 16th, 2014)

  4. #3
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Reversing a String

    I did some googling and I found out that indexOf() would return a -1 if the thing inside the () is not found. However, this is not true in my code. When I enter a name, I would type John Smith. There is a space between the n and the S, so I shouldn't be getting an error because of that.
    Also, my code worked when I didn't have the codes that try to reverse the Smith and the John words. (Did not have the for loops and the code that goes with it)

    Btw, I don't have a textbook, I am just learning how to code as a hobby.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reversing a String

    However, this is not true in my code.
    Either you're wrong, the reference you found is wrong (it's not), or the computer is executing the code incorrectly (it's not).

    Rather than assuming you know exactly what's going on, add a few print statements to 'see' what's going on behind the scenes. What are newFirst, name, and newFirst2 in Flip2 (method names should begin with lowercase letters)? Print them out and see. Then review the error (-1) and figure out how to correct for it.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    hkfrenchtoast (February 16th, 2014)

  7. #5
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Reversing a String

    Okay, I figured it out!

    Just a small line of code has such much effect, wow. All I had to do was change String a = scanner2.next() to String a = scanner2.nextLine();

    I don't really understand why though, do you mind explaining why? Thanks for the help.

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Reversing a String

    Figuring it out is a skill you need to be able to do on your own. I'll help you improve that skill, and then I'll help if you understand the rest if you need it.

    First, Google "java scanner doc", select the first result, which is the Java 7 API page for the Scanner class. It's okay if you're using Java 6 or some other version. Review the two methods next() and nextLine() and see if you can figure it out. If not, come back with questions.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    hkfrenchtoast (February 16th, 2014)

Similar Threads

  1. Reversing a String with no Buffer
    By C++kingKnowledge in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 11th, 2013, 12:54 AM
  2. Reversing a String with no Buffer
    By C++kingKnowledge in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 10th, 2013, 12:17 PM
  3. Reversing an Array
    By georger55 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 8th, 2012, 07:22 AM
  4. Reversing an array
    By severus1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 30th, 2011, 03:54 PM
  5. Reversing lines using LinkedList
    By velop in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 14th, 2011, 06:10 AM