【问题标题】:Checking if two characters are the same using == (Java)使用 == (Java) 检查两个字符是否相同
【发布时间】:2015-05-23 07:16:54
【问题描述】:

我正在尝试在 while 循环中检查两个字符是否相等,但运行时出现此错误:

线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:5 在 java.lang.String.charAt(String.java:686) 在 Practice.main(Practice.java:27)

我的代码:

import java.util.Scanner;

public class Practice {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("String: ");
        String firstIndex = input.next();
        System.out.print("String Two: ");
        String secondIndex = input.next();

        int seqStart = -1;
        int secCheck = 0;
        int start;

    if (firstIndex.length() >= secondIndex.length()) {
        for (int firstCheck = 0; firstCheck <= firstIndex.length(); firstCheck++) {
            if (firstIndex.charAt(firstCheck) != secondIndex.charAt(0)) {
                continue;
            }else if (firstIndex.charAt(firstCheck) == secondIndex.charAt(0)) {
                start = firstCheck;
                while (firstIndex.charAt(firstCheck) == secondIndex.charAt(secCheck)) {
                    for (int check = 0; secCheck < secondIndex.length(); check++) {
                        firstCheck++;
                        secCheck++;
                        if (check == secondIndex.length()) {
                            seqStart = start;
                            secCheck = (secondIndex.length() + 10);
                        }
                    }
                }
            }
        }
    }
System.out.println(seqStart);     
    }
}

程序应该检查一个字符串是否包含在另一个字符串中,如果是,它会返回第二个字符串在第一个字符串中开始的位置。如果不是,则返回 -1。

任何帮助将不胜感激!

【问题讨论】:

  • firstCheck &lt;= firstIndex.length() 应该是 firstCheck &lt; firstIndex.length() - Java (大部分)是零索引
  • 做到了,但我仍然在第 27 行收到错误消息:while (firstIndex.charAt(firstCheck) == secondIndex.charAt(secCheck)) {
  • while (firstIndex.charAt(firstCheck) == secondIndex.charAt(secCheck)) - 如果没有匹配的chars 会发生什么?
  • 它工作正常,返回-1
  • 但是for-loop 完成后,firstChecksecCheck 设置为什么?您需要检查这些变量的状态...while (firstCheck &lt; firstIndex.length() &amp;&amp; secCheck &lt; secondIndex.length() &amp;&amp; firstIndex.charAt(firstCheck) == secondIndex.charAt(secCheck)) {

标签: java string char charat


【解决方案1】:

你的 for 循环是这样说的:

for (int firstCheck = 0; firstCheck <= firstIndex.length(); firstCheck++)

问题在于中间的语句firstCheck &lt;= firstIndex.length()。循环将以firstCheck 等于firstIndex.length() 运行。然后,当你使用firstIndex.charAt(firstCheck) 时,它会超出范围,因为字符串是零索引的,所以在等于字符串长度的位置没有字符。你可以这样解决:

for (int firstCheck = 0; firstCheck < firstIndex.length(); firstCheck++)

【讨论】:

  • 我这样做了,但在第 27 行仍然出现错误:while (firstIndex.charAt(firstCheck) == secondIndex.charAt(secCheck)) {
  • Alex 的 MacBook Pro:Desktop Alex$ javac Practice.java Alex 的 MacBook Pro:Desktop Alex$ java 练习字符串:hello 字符串二:ell 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引输出范围:3 at java.lang.String.charAt(String.java:686) at Practice.main(Practice.java:27) Alex 的 MacBook Pro:Desktop Alex$
  • @AlexSmadja secCheck 越界了。我不确定你想在那里做什么,但是没有重置它的嵌套循环使它超出了界限。
  • 是什么让它越界了?我刚开始编程,所以我还不是很擅长。我要做的是让它检查字母是否连续匹配,直到检查达到第二个字符串的长度
猜你喜欢
  • 2021-06-21
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多