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.


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

Results 1 to 3 of 3

Thread: Problem with Looping...

  1. #1
    Junior Member
    Join Date
    Mar 2013
    My Mood
    Thanked 0 Times in 0 Posts

    Default Problem with Looping...

    I am trying to create a program that generates a string output from a string inputted by a user. The program must convert the string inputted by the user to string that is generated depending on the Key(any integer) which will be used as a basis of the to generate the secret code.

    For example: if the user inputs "java" and the Key is "3" the output must be "mdyz". The problem is that i created an array of the 26 letters of the alphabet and if the Key is greater than 26 i always get an ArrayIndexOutOfBoundsException.

    char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    How can i loop through that array and return to the first index after it reaches the last index of the array?
    Last edited by Norm; March 19th, 2013 at 08:12 AM. Reason: spaces removed in code tag

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 632 Times in 540 Posts

    Default Re: Problem with Looping...

    I would write out a bunch of examples until you see a pattern. What if the user enters 'z' and 5? What about 'y' and 3? Keep writing examples until a pattern emerges, then use that pattern to come up with your code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2013
    N. Ireland
    Thanked 6 Times in 6 Posts

    Default Re: Problem with Looping...

    ArrayIndexOutOfBoundsException - Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

    ArrayIndexOutOfBoundsException JAVA API Link

Similar Threads

  1. Problem with looping
    By gajen in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 27th, 2013, 06:56 PM
  2. Looping method problem.
    By Swen in forum What's Wrong With My Code?
    Replies: 24
    Last Post: December 29th, 2011, 08:49 AM
  3. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  4. Looping / OutOfBounds code problem
    By thisbeme in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 3rd, 2011, 07:10 AM
  5. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 02:24 PM

Tags for this Thread