Java Array Not Printing(正在使用 BlueJ)



我目前正在尝试完成一项需要使用Java编程的大学任务。任务如下。

我必须对"钓鱼比赛"的输入文件进行排序。该文件首先为每行提供一个整数(介于 1-7 之间),然后给出一个实数。棘手的部分是数字尚未相加或按顺序加起来。这意味着我必须让程序读取整数,证明其所需位置,然后将以下权重添加到该位置。

以下是文件外观的想法

3 36.2

6 27.8

7 10.3

我对如何完成整个任务有一个大致的想法,但我被困在一个部分,即打印每个竞争对手的总重量。

这是我到目前为止编写的代码。如果你能告诉我什么可能会给我带来问题,将不胜感激。(它没有给出错误代码,而是BlueJ,我正在使用的软件,只是不断尝试运行该程序)

import java.io.*;
import java.util.Scanner;
public class Project7
{
public static void main(String[] args)
throws FileNotFoundException
{
Scanner inF = new Scanner(new File("inputpro7.txt"));
int [] arrayNum = new int[7];
double [] arrayWeight = new double[7];
int place = inF.nextInt();
double weight = inF.nextDouble();
for (int m = 0; m < 7; m++)
{
arrayNum[m] = m + 1;
}
while (inF.hasNextLine())
{
if (place == 1)
arrayWeight[0] += weight;
else if (place == 2)
arrayWeight[1] += weight;
else if (place == 3)
arrayWeight[2] += weight;
else if (place == 4)
arrayWeight[3] += weight;
else if (place == 5)
arrayWeight[4] += weight;
else if (place == 6)
arrayWeight[5] += weight;
else 
arrayWeight[6] += weight;    
} 
for (int k = 0; k < 7; k++) 
System.out.printf("%6d %6.2f%n", arrayNum[k],                  
arrayWeight[k]);
inF.close();
}
}

代码中的问题出在 While 循环中。您正在连续读取输入文件的第一行。While 循环中没有读取下一行语句。即使你输入了该语句(阅读下一行),你也需要在代码中进行一些修改。以下是修改后的代码。

public class ExampleCode {
public static void main(String[] args) throws FileNotFoundException {
Scanner inF = new Scanner(new File("Input.txt"));
int[] arrayNum = new int[7];
double[] arrayWeight = new double[7];
int place;
double weight;
for (int m = 0; m < 7; m++) {
arrayNum[m] = m + 1;
}
while (inF.hasNextLine()) {
place = inF.nextInt();
weight = inF.nextDouble();
if (place == 1)
arrayWeight[0] += weight;
else if (place == 2)
arrayWeight[1] += weight;
else if (place == 3)
arrayWeight[2] += weight;
else if (place == 4)
arrayWeight[3] += weight;
else if (place == 5)
arrayWeight[4] += weight;
else if (place == 6)
arrayWeight[5] += weight;
else
arrayWeight[6] += weight;
}
for (int k = 0; k < 7; k++)
System.out.printf("%6d %6.2f%n", arrayNum[k], arrayWeight[k]);
inF.close();
}
}

解决此类问题"处理输入文件并根据相似的键值聚合输入"的最佳方法是使用 HashMap。以下是可能的解决方案。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class ExampleCode {
public static void main(String[] args) throws FileNotFoundException
{
Scanner inF = new Scanner(new File("Input.txt"));
Map<String, Double> table = new HashMap<String, Double>(); 
String line = "";
String token[] = new String[2];
//Process till end of file
while (inF.hasNextLine())
{
//Read each lines
line = inF.nextLine();
//Split line based on separator i.e " " character
//use these tokens as Key-Value pairs
token = line.split(" ");
//If table already has the entry add the value to same key entry
if(table.containsKey(token[0])){
Double value = table.get(token[0]) + new Double(token[1]);
table.put(token[0], value);
} else { //If key not found, insert the new entry
table.put(token[0], new Double(token[1]));
}
}
for(Entry entry : table.entrySet()){
System.out.println("Key:"+entry.getKey()+" == Value:"+entry.getValue());
}
//close the file 
inF.close();
}
}
Sample Input file: 
2 9.25
4 8.5
1 1.5
2 4.215
5 1.4555
6 8.989
7 1
4 8.049
1 3.567
Output : 
Key:1 == Value:5.067
Key:2 == Value:13.465
Key:4 == Value:16.549
Key:5 == Value:1.4555
Key:6 == Value:8.989
Key:7 == Value:1.0

最新更新