数组索引越界导致错误?
Array index out of bounds and causing error?
我知道这很容易修复,但它让我很痛苦。我试图查看其他问题,但找不到任何有帮助的问题。这是我在这里 post 的最后一个选项,因为我 运行 没有时间完成这个程序。该程序从文件中读取数字并打印出每个数字的单词值,即。 30:三零,150:一五零
错误显示代码行越界
System.out.print(alsoWords[((int) digit - 0)] + " ");
package main;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
// can use String array instead of Map as suggested in comments
private static final String[] alsoWords = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
public static void main(String args[]) //throws FileNotFoundException
{
Scanner fin = null;
// Scanner scanner = new Scanner(new File("translates.txt"));
//
// while (scanner.hasNextInt())
{
try {
fin = new Scanner(new File("C:\Users\Brian2\Documents\NetBeansProjects\main\src\main\translate.txt"));
} catch (FileNotFoundException e) {
System.err.println("Error opening the file translates.txt");
System.exit(1);
}
while (fin.hasNext()) {
int i = 0;
i ++;
char[] chars = ("" + fin.nextInt()).toCharArray();
System.out.print(String.valueOf(chars) + ": ");
// for each digit in a given number
for (char digit : chars) {
System.out.println(i);
System.out.print(alsoWords[((int) digit - 0)] + " ");
}
System.out.println();
}
}
fin.close();
}
}
在调试器中单步执行您的代码。检查每个变量的值。
for (char digit : chars)
digit
是一个 Unicode 字符。
(int) digit
您正在获取 digit
的 Unicode 点。对于 ASCII 个字符,这与 ASCII 值相同。例如,NUL
的 ASCII 值为零。字符 0
的 ASCII 值为 48
。假设第一个字符是零。您将获得:
48 - 0
也就是 48。
alsoWords[48]
超出范围。你想要:
alsowords[(int)digit - (int)'0']
如何处理 '0'
之前的字符留作 reader 的练习。
我知道这很容易修复,但它让我很痛苦。我试图查看其他问题,但找不到任何有帮助的问题。这是我在这里 post 的最后一个选项,因为我 运行 没有时间完成这个程序。该程序从文件中读取数字并打印出每个数字的单词值,即。 30:三零,150:一五零
错误显示代码行越界
System.out.print(alsoWords[((int) digit - 0)] + " ");
package main;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
// can use String array instead of Map as suggested in comments
private static final String[] alsoWords = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
public static void main(String args[]) //throws FileNotFoundException
{
Scanner fin = null;
// Scanner scanner = new Scanner(new File("translates.txt"));
//
// while (scanner.hasNextInt())
{
try {
fin = new Scanner(new File("C:\Users\Brian2\Documents\NetBeansProjects\main\src\main\translate.txt"));
} catch (FileNotFoundException e) {
System.err.println("Error opening the file translates.txt");
System.exit(1);
}
while (fin.hasNext()) {
int i = 0;
i ++;
char[] chars = ("" + fin.nextInt()).toCharArray();
System.out.print(String.valueOf(chars) + ": ");
// for each digit in a given number
for (char digit : chars) {
System.out.println(i);
System.out.print(alsoWords[((int) digit - 0)] + " ");
}
System.out.println();
}
}
fin.close();
}
}
在调试器中单步执行您的代码。检查每个变量的值。
for (char digit : chars)
digit
是一个 Unicode 字符。
(int) digit
您正在获取 digit
的 Unicode 点。对于 ASCII 个字符,这与 ASCII 值相同。例如,NUL
的 ASCII 值为零。字符 0
的 ASCII 值为 48
。假设第一个字符是零。您将获得:
48 - 0
也就是 48。
alsoWords[48]
超出范围。你想要:
alsowords[(int)digit - (int)'0']
如何处理 '0'
之前的字符留作 reader 的练习。