是一个初学者,谁能帮助我弄清楚我们的情况。 我正在尝试读取字符串,并将字符串的每个字符存储在数组中。 输出值 输入一个字符串以反向提取并垂直显示: 哈克 用户输入的字符串是:HACK 使用用户输入字符串....“ HACK”调用类“ digitExtractor ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
如果输入以“Today”开头并以“MLIA”结尾(大小写无关紧要),我正在尝试创建一个打印“VALID ENTRY”的代码。 如果没有,则打印“格式不正确,尝试其他提交”。 出于某种原因,程序不断给我一个越界错误,我不知道为什么。
当我将 substring 代码更改为sub.substring(0,1)
进行测试时,它仍然给了我一个错误,所以这不是问题。 我还尝试为每个字母添加 char 值以确定单词是什么,但这也不起作用。
public class submit{
public static void main(String[] args) throws IOException{
Scanner scanner = new Scanner(new File("submit.txt"));
int trials = scanner.nextInt();
int total = 0;
for(int x = 0; x <= trials; x++){
String sub = scanner.nextLine();
sub = sub.toLowerCase();
if(sub.substring(0,5) == "today") //I only have it set up to find "today"
System.out.println("VALID ENTRY");
else
System.out.println("INCORRECT FORMATTING, TRY ANOTHER SUBMISSION");
}
}//end of main
}//end of class
输入:
5
ToDAY, I went to school. mlia
Hehehe today mlia this shouldn't work
Today, I went to a programming contest. Hehe. MLIA
TODAYMLIA
T0day is a brand new day! MLIA
预期的 output 应该是:
VALID ENTRY
INCORRECT FORMATTING, TRY ANOTHER SUBMISSION
VALID ENTRY
VALID ENTRY
INCORRECT FORMATTING, TRY ANOTHER SUBMISSION
实际 output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.substring(String.java:1963)
at submit.main(submit.java:15)
代码有两个问题。 首先,您应该使用equals
来比较String
,而不是==
。 否则,即使对于相同的String
,它也可能返回 false 。
其次, nextLine
将从Scanner
读取直到下一个换行符之后。 但是nextInt
只会读取到该换行符之前,因此您对nextLine
的第一次调用只需将Scanner
前进一个字符并返回一个空String
。 您需要在nextInt
之后调用一个额外的nextLine
以前进到 integer 之后的下一行。 有关详细信息,请参阅此问题。
因此,您应该对代码进行以下更改:
Scanner scanner = new Scanner(new File("submit.txt"));
int trials = scanner.nextInt();
scanner.nextLine(); //Add this line, to consume the newline character after "5"
// alternatively, you could replace both of the above lines with this:
// int trials = Integer.parseInt(scanner.nextLine());
int total = 0;
for(int x = 0; x <= trials; x++){
String sub = scanner.nextLine();
sub = sub.toLowerCase();
if(sub.substring(0,5).equals("today")) //compare strings using equals, not ==
System.out.println("VALID ENTRY");
else
System.out.println("INCORRECT FORMATTING, TRY ANOTHER SUBMISSION");
}
您可以改用以下代码。
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Submit {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(new File("submit.txt"));
int trials = scanner.nextInt();
scanner.nextLine();
int total = 0;
for (int x = 0; x < trials; x++) {
String sub = scanner.nextLine();
sub = sub.toLowerCase();
System.out.print(sub + " -> ");
if (sub.startsWith("today")) // I only have it set up to find "today"
System.out.println("VALID ENTRY");
else
System.out.println("INCORRECT FORMATTING, TRY ANOTHER SUBMISSION");
}
scanner.close();
}// end of main
}// end of class
您可以使用 startsWith 并且 nextLine 需要在 nextInt 之后调用,因为 nextInt 不读取换行符并且在读取 int 5 后将光标保持在同一行。 所以你得到了你所面临的错误。
当您放置一个额外的nextLine()
时,它实际上会移动到下一行。
之后你只有5
行要阅读,你不需要<=
否则它会再次抛出错误。
所以 make 是<
only
如果您只需要检查开头并且还想检查结尾,那么startsWith()
就很好,那么还有一个方法endsWith()
如果要使用相等,字符串也需要equals
才能与字符串文字匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.