简体   繁体   English

我不断收到此错误:线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 28

[英]I keep getting this error : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 28

This is my code :这是我的代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String tweet = scan.nextLine();

        int link = 0;
        int tag = 0;
        int at = 0;
        int c = 0;
        int l = tweet.length();

        if (l <= 140) {
            while (c < tweet.length()) {
                char let = tweet.charAt(c);
                if (let == '#') {
                    tag++;
                    String hash = tweet.substring(c, c + 2);
                    if (hash.equals("# ")) {
                        tag--;
                    }

                } else if (let == '@') {
                    at++;
                    String to = tweet.substring(c, c + 2);
                    if (to.equals("@ ")) {
                        at--;
                    }
                } else if (let == 'h') {
                    String http = tweet.substring(c, c + 7);
                    if (http.equals("http://")) {
                        link++;
                    }
                }
                c++;
            }
            System.out.println("Number of Hashtags: " + tag);
            System.out.println("Number of Attributions: " + at);
            System.out.println("Number of Links: " + link);
        } else if (l > 140) {
            int difference = tweet.length() - 140;
            System.out.print("Excess Characters: " + difference);
        }

    }
}

Every time i run it I get the error:每次我运行它我都会收到错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 28
    at java.lang.String.substring(String.java:1951)
    at Main.main(Main.java:268)
    at Ideone.assertRegex(Main.java:110)

I was just wondering if there is a way to fix it?我只是想知道是否有办法解决它?

Add a range check before your do subString because tweet.substring(c, c + 2);在 do subString 之前添加范围检查,因为 tweet.substring(c, c + 2); might cause index out of bound error when, for example, the tweet length is 7, c is currently 6, c+2 is 8, which exceeds the length.当推文长度为7,c当前为6,c+2为8,超过长度时,可能会导致索引越界错误。

if (let == '#') {
                tag++;
                if (c < tweet.length() - 2) { // make sure c+2 is in range for the substring function
                    String hash = tweet.substring(c, c + 2);
                    if (hash.equals("# ")) {
                        tag--;
                    }
                }

            } else if (let == '@') {
                at++;
                if (c < tweet.length() - 2) {// make sure c+2 is in range for the substring function
                    String to = tweet.substring(c, c + 2);
                    if (to.equals("@ ")) {
                        at--;
                    }
                }
            } else if (let == 'h') {
                if (c < tweet.length() - 7) {// make sure c+7 is in range for the substring function
                    String http = tweet.substring(c, c + 7);
                    if (http.equals("http://")) {
                        link++;
                    }
                }
            }

暂无
暂无

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

相关问题 为什么我不断收到:线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:4 - Why do I keep geting: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 构建成功,但我不断收到“线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 1&#39; - Build is successful but I kept getting the 'Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1' 我收到如下错误消息:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - I get an error message as follows: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 编译Java项目后出错:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Error after compiling Java project : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 该程序不断给我一个错误:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - This program keeps on giving me an error: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 使用递归错误。 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - using recursion error. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 线程“main”中的错误异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Error Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1 如何修复线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: error - How to fix Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: error anagram的运行时错误-线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Runtime Error for an anagram - Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1 为什么程序会抛出这个错误??:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:36 - Why is the program throwing this error??: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 36
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM