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

Thread: Apparent bug?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Apparent bug?

    Basically, the program is supposed to ask for the user's name and print it out in reverse and in all lowercase letters.

    However, when I run it, Eclipse makes the debugger open... I get "Source not found."

    Debugger shows this:

    NameReversal [Java Application]
    NameReversal at localhost:52896
    Thread [main] (Suspended (exception StringIndexOutOfBoundsException))
    String.substring(int, int) line: 1762
    NameReversal.main(String[]) line: 13
    /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/bin/java
    Don't know how there could be an error as line 1762...

    Here's the code:
    import java.io.*;
    import java.util.*;
    public class NameReversal 
    {
    	public static void main(String args[])
    	{
    		Scanner kbreader = new Scanner(System.in);
    		System.out.print("Please enter your name. ");
    		String name = kbreader.nextLine();
    		int howLong = name.length();
    		for(int g=howLong;g>=0;g--)
    		{
    			String reversed = name.substring(g-1,g);
    			System.out.print(reversed.toLowerCase());
    		}
    	}
    }

    Reeaaalllly confused here.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Apparent bug?

    The error message saying line 1762 is actually referring to the substring method in the String class. Your error is occurring on line 13.

    Your loop goes while g is greater than or equal to 0. Inside the loop you substring from g-1 to g. What happens when g is equal to zero? You try to substring from -1 to 0 which throws a StringIndexOutOfBoundsException.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Apparent bug?

    Unless you really have to reverse the string like that I would use a StringBuilder instead.

    final StringBuilder stringBuilder = new StringBuilder("String To Reverse");
    final String reversedLowerCasedString = stringBuilder.reverse().toString().toLowerCase();

    // Json