繁体   English   中英

无法弄清楚这个 java Exception: "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1

[英]Can't figure out this java Exception: "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1

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

这是代码:

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)

感谢您的帮助。

你可能是说

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

代替

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

当您迭代str1的长度时,并期望str2.length等于1

您可以尝试使用一些更简洁的方法,使用一些 String 方法:

package com.example.count;

导入 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");

}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM