简体   繁体   English

Java阵列未打印(正在使用BlueJ)

[英]Java Array Not Printing (BlueJ being used)

I am currently trying to complete a task for college that requires using java programming. 我目前正在尝试完成要求使用Java编程的大学任务。 The task goes as follows. 任务如下。

I have to sort through an input file for a "fishing competition". 我必须对输入文件进行分类以进行“钓鱼比赛”。 The file firstly gives an integer (between 1-7), then a real number, for each line. 该文件首先为每行给出一个整数(1-7之间),然后是一个实数。 The tricky part is that the numbers are not already added up, or in order. 棘手的部分是尚未将这些数字累加或按顺序累加。 This means I have to make the program read the integer, prove its needed location, then add the following weight to that location. 这意味着我必须使程序读取整数,证明其所需的位置,然后在该位置添加以下权重。

Here's an idea of what the file looks like 这是文件外观的一个想法

3 36.2 3 36.2

6 27.8 6 27.8

7 10.3 7 10.3

I have the general idea on how to complete the whole task, but I am getting stuck on one part, which is printing the total weight for each competitor. 我对如何完成整个任务有一般的想法,但是我陷入了一个问题,那就是打印每个竞争对手的总重量。

Here is the code that I have written so far. 这是我到目前为止编写的代码。 If you could tell me what may be giving me problems, it would be much appreciated. 如果您能告诉我是什么给我带来了问题,将不胜感激。 (It is not giving an error code, rather, BlueJ, the software i am using, just continuously tries to run the program) (它没有给出错误代码,而是我正在使用的软件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(); } } 

The problem in your code is in While loop. 您的代码中的问题在While循环中。 You are continuously reading the first line of the Input file. 您正在连续读取输入文件的第一行。 There is no read nextline statement inside While loop. While循环内没有已读取的nextline语句。 Even when you put that statement (reading next line) you need some modification in your code. 即使您放置该语句(阅读下一行),您也需要在代码中进行一些修改。 Following is the modified code. 以下是修改后的代码。

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();
    }
}

Best way to solve such problem "Process Input file and aggregate the input based on similar Key values" is to use HashMap. 解决此类问题“处理输入文件并基于相似的键值聚合输入”的最佳方法是使用HashMap。 Following is the possible solution. 以下是可能的解决方案。

    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

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

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