从文件读取输入并计算Java中的出现次数

时间:2018-09-04 16:51:33

标签: java file java-stream

我是Java新手。请帮助解决以下问题说明。

我有一个如下所述的输入文件,并以逗号分隔的“ id,invoiceNumber,custid,totalamt,amountdue”按以下顺序排列了值。我需要找出谁的应付帐款金额大于1000。如果一个监护人被重复多次,且Dueamt> 1000,那么我需要打印未到期的应付款项。

*Input file :
102,12545,111,10000,5000
103,12546,111,10300,4000
104,12545,110,10000,2000*

*Output in the console:
cust id : 111
No of pending due payment ; 2
Total due amount : 9000
cust id : 110
No of pending due payment ; 1
Total due amount : 2000*

我正在处理下面的代码,但没想到

public static void main(String[] args) throws FileNotFoundException, IOException 
    {

        String line = null;
        Long custid;
        Double dueamt;
        int count = 1;
        Double newdue;
        Map<Long,Map> hashmap = new TreeMap<>();
        Invoice invoiceobj = null;
        try(BufferedReader br1 = new BufferedReader(new FileReader("input.txt")))
        {
            while((line = br1.readLine()) != null)
            {
                invoiceobj = new Invoice();
                String[] detailsarr = line.split(",");
                invoiceobj.setId(Long.parseLong(detailsarr[0]));
                invoiceobj.setInvoiceNumber(detailsarr[1]);
                invoiceobj.setCustomerId(Long.parseLong(detailsarr[2]));
                custid = Long.parseLong(detailsarr[2]);
                invoiceobj.setTotalAmount(Double.parseDouble(detailsarr[3]));
                invoiceobj.setAmountDue(Double.parseDouble(detailsarr[4]));
                dueamt = Double.parseDouble(detailsarr[4]);

                if(hashmap.containsKey(custid))
                {
                    Map<Double,Integer> hashmap2 = hashmap.get(custid);
                    newdue = olddue + dueamt;
                    count++;
                    hashmap2.put(newdue, count);



                }
                else
                {
                Map<Double,Integer> hashmap1 = new TreeMap<>(); 
                hashmap1.put(dueamt, 1);
                hashmap.put(custid, hashmap1);
                }
    }

            for(Map.Entry<Long,Double> entry : hashmap2.entrySet())
            {
                Long custid1 = entry.getKey();
                Double amt = entry.getValue();



                if(amt>1000)
                {
                System.out.println("Customer id "+custid1);

                System.out.println("Number of invoice pending for payment:"+count);

                System.out.println("Total Due Amount: $"+amt);
                }


            }

2 个答案:

答案 0 :(得分:1)

您还可以通过在一张地图中存储客户ID,到期金额并在另一张地图中计算客户ID来改善逻辑。

Map<Long,Double> map1 = new TreeMap<>();
 Map<Long,Integer> map2 = new HashMap<>();
 if(map1.containsKey(custid)){
    Double currDue = map1.get(custid);
    currDue+=dueAmt;
    map1.put(custid,currDue);
    map2.put(custid,map2.get(custid)+1);
} else {
    map1.put(custid,dueAmt);
    map2.put(custid,1);
}

最后,只需遍历map1的条目集,并检查值是否大于1000,然后使用相同的键从map2获取计数。

答案 1 :(得分:0)

如果您在代码中使用Multimap,则可以轻松完成,并且不需要两个或多个映射。 Multimap允许您在同一个键上存储多个值(对于单个dueamt,这里将是多个custid),随后对其进行迭代以添加所需的最终金额。另外,您无需计数金额,只需检查每个键的值数即可包含发票计数。 关于Multimap的更多详细信息是here

将其导入为:

import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap;

public static void main( String[] args ) throws FileNotFoundException, IOException {
    String line = null;
    Long custid;
    Double dueamt;
    Multimap< Long, Double > hashmap = ArrayListMultimap.create( );

    try (BufferedReader br1 = new BufferedReader( new FileReader( "input.txt" ) )) {

        while ( ( line = br1.readLine( ) ) != null ) {

            String[] detailsarr = line.split( "," );
            Long invoiceID = Long.parseLong( detailsarr[ 0 ] );
            String invoiceNumber = detailsarr[ 1 ];
            custid = Long.parseLong( detailsarr[ 2 ] );
            Double totalAmount = Double.parseDouble( detailsarr[ 3 ] );
            dueamt = Double.parseDouble( detailsarr[ 4 ] );
            if ( dueamt > 1000.00 ) {
                hashmap.put( custid, dueamt );
            }
        }
    }
    for ( Long key: hashmap.keySet( ) ) {
        System.out.println( "CustomerId " + key );
        System.out.println( "Number of invoice pending for payment:" + hashmap.get( key ).size( ) );
        System.out.println( "Total Due Amount: $" + hashmap.get( key ).stream( ).mapToDouble( Double::doubleValue ).sum( ) );
    }
}

输出:

CustomerId 110
Number of invoice pending for payment:1
Total Due Amount: $2000.0
CustomerId 111
Number of invoice pending for payment:2
Total Due Amount: $9000.0