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: Problem using the length of a string to bound the number of iterations of a for loop

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem using the length of a string to bound the number of iterations of a for loop

    Hi there, apologies if this is a very basic question, new to Java. I've been trying to take Roman numerical input and convert to Arabic, and in doing so have been attempting to run through all the "digits" in a roman numeral string, adding (or subtracting) their values. I've been using the following conditions for a for loop


    for (int x=0; x<=number.length(); x++)


    where number is inputted from the console. This made sense to me, but I'm getting the following error message


    Exception in thread "main" Mjava.lang.StringIndexOutOfBoundsException: String index out of range: [!!!]
    at java.lang.String.charAt(String.java:686)
    at Q3_6_a.main(Q3_6_a.java:106)

    where the underlined part is the length of the string inputted. Does anyone know why this doesn't work, and if there's any way to use a variable string length as an upper bound for the index in a loop? As far as I can see this problem is solely due to the limit, not to the action of the for loop.


    Thanks


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem using the length of a string to bound the number of iterations of a for l

    Despite this not having come to me after many hours, after posting it I immediately realised that I needed to have a -1 on the max. x value, please ignore this somewhat dim post!

Similar Threads

  1. [SOLVED] bound mismatcherror?
    By zytm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2011, 07:52 AM
  2. text.length method problem?
    By computercoder in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2010, 10:40 AM
  3. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 10:06 PM
  4. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 07:24 AM
  5. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM