Can';我不明白这个java异常是什么;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1
我希望这个程序询问用户一个字符串和字符,然后告诉用户字符中的字符串数 代码如下:Can';我不明白这个java异常是什么;“主要”;java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1,java,string,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
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);
}
}
谢谢你的帮助。你的意思可能是
if (str1.charAt(i) == myChar)
而不是
if (str2.charAt(i) == myChar)
当您在
str1
的长度上进行迭代,并期望str2.length
等于1
时,您可以使用一些字符串方法尝试更简洁的方法:
package com.example.count;
导入java.util.Scanner;//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");
}
}
谢谢!我很惊讶我没听清楚,是的,这就是我的意思。我把它改为if(str1.charAt(I)=myChar),但我仍然得到了异常。这意味着(duh!)字符串索引超出了范围。做一点调试——你知道哪一行有错误,所以调试应该很简单。如果这有用,请标记答案“up”并“已回答”。对不起,我是新手,该怎么做?单击答案上的向上箭头符号并“检查”答案谢谢。我已检查过。
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");
}
}