Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从文本扫描数组中的特定数字?_Java_Arrays - Fatal编程技术网

Java 如何从文本扫描数组中的特定数字?

Java 如何从文本扫描数组中的特定数字?,java,arrays,Java,Arrays,我有一个文本输入,我正试图扫描每三个数字。我只能扫描每一个数字,但不能特别扫描每三个数字 Scanner inFile = new Scanner(new FileReader("budgettracker.txt")); int counter = 0; PrintWriter outFile = new PrintWriter ("textout.text"); while(inFile.hasNextInt()) { counter+

我有一个文本输入,我正试图扫描每三个数字。我只能扫描每一个数字,但不能特别扫描每三个数字

    Scanner inFile = new Scanner(new FileReader("budgettracker.txt"));
    int counter = 0;
    PrintWriter outFile = new PrintWriter ("textout.text");
    while(inFile.hasNextInt())
    {
        counter++;
        inFile.nextInt();
    }
    Scanner inFile1 = new Scanner(new FileReader("budgettracker.txt"));
    int a[] = new int[counter];
        for (int p = 0; p<counter; p++)
        {
            if(a[p] == a[0] || a[p] == a[3] || a[p] == a[6])
            {
            a[p] = inFile1.nextInt();
            outFile.print(a[p] + " ");
            }
        }

        outFile.close();
        inFile.close();
    }
Scanner infle=新的扫描仪(新的文件阅读器(“budgettracker.txt”);
int计数器=0;
PrintWriter outFile=新的PrintWriter(“textout.text”);
while(infle.hasNextInt())
{
计数器++;
infle.nextInt();
}
Scanner inFile1=新扫描仪(新文件阅读器(“budgettracker.txt”);
int a[]=新的int[计数器];

对于(int p=0;p我不完全确定你的意思。这就是你要找的吗

int index = 0;
for (int p = 0; p<counter; p++)
{
  if(p%3 == 0) // Every third number
  {
    a[index] = inFile1.nextInt();
    outFile.print(a[index++] + " ");
  }else{
    inFile1.nextInt(); // Skip if the line isn't a multiple of 3
  }
}
int索引=0;

对于(int p=0;p你的意思是,你想扫描文本文件的第三行吗?第三个数字实际上我很抱歉我应该添加文本输入500 400 100 300 200 200 900 200 150我只想扫描500、300和900我尝试了你的答案,但它只打印出第一个数字sorry!我对数组使用了错误的索引。我现在修复了它。我不知道哦,你的意思是什么。我刚刚运行了这个精确的示例(使用你的其余代码),它将
500300900
打印到文件“textout.text”