2014-04-01 34 views
-1

我想讓這個程序詢問用戶一個字符串和字符,然後告訴用戶字符串的數量人物。找不到這個java異常:「main」java.lang.StringIndexOutOfBoundsException:String index超出範圍: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) 

謝謝你的幫助。

+0

這意味着(廢話!)的字符串索引超出範圍。做一些調試 - 你知道哪一行有錯誤,所以調試應該是微不足道的。 –

回答

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

} 

} 
+0

非常感謝! – user3486062

+0

如果這很有用,請將答案標記爲「上」和「已回答」。 – ErstwhileIII

+0

對不起,我是新手,我該怎麼做? – user3486062

1

你可能是指

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

,而不是

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

當你迭代的str1長度,並期望str2.length等於1

+0

謝謝!我很驚訝,我沒有注意到,是的,這就是我的意思。 – user3486062

+0

我將它改爲if(str1.charAt(i)== myChar),但我仍然得到異常。 – user3486062