发现使用ArrayList的从文本文件中的平均

问题描述 投票:0回答:3
name, place, money
-----------------
rohit1, us,1500
rohit2, us,1600
rohit3, ind,1700
rohit4, ind,1800

我们如何可以比较这是由数组列表中Ø用户“P”输入值= VAL [1],然后总结所有的共同的地方钱找平均值和显示数据 这是大于金钱进入用户

public static void main(String args[]) throws IOException 
    {
        int n;
        String place=null;
        int money=0, h=0;   
        BufferedReader br = new BufferedReader(new 
        InputStreamReader(System.in));
        //Scanner sc = new Scanner(System.in);
        System.out.println("Enter Number"); 
        n=Integer.parseInt(br.readLine());
        System.out.println("Enter a Origin"); 
        money = br.readLine();  
        System.out.println("You entered String "+n+o);
        List<String> arr = new ArrayList<String>();
        try (BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\RhlSin\\Desktop\\car_input1.txt")))
        {

            String sCurrentLine;
            while ((sCurrentLine = bf.readLine()) != null) 

            {
                 String[] val = sCurrentLine.split(",");
                 if (val[1]==place)
                 {
                    money= Integer.parseInt(val[2]) ;
                     h= h+money;
                    //arr.add(val[0]);
                    //arr.add(sCurrentLine);
                    //System.out.println(o);
                    System.out.println(h);

                 }



            }

        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
}
java arrays arraylist
3个回答
0
投票

相反,while循环我用的lambda表达式

try(Stream<String> stream = Files.lines(Paths.get("C:\\Users\\RhlSin\\Desktop\\car_input1.txt"))) {
    List<BigDecimal> moneyFromPlace = stream.filter(line -> {
        // Filter by place
        String[] row = line.split(",");
        return row.length == 3 && row[1].trim() != "place" && row[1].trim().equals(o);
    }).map(line -> {
        // Find all the money for that place
        String[] row = line.split(",");
        return new BigDecimal(row[2].trim());
    }).collect(Collectors.toList());

    // Sum all the money and avarage
    BigDecimal sum = moneyFromPlace.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
    if(moneyFromPlace.size() > 0) {
        BigDecimal result = sum.divide(new BigDecimal(moneyFromPlace.size()), BigDecimal.ROUND_DOWN);
        System.out.println("Result: " + result);
    } else {
        System.out.println("No Result");
    }
} catch (IOException e) {
    e.printStackTrace();
}

0
投票

如果您对文件数据的实体这将是很好。就像是:

public class CarInput {
    private String name;
    private String origin;
    private double money;
    //Getters and setters
}

所以,你可以从序列化对象中的文件中的行:

List<CarInput> inputs = new ArrayList<>();
...
String[] val = sCurrent.split(",");
inputs.add(new CarInput(val[0], val[1], val[2]);

最后,您可以过滤和使用lambda表达式得到平均:

Map<String, List<CarInput>> inputsPerOrigin = inputs.stream()
    .collect(Collectors.groupingBy(CarInput::getOrigin));

Map<String, Double> averagePerOrigin = new HashMap<>();

inputsPerOrigin.forEach((origin, inputsSeparated) -> averagePerOrigin.put(origin, 
    inputsSeparated.stream().mapToDouble(CarInput::getMoney).average().getAsDouble()));

for (Map.Entry<String, Double> averages : averagePerOrigin.entrySet()) {
  System.out.println(String.format("Origin %s - Average %.2f", averages.getKey(), 
    averages.getValue()));
}

0
投票

在开始我有解决这个问题,在这样的COS我不知道该怎么办的想法,我们如何更优化这个,即时通讯与lambda表达式,这将有助于尝试

import java.io.*;
import java.util.ArrayList;
class carpro
{
    public static void main(String args[]) throws IOException 
    {
        int n, a=0, c=0, j=1,b=0;
        float horsepower=0, hp=0,avg=0;
        String o=null, name=null, origin=null ;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Number"); 
        n=Integer.parseInt(br.readLine());
        System.out.println("Enter a Origin"); 
        o = br.readLine();  
        //System.out.println("You entered String "+n+o);
        ArrayList<String> arr = new ArrayList<String>();
        ArrayList<String> arr1 = new ArrayList<String>();
        try (BufferedReader bf = new BufferedReader(new FileReader("C:\\Users\\RhlSin\\Desktop\\car_input1.txt")))
        {

            String sCurrentLine;
            while ((sCurrentLine = bf.readLine()) != null) 

            { 
            if( a>0){
                 String[] val = sCurrentLine.split(",");
                 arr.add(sCurrentLine);
                 name = val[0];
                 origin = val[1];
                 horsepower =Float.parseFloat(val[2]);     
                 if(origin.equals(o))
                 {
                     hp=hp+horsepower;
                     //System.out.println(name+"   "+origin+"  "+horsepower);
                     c=c+1;
                 }
                 avg= hp/c;

                //System.out.println(c);
            }
            a++;

            }
             //System.out.println(hp);
             //System.out.println(c);

        } catch (IOException e) {
            e.printStackTrace();
        }
//----------------------------------------------------------------------------------------------------------------------------           
        try (BufferedReader bg = new BufferedReader(new FileReader("C:\\Users\\RhlSin\\Desktop\\car_input1.txt")))
        {

            String sCurrent;
            while ((sCurrent = bg.readLine()) != null) 
            { 
                  if( b>0)
                    {

                      String[] valu = sCurrent.split(",");
                         arr1.add(sCurrent);
                         name = valu[0];
                         origin = valu[1];
                         horsepower =Float.parseFloat(valu[2]);
                          if(origin.equals(o)&&horsepower>=avg)
                             {
                                     {
                                         if(j<=n)
                                         {
                                             System.out.println(name+"   "+origin+"  "+horsepower);
                                             j++;
                                         }

                                     }
                            }    
                    }
                    b++;         
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 
    }       
}

推荐问答