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();
}
}
}
答案 0 :(得分:0)
我使用lambda代替了while循环
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();
}
答案 1 :(得分: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()));
}
答案 2 :(得分:0)
一开始我以这种方式解决了这个问题,因为我不知道该怎么办 我们如何进一步优化它,我尝试使用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();
}
}
}