简体   繁体   English

Java扫描仪无法正常工作

[英]Java Scanner not working as expected

I am trying to read a file into my java program where the first row in the txt file is an int and everything after is a long. 我正在尝试将文件读取到我的Java程序中,其中txt文件的第一行是int,之后的所有内容都很长。 The issue that I am having is every single line of code in the while loop is calling s.nextint() and s.nextLong() (at least when I put a watch on them in Eclipse). 我遇到的问题是while循环中的每一行代码都在调用s.nextint()s.nextLong() (至少当我在Eclipse中监视它们时)。 I want them only to increment through the text file where I call them. 我只希望它们在我称之为它们的文本文件中递增。

Firstly, what am I doing wrong because it was my understanding they should only increment when called and not on every line of code, and is there a better way to do this? 首先,我在做错什么,因为据我了解,它们仅应在调用时增加,而不应在每一行代码中递增,并且有更好的方法吗? I was thinking if need be I could just load them all in as a single type to an array and cast later, but this wouldn't be what I consider reasonable. 我在想是否可以将它们全部作为一个单一类型加载到数组中,然后再进行转换,但是我认为这不是合理的。 I feel this should be fairly simple but I am overlooking something. 我觉得这应该很简单,但是我忽略了一些东西。

Also lets say there are 10 numbers in the text file and go 1-10. 还可以说文本文件中有10个数字,然后是1-10。 I understand that it is a waste to save a small number as an int but just go with it. 我知道将少量保存为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;
}

I'd prefer something like this: 我更喜欢这样的东西:

do 
{   
  if(row == 1){
    nums = new long[s.nextInt()];
    row++;
  }
  else 
  {
    nums[row] = s.nextLong();
    row++;
  }
}while(s.hasNextLong());

I didn't try to compile or debug it; 我没有尝试对其进行编译或调试。 holler if you need further help. 您需要进一步的帮助。 (It assumes there will be an integer atop the file, as you said would be the case. You should probably add code to guard against that not happening.) (它假定文件顶上将有一个整数,就像您所说的那样。您可能应该添加代码以防止这种情况发生。)

You're code is throwing an ArrayIndexOutOfBoundsException . 您的代码正在抛出ArrayIndexOutOfBoundsException You're trying to use row for two purposes which throws off your array indexing. 您正在尝试将row用于两个目的,这会引发数组索引编制工作。 By the time we get to the else block, row is equal to 2 and you're trying to apply row to an array from 0 - 9 (10 longs for example). 当我们到达else块时,row等于2,并且您尝试将row应用于0-9(例如10个long)的数组。

You should trying initializing your array after checking hasNextInt() then get your long numbers after checking hasNextLong() 您应该在检查hasNextInt()之后尝试初始化数组,然后在检查hasNextLong()之后获取long整数

Code Sample (text.txt has 10 numbers [1234567890]): 代码示例(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;
}

Results: 结果:

1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890
1234567890

I've generally found that Scanners behave oddly when attempting to read multiple types. 我通常发现,尝试读取多种类型时,扫描仪的行为异常。 I'd create a separate Scanner for each type that you'll be reading. 我将为您要阅读的每种类型创建一个单独的扫描仪。

It seems that the issue i stated was having something to do with an eclipse error. 看来我所说的问题与月食错误有关。 If i just run the code or set a breakpoint after the readfile in the main my code works as expected, but when I stepped through values weren't being assigned right by eclipse i'm guessing. 如果我只是运行代码或在主文件中的readfile之后设置断点,我的代码将按预期工作,但是当我逐步执行值时,Eclipse并没有正确分配值。 Interesting issue. 有趣的问题。 I would like to see exactly what was causing it. 我想确切地了解是什么原因造成的。 Seems very unlikely but isn't that coding? 似乎不太可能,但那不是编码吗? nothing goes as expected. 没有任何进展。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM