title: Why do I get a "String index out of range" error when I run this program?
body: <p>I am creating a program that converts roman numeral input to it's integer value and every time I run the program I get an error that says, </p>
  
  <pre><code>"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
      at java.lang.String.charAt(String.java:646)
      at romannumeralconverter.RomanNumeralConverter.convert(RomanNumeralConverter.java:20)
      at romannumeralconverter.RomanNumeralConverter.romanInput(RomanNumeralConverter.java:68)
      at romannumeralconverter.RomanNumeralConverter.printValue(RomanNumeralConverter.java:72)
      at romannumeralconverter.RomanNumeralConverter.main(RomanNumeralConverter.java:77)
  Java Result: 1"
  </code></pre>
  
  <p>Now I am new to programming so I don't know what this means exactly. I am guessing my conversion algorithm is wrong in which the roman numeral entered is not read by the loop. Here is what I have:</p>
  
  <pre><code>public class RomanNumeralConverter {  
  
  public String getUserInput() {
  
      Scanner numberInput = new Scanner (System.in);
      System.out.print("Enter a roman numeral in uppercase: ");
      String userInput = numberInput.next();
      numberInput.close();
      return userInput;
  }
  
  public int convert (String userInput) {
      int result = 0;
      int subtractamount = 0;
      int x = userInput.length();
      while(x != 0) {
      char romanConvert = userInput.charAt(x);
          if(x &gt;= 1) {
          if(convertChar(romanConvert) &gt;= convertChar(userInput.charAt(x - 1))) {
          subtractamount += convertChar(userInput.charAt(x - 1));
      }
  }
      result += convertChar(romanConvert);
      x--;
  
  }
  result -= subtractamount;
  return result;
  
  }
  
  public static char convertChar(char value) {
  char result;
  switch (value) {
      case 'I':
          result = 1;
          break;
      case 'V':
          result = 5;
          break;
      case 'X':
          result = 10;
          break;
      case 'L':
          result = 50;
          break;
      case 'C':
          result = 100;
          break;
      case 'D':
          result = 500;
          break;
      case 'M':
          result = 1000;
          break;
      default:
          System.out.println("Invalid character!");
          result = 0;
          break;
                 }
  return result;
  }
  
  public int romanInput() {       
      return convert(getUserInput());
  }
  
  public void printValue() {
  System.out.println(romanInput());
  }
  
  public static void main (String[] args) {
  
  new RomanNumeralConverter().printValue();
  }
  }
  </code></pre>
  
  <p>If my algorithm is wrong, does anyone know how to fix it?</p>