Java 扫描仪未按预期工作
Java Scanner not working as expected
我正在尝试将文件读入我的 java 程序,其中 txt 文件的第一行是一个 int,后面的所有内容都是 long。我遇到的问题是 while 循环中的每一行代码都在调用 s.nextint()
和 s.nextLong()
(至少当我在 Eclipse 中监视它们时)。我希望它们只在我调用它们的文本文件中递增。
首先,我做错了什么,因为据我所知,它们应该只在调用时递增,而不是在每一行代码上递增,有没有更好的方法来做到这一点?我在想,如果需要的话,我可以将它们作为单一类型全部加载到一个数组中,然后再进行转换,但这不是我认为合理的。我觉得这应该很简单,但我忽略了一些东西。
还假设文本文件中有 10 个数字,然后从 1-10 开始。我知道将一个小数字保存为 int 是一种浪费,但还是随它去吧。
public static long[] readfile()
{
int row = 1;
Scanner s = null;
long[] nums = null;
try {
s = new Scanner(new BufferedReader( new FileReader("text.txt")));
while (s.hasNext()) {
if(row == 1){
nums = new long[s.nextInt()];
row++;
}
else {
nums[row - 2] = s.nextLong();
row++;
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
if (s != null) {
s.close();
}
}
return nums;
}
我更喜欢这样的东西:
do
{
if(row == 1){
nums = new long[s.nextInt()];
row++;
}
else
{
nums[row] = s.nextLong();
row++;
}
}while(s.hasNextLong());
我没有尝试编译或调试它;如果您需要进一步的帮助,请大声喊叫。 (它假设文件顶部会有一个整数,正如您所说的那样。您可能应该添加代码以防止这种情况发生。)
您的代码正在抛出 ArrayIndexOutOfBoundsException
。您正试图将 row
用于两个目的,这会破坏您的数组索引。当我们到达 else
块时,行等于 2,并且您正在尝试将行应用于 0 - 9 的数组(例如 10 个长整数)。
您应该在检查 hasNextInt()
之后尝试初始化您的数组,然后在检查 hasNextLong()
之后获取您的 long
数字
代码示例(text.txt 有 10 个数字 [1234567890]):
public static void main(String[] args) throws Exception {
long[] longs = readfile();
if (longs != null) {
for (long l : longs) {
System.out.println(l);
}
}
}
public static long[] readfile() {
int row = 0;
Scanner s = null;
long[] nums = null;
try {
s = new Scanner(new BufferedReader(new FileReader("text.txt")));
if (s.hasNextInt()) {
nums = new long[s.nextInt()];
}
while (s.hasNextLong()) {
nums[row] = s.nextLong();
row++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return nums;
}
结果:
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
我通常发现扫描仪在尝试读取多种类型时表现得很奇怪。我会为您将要阅读的每种类型创建一个单独的扫描仪。
看来我所说的问题与日食错误有关。如果我只是 运行 代码或在主代码中的读取文件之后设置断点,我的代码会按预期工作,但是当我逐步执行值时,我猜 eclipse 没有正确分配值。有趣的问题。我想看看到底是什么原因造成的。似乎不太可能,但这不是编码吗?一切都不如预期。
我正在尝试将文件读入我的 java 程序,其中 txt 文件的第一行是一个 int,后面的所有内容都是 long。我遇到的问题是 while 循环中的每一行代码都在调用 s.nextint()
和 s.nextLong()
(至少当我在 Eclipse 中监视它们时)。我希望它们只在我调用它们的文本文件中递增。
首先,我做错了什么,因为据我所知,它们应该只在调用时递增,而不是在每一行代码上递增,有没有更好的方法来做到这一点?我在想,如果需要的话,我可以将它们作为单一类型全部加载到一个数组中,然后再进行转换,但这不是我认为合理的。我觉得这应该很简单,但我忽略了一些东西。
还假设文本文件中有 10 个数字,然后从 1-10 开始。我知道将一个小数字保存为 int 是一种浪费,但还是随它去吧。
public static long[] readfile()
{
int row = 1;
Scanner s = null;
long[] nums = null;
try {
s = new Scanner(new BufferedReader( new FileReader("text.txt")));
while (s.hasNext()) {
if(row == 1){
nums = new long[s.nextInt()];
row++;
}
else {
nums[row - 2] = s.nextLong();
row++;
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
if (s != null) {
s.close();
}
}
return nums;
}
我更喜欢这样的东西:
do
{
if(row == 1){
nums = new long[s.nextInt()];
row++;
}
else
{
nums[row] = s.nextLong();
row++;
}
}while(s.hasNextLong());
我没有尝试编译或调试它;如果您需要进一步的帮助,请大声喊叫。 (它假设文件顶部会有一个整数,正如您所说的那样。您可能应该添加代码以防止这种情况发生。)
您的代码正在抛出 ArrayIndexOutOfBoundsException
。您正试图将 row
用于两个目的,这会破坏您的数组索引。当我们到达 else
块时,行等于 2,并且您正在尝试将行应用于 0 - 9 的数组(例如 10 个长整数)。
您应该在检查 hasNextInt()
之后尝试初始化您的数组,然后在检查 hasNextLong()
long
数字
代码示例(text.txt 有 10 个数字 [1234567890]):
public static void main(String[] args) throws Exception {
long[] longs = readfile();
if (longs != null) {
for (long l : longs) {
System.out.println(l);
}
}
}
public static long[] readfile() {
int row = 0;
Scanner s = null;
long[] nums = null;
try {
s = new Scanner(new BufferedReader(new FileReader("text.txt")));
if (s.hasNextInt()) {
nums = new long[s.nextInt()];
}
while (s.hasNextLong()) {
nums[row] = s.nextLong();
row++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return nums;
}
结果:
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
我通常发现扫描仪在尝试读取多种类型时表现得很奇怪。我会为您将要阅读的每种类型创建一个单独的扫描仪。
看来我所说的问题与日食错误有关。如果我只是 运行 代码或在主代码中的读取文件之后设置断点,我的代码会按预期工作,但是当我逐步执行值时,我猜 eclipse 没有正确分配值。有趣的问题。我想看看到底是什么原因造成的。似乎不太可能,但这不是编码吗?一切都不如预期。