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

Thread: Small Problem with my Code

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

    Default Small Problem with my Code

    Hello, I have this code:

    import java.util.Scanner;

    public class Question1 {

    // public static char isConsecutive() {


    //}

    public static void main (String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.print("Enter something: ");
    String word=reader.next();

    // creating an array
    String[] ArrayKespass= word.split("");

    System.out.println();

    //place our letters in array, and print them
    for (int x=0; x<ArrayKespass.length; x++) {
    System.out.print(ArrayKespass[x] + " ");
    }

    // skip a line
    System.out.println();

    // convert our letters into characters
    for (int i=0; i<=ArrayKespass.length; i++) {
    char c=word.charAt(i);



    }
    }

    }



    It works, but when i execute it it gives me this in red:

    java.lang.StringIndexOutOfBoundsException: String index out of range: 13
    at java.lang.String.charAt(Unknown Source)
    at Question1.main(Question1.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)

    Can someone please explain to me why? And what should I do to solve the problem?


  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: Small Problem with my Code

    Please read the Announcement topic at the top of the sub-forum to learn how to post your code in code tags.

    Your error is caused by looping: i<=ArrayKespass.length, and you knew that. You did it right in the other loop but wrong here.

Similar Threads

  1. Small problem in big code, not sure why
    By diesal11 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 16th, 2011, 10:25 AM
  2. Small problem, help please?
    By Jonathansafc in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 3rd, 2011, 06:46 PM
  3. WatchService - small code for monitoring folders
    By zincc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 8th, 2011, 10:19 AM
  4. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 08:51 PM
  5. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 11:26 AM