字符串索引超出范围:4

时间:2013-12-31 23:18:37

标签: java

我是Java的初学者。所以我试着在这里运行这段代码,但它一直给我这个错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4.

我需要一些帮助。这是我的代码:

import java.util.Scanner;

public class TestFour
{
public static void main(String[]args)
{

  String inp= new String();
  Scanner scan = new Scanner(System.in);
  System.out.println("Enter Word ");
  inp = scan.nextLine();
  int output = 1;
  int [] board = new int[40];
  int points = 0; 
  int totalpoints = 0;
  int input;
  //start of for loop
  for(int i = 0; i < 5; i++)
  {
     input = scan.nextInt();
     for (int j = 0; j < inp.length(); j++)
     {
        //values of letters
        if(inp.charAt(i) == 'a' || inp.charAt(i) == 'e')
        {
           points = 1;
        }
        else if(inp.charAt(i) == 'd' || inp.charAt(i) == 'r') 
        {
           points = 2;
        }
        else if(inp.charAt(i) == 'b' || inp.charAt(i) == 'm')
        {
           points = 3;
        }
        else if(inp.charAt(i) == 'v' || inp.charAt(i) == 'y')
        {
           points = 4;
        }
        else if(inp.charAt(i) == 'j' || inp.charAt(i) == 'x')
        {
           points = 8;
        }
        else
        {
           points = points; 
        }
        //checking if double letter or triple letter and executing program
        if ( input % 3 == 0 && input % 6 != 0)
        {
           points = points * 2;
        }
        else
        {
           points = points;

        }   
        if (input % 5 == 0 && input != 15)
        {
           points = points * 3;
        }
        else 
        {
           points = points;
        }
        totalpoints = totalpoints + points;
        input = input + 1;
     }//end of for loop
     input = input - 4;
     //checking if double word or triple word and executing program
     for (int k = 0; k < inp.length(); k++)
     {   
        if (input % 7 == 0 && input != 21 && input != 25)
        {
           totalpoints = totalpoints * 2;
        }
        else
        {
           totalpoints = totalpoints;
        }

        if (input % 8 == 0 && input != 40)
        {
           totalpoints = totalpoints * 3;
        }
        else
        {
           totalpoints = totalpoints;
        }
        input = input + 1;
     }
  }
  System.out.println(totalpoints);
}
}

第五次进入输入时问题从for循环开始。感谢您的时间。即使我知道发生了什么,我也真的无法解决它。

1 个答案:

答案 0 :(得分:2)

您使用了错误的迭代计数器,将inp.charAt(i)替换为inp.charAt(j)