[英]finding the Average from text file using ArrayList
name, place, money
-----------------
rohit1, us,1500
rohit2, us,1600
rohit3, ind,1700
rohit4, ind,1800
我们如何比较用户“ p”在arraylist o = 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();
}
}
}
我没有使用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();
}
如果您有文件数据的实体,那就太好了。 就像是:
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()));
}
一开始我已经用这种方式解决了这个问题,因为我不知道该怎么做,我们如何进一步优化这一点,我尝试使用lambdas会有所帮助
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.