字符串数组java中的分组和总和销售

时间:2019-03-15 16:54:27

标签: java arrays readfile

我的文本文件如下:

Pen 100 Ink 50 Pen 150 Paper 20

,我想对每种商品求和 输出应如下所示:

Pen 250
Ink 50
Paper 20
Max Pen 250
Min Paper 20

我在下面只写了一些代码,但卡住了:

public  static void readData(){
File infile = new File("D:\\itemData.txt");
        String itemName = new String();
        String[] nameList = new String[100];
        String[] saleList = new String[100];
        int sale;
        int count =0;
        try {
            Scanner data = new Scanner(infile);
            while (data.hasNext()) {
                itemName = data.next();
                sale = data.nextInt();
                nameList[count] = name;
                saleList[count] = String.valueOf(sale);
                count++;
                for (int i = 0; i < nameList.length; i++) {
                    if (nameList[i] != null) {
                        System.out.println(nameList[i] + " " + saleList[i]);
                    }
                }
                data.close();
            }
        } catch (IOException e) {
        }}

3 个答案:

答案 0 :(得分:0)

尝试下面的代码。 我可以看到代码中有2个问题。 a)您没有声明变量count b)您正在读取数据时正在打印,这是不希望的。

public  static void readData(){
File infile = new File("D:\\itemData.txt");
        String itemName = new String();
        String[] nameList = new String[100];
        String[] saleList = new String[100];
        int sale;
        int count = 0;
        try {
            Scanner data = new Scanner(infile);
            while (data.hasNext()) {
                itemName = data.next();
                sale = data.nextInt();
                nameList[count] = name;
                saleList[count] = String.valueOf(sale);
                count++;

                data.close();
            }
        } catch (SIOException e) {
        }
        for (int i = 0; i < nameList.length; i++) {
                    if (nameList[i] != null) {
                        System.out.println(nameList[i] + " " + saleList[i]);
                    }
                }
}

答案 1 :(得分:0)

还可以考虑实施类似的方法来为您跟踪最大和最小:

package com.example.demo;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.collections4.map.HashedMap;

public class Inventory {
    private Map<String, AtomicInteger> totals = new HashedMap<>();
    private Map<String, AtomicInteger> maxes = new HashedMap<>();
    private Map<String, AtomicInteger> mins = new HashedMap<>();

    public void add(String name, int number) {
        getOrCreateFromMap(totals, name, 0).addAndGet(number);

        getOrCreateFromMap(maxes, name, Integer.MIN_VALUE).getAndUpdate(present -> (present < number) ? number : present);

        getOrCreateFromMap(mins, name, Integer.MAX_VALUE).getAndUpdate(present -> (number < present) ? number : present);
    }

    private AtomicInteger getOrCreateFromMap(Map<String, AtomicInteger> map, String name, int initialValue) {
        return map.computeIfAbsent(name, x -> new AtomicInteger(initialValue));
    }

    public int getTotal(String name) {
        return getFromMap(totals, name);
    }

    public int getMax(String name) {
        return getFromMap(maxes, name);
    }

    public int getMin(String name) {
        return getFromMap(mins, name);
    }

    private int getFromMap(Map<String, AtomicInteger> map, String key) {
        return map.computeIfAbsent(key, x -> new AtomicInteger())
                  .get();
    }

    public static void main(String[] args) {
        Inventory i = new Inventory();

        i.add("paper", 50);
        i.add("paper", 150);

        System.out.println(i.getTotal("paper"));
        System.out.println(i.getMax("paper"));
        System.out.println(i.getMin("paper"));

    }
}

答案 2 :(得分:0)

这是一个有效的示例,用于提到的问题陈述-

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.stream.Collectors;

public class ReadAndSumFileData {

    public static void main(String... arguments) throws IOException {
        String filename = "C:/temp/SalesData.txt";
        readData(filename);
    }

    private static void readData(String filename) throws IOException {
        Map<String, Integer> map = Files.lines(Paths.get(filename))
                .map(s -> s.split("\\s+"))
                .collect(Collectors.groupingBy(a -> a[0], Collectors.summingInt(a -> Integer.parseInt(a[1]))));

        map.entrySet().stream().forEach(e-> System.out.println(e));
    }
}