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: [!!!]
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.
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!