簡體   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