Java,从文件

时间:2017-10-15 17:42:15

标签: java io

我试图从文件中读取正负整数列表,并使用扫描仪将它们存储在数组中。问题是它没有读取负值。这是输入:

7 4 5 8 2 4 7 5 0 8 2 3 9 23 48 -12 49 

这是我现在的代码:

try {
        Scanner input=  new Scanner(new File(args[0]));
        key = input.nextInt();
        while(input.hasNextInt())
        {
            nums[count] = input.nextInt();
            count++;
        }
        input.close();

    } catch (FileNotFoundException e) {
        System.out.println("No file \""+args[0]+"\" found!");
    }

问题是它退出了第一个负值。

1 个答案:

答案 0 :(得分:2)

在您的文件中似乎隐藏了一个带有转义字符的额外--12。如果您将已经提供的行粘贴到呈现它们的编辑器中,则会得到:

  

7 4 5 8 2 4 7 5 0 8 2 3 9 23 48 --12 49

其中SELECT Item, SUM(CASE WHEN CAST(DateTimeCol AS Time) >= '08:00:00' AND CAST(DateTimeCol AS Time) < '10:00:00' THEN 1 ELSE 0 END) AS [8AM-10AM], SUM(CASE WHEN CAST(DateTimeCol AS Time) >= '10:00:00' AND CAST(DateTimeCol AS Time) < '12:00:00' THEN 1 ELSE 0 END) AS [8AM-10AM], SUM(CASE WHEN CAST(DateTimeCol AS Time) >= '12:00:00' AND CAST(DateTimeCol AS Time) < '14:00:00' THEN 1 ELSE 0 END) AS [10AM-12AM], SUM(CASE WHEN CAST(DateTimeCol AS Time) >= '14:00:00' AND CAST(DateTimeCol AS Time) < '16:00:00' THEN 1 ELSE 0 END) AS [12PM-2PM], SUM(CASE WHEN CAST(DateTimeCol AS Time) >= '16:00:00' THEN 1 ELSE 0 END) AS [>4PM] FROM table1 GROUP BY Item; 显然不是要读取的正确整数。