【问题标题】:How do I print an error message stating the user input index is too large?如何打印指出用户输入索引太大的错误消息?
【发布时间】:2018-09-17 12:02:56
【问题描述】:

我正在尝试编写一个程序,当用户输入一个单词时,然后是一个索引,这会导致程序在给定的索引处显示字符,或者给出错误告诉用户给定的索引太大。每当我运行代码并放置一个太大的索引时,我都会从 java 中收到一条错误消息。任何帮助表示赞赏!

import java.util.Scanner;
public class scratch {
    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);
        System.out.printf("Enter a word:");
        String word = reader.next();

        Scanner letter = new Scanner (System.in);
        System.out.printf("Enter an index:");
        int index = letter.nextInt();

        char inputIndex = word.charAt(index);
        int length = word.length();
        if (index < length - 1 ) {
            System.out.printf("In word \"%s\", the letter at index"
                + " \"%2d\" is \'%c\'.\n"
                ,word, index, inputIndex );

        } else {
            System.out.printf("too big");   
        }
        reader.close();
        letter.close();
    }
}

错误信息: 线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:java.base/java.lang.StringLatin1.charAt(Unknown Source) at java.base/java.lang.String.charAt(Unknown Source) 中的 3 ) 在scratch.main(scratch.java:15)

【问题讨论】:

  • 你看过String.fomat()吗?
  • 你得到什么错误信息?
  • 考虑为所有读取使用相同的Scanner 实例,您不需要多个。
  • “我收到一条错误消息” - 错误消息包含许多有用的信息,可帮助您找出问题的原因。当您收到错误消息时,请在您的问题中包含完整、准确的错误消息 - 不要只说“我收到错误”。这样可以更轻松地为您提供帮助。
  • 永远不要将更多信息放入 cmets,而是始终更新您的问题。

标签: java string


【解决方案1】:

您应该在检查后致电charAt

if (index < length ) {
    char inputIndex = word.charAt(index); // move this line here
    System.out.printf("In word \"%s\", the letter at index"
            + " \"%2d\" is \'%c\'.\n"
            ,word, index, inputIndex );

} else {
    System.out.printf("too big");   
}

该异常是由于试图在一个太大的索引处获取一个字符而引起的。所以你应该在确保索引不太大之后再尝试获取字符,对吧?

【讨论】:

  • 另外,条件应该是index &lt; lengthindex &lt;= length - 1,并且没有理由创建两个Scanners,如果一个会更好(如果所有输入都不会失败)立即提供)。
  • 成功了!啊,有道理。感谢您的帮助
  • 是的,扫描仪确实如此,但这与问题无关。
  • 是的,很抱歉额外的扫描仪。我的同学加了它,我不确定它是否在那儿喊。谢谢。
【解决方案2】:

这是 try-catch 块的完美使用。尝试访问索引,如果有错误,捕获产生的异常并从那里打印:

char inputIndex;
try {
    inputIndex = word.charAt(index);
} catch(IndexOutOfBoundsException e) {
    System.out.println("Out of bounds!");
}

【讨论】:

  • 不,不是。您在 Java 中使用 try/catch 来处理异常的情况。失败的“字符串不包含我认为应该包含的内容”不属于该类别。您只需添加前面的检查,以避免一开始就抛出代码。
  • 一种特殊情况——您正试图访问一个不存在的索引。 java.lang 甚至为此提供了 a defined Exception,我相信你已经知道了。
  • 在 Java 中,您在使用索引之前对其进行检查。其他语言遵循不同的思维方式,但同样,在 Java 中,您不会使用 try/catch 来完成类似的事情。当出现债券索引时,您修复代码,并防止发生异常。这主要是关于在这里使用的最佳实践。还有一个 NullPointerException。这是否意味着你应该扔掉它们,或者抓住它们?!
【解决方案3】:

首先 - 使用一个 Scanner 对象。 if 条件也有修正。 index 应小于或等于 length-1

char inputIndex = word.charAt(index); 放入 if 语句中。

试试这个:

import java.util.*;



 public class Scratch { 

 public static void main(String[] args) {

    Scanner reader = new Scanner(System.in);
    System.out.printf("Enter a word:");
    String word = reader.next();

    //Scanner letter = new Scanner(System.in);
    System.out.printf("Enter an index:");
    int index = reader.nextInt();
    int length = word.length();
    if (index < length - 1) {
        char inputIndex = word.charAt(index);
        System.out.printf("In word \"%s\", the letter at index" + " \"%2d\" is \'%c\'.\n", word,
            index, inputIndex);

    } else {
        System.out.printf("too big");
    }
    reader.close();
    }
}

PS : 类名应该是 Scratch 而不是 Scratch(Java 约定)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多