无法弄清楚这个java异常:“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1

时间:2014-04-01 16:53:50

标签: java string

我希望这个程序向用户询问字符串和字符,然后告诉用户字符中的字符串数。

这是代码:

import java.util.Scanner;  // Needed for the Scanner Class

/* 

This program ask you to enter a string and a character and then tells you how many times the character appears in the string. 

*/

public class PC5

{ public static void main(String[] args) throw StringIndexOutOfBoundsException


   {
      int times = 0;  // To keep track of the times the charcater appears in the String 
      String str1;    // To get the string you want to check
     String str2;   // To get the string for the character you want to check in the String.
      char myChar;  // To get the character from str2 


      Scanner keyboard = new Scanner(System.in);

      // Get the string you want to check 
      System.out.print("Enter the string you want to check: ");
      str1 = keyboard.nextLine();


      // To get the charcater
      System.out.print("Enter the character you want to check: ");
      str2 = keyboard.nextLine();
      myChar = str2.charAt(0); 

     for (int i = 0; i < str1.length(); i++)

         {
            if (str2.charAt(i) == myChar)
                    times += 1;


         }


         System.out.println("The number of times the character appears in the string is: " + times);


  }


  }

运行此程序时出现此异常:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(String.java:695)
    at PC5.main(PC5.java:36)

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

你可能意味着

if (str1.charAt(i) == myChar)

而不是

if (str2.charAt(i) == myChar)

当您在str1的长度上进行迭代时,期望str2.length等于1

答案 1 :(得分:0)

你可以尝试一些更简洁的东西,利用一些String方法:

package com.example.count;

import java.util.Scanner; //需要扫描仪类

/ *

此程序要求您输入字符串和字符,然后告诉您字符在字符串中出现的次数。

* /

公共类PC5 {

public static void main(String[] args)

{
    int times = 0; // To keep track of the times the charcater appears in
                    // the String
    String str1; // To get the string you want to check
    String str2; // To get the string for the character you want to check in
                    // the String.

    Scanner keyboard = new Scanner(System.in);

    // Get the string you want to check
    boolean stopping = false;
    while (!stopping) {
        System.out
                .print("Enter the string you want to check: (enter 'end' to stop)");
        str1 = keyboard.nextLine();
        stopping = "end".equalsIgnoreCase(str1.trim());
        if (!stopping) {

            // To get the character
            System.out.print("Enter the character you want to check: ");
            str2 = keyboard.nextLine();
            if (str2.length() > 1) {
                str2 = str2.substring(0, 1);
            }

            times = 0;
            int loc = -1;
            while ((loc = str1.indexOf(str2, loc + 1)) > -1) {
                times++;
            }

            System.out.println("Results: '" + str2 + "' appears " + times
                    + " times in string " + str1);
        }

    }
    keyboard.close();
    System.out.println("Thanks");

}

}