java scanner 就是我知道如何用它 但是我现在有的数据是
分别对应的是 MM/DD/YY, HH:MM, Category, Price, Quantity, Rating, Duration
然后我现在在scanner这步困住了 我已经建立了我的class 并且要把我的数据 一一把它放进去 然后达到我想要的效果
import java.util.Scanner;
public class Deal {
private String date;
private String time;
private String type;
private Double price;
private Double quantity;
private Double rating;
private Double duration;
public Deal(String date, String time, String type, Double price, Double quantity, Double rating, Double duration) {
this.date = date;
this.time = time;
this.type = type;
this.price = price;
this.quantity = quantity;
this.rating = rating;
this.duration = duration;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
s.nextInt() =
}
我尝试了用scanner 但是我不知道如何处理多行数据 就是我只会把一行的数据分类 然后后面就完全不知道了
然后我也不知道该怎么去找对应的数值
就比如说我把每一行的价格都找到 然后归类到我的price里 这样我就会有一个都是price数值的array 那我可以找到一个最大的值 可是我怎么去找相对的卖的东西是什么,时间是什么 就是我不知道在我写的code的基础上如何去用scanner来完成我想要达到的效果
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Deal {
private String date;
private String time;
private String type;
private Double price;
private Double quantity;
private Double rating;
private Double duration;
public Deal(String date, String time, String type, Double price, Double quantity, Double rating, Double duration) {
this.date = date;
this.time = time;
this.type = type;
this.price = price;
this.quantity = quantity;
this.rating = rating;
this.duration = duration;
}
public Double getPrice() {
return price;
}
public String getDate() {
return date;
}
public String getTime() {
return time;
}
public String getType() {
return type;
}
public Double getQuantity() {
return quantity;
}
public Double getRating() {
return rating;
}
public Double getDuration() {
return duration;
}
public static void main(String args[]) throws Exception{
List<Deal> list = new ArrayList<>();
//读取文件
File file = new File("test.txt");
Scanner sc = new Scanner(file);
//文件每行取值
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] argsArr = line.split(" ");
//对象生成
Deal deal = new Deal(argsArr[0], argsArr[1], argsArr[2], Double.parseDouble(argsArr[3]),
Double.parseDouble(argsArr[4]), Double.parseDouble(argsArr[5]), Double.parseDouble(argsArr[6]));
list.add(deal);
}
//最大最小值处理
Deal max = list.get(0);
Deal min = list.get(0);
for(Deal deal : list) {
if(deal.getPrice() > max.getPrice()) {
max = deal;
}
if(deal.getPrice() < min.getPrice()) {
min = deal;
}
}
System.out.println("Highest per unit sale:");
System.out.println("\tWhen:" + max.getDate() + " " + max.getTime());
System.out.println("\tCategory:" + max.getType());
System.out.println("\tPrice:" + max.getPrice());
System.out.println("\tRating:" + max.getRating());
System.out.println("Lowest per unit sale:");
System.out.println("\tWhen:" + min.getDate() + " " + min.getTime());
System.out.println("\tCategory:" + min.getType());
System.out.println("\tPrice:" + min.getPrice());
System.out.println("\tRating:" + min.getRating());
//类别平均处理
Map<String, List<Deal>> map = new HashMap<>();
for(Deal deal : list) {
if(map.get(deal.getType()) == null) {
List<Deal> itemList = new ArrayList<>();
itemList.add(deal);
map.put(deal.getType(), itemList);
}else {
map.get(deal.getType()).add(deal);
}
}
HashMap<String, String> resultMap = new HashMap<String, String>();
for(Entry<String, List<Deal>> entry : map.entrySet()) {
Double sumPrice = 0.0;
Double sumQuantity = 0.0;
Double sumRating = 0.0;
Double sumDuration = 0.0;
List<Deal> valueList = entry.getValue();
for(Deal deal : valueList) {
sumPrice = sumPrice + deal.getPrice();
sumQuantity = sumQuantity + deal.getQuantity();
sumRating = sumRating + deal.getRating();
sumDuration = sumDuration + deal.getDuration();
}
DecimalFormat dmf = new DecimalFormat(".00");
String str = "Averages by " + entry.getKey() + ":" + "\n\tQuantity:" +
dmf.format((sumQuantity/valueList.size())) + "\n\tPrice:" +
dmf.format((sumPrice/valueList.size())) + "\n\tRating:" +
dmf.format((sumRating/valueList.size())) + "\n\tDuration:" +
dmf.format((sumDuration/valueList.size()));
resultMap.put(entry.getKey(), str);
}
System.out.println(resultMap.get("book"));
System.out.println(resultMap.get("jewelry"));
System.out.println(resultMap.get("phone"));
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
class Deal {
private String date;
private String time;
private String type;
private Double price;
private Double quantity;
private Double rating;
private Double duration;
public String getType() {
return type;
}
public Double getPrice() {
return price;
}
public Double getQuantity() {
return quantity;
}
public Double getRating() {
return rating;
}
public Double getDuration() {
return duration;
}
public Deal(String date, String time, String type, Double price, Double quantity, Double rating, Double duration) {
this.date = date;
this.time = time;
this.type = type;
this.price = price;
this.quantity = quantity;
this.rating = rating;
this.duration = duration;
}
@Override
public String toString() {
return "When:"+date+" "+time+"\nCategory:"+type+"\nPrice:"+price+"\nRating:"+rating;
}
}
public class Main {
public static void main(String[] args) throws FileNotFoundException {
List<Deal> list=new ArrayList<>();
File file = new File("test.txt");
Scanner sc = new Scanner(file);
sc.nextLine();
while (sc.hasNext()) {
String temp = sc.nextLine();
String[] s = temp.split(" ");
list.add(new Deal(s[0],s[1],s[2],Double.parseDouble(s[3]),Double.parseDouble(s[4]),Double.parseDouble(s[5]),Double.parseDouble(s[6])));
}
//遍历集合
double max=list.get(0).getPrice();
double min=list.get(0).getPrice();
int maxIndex=0;
int minIndex=0;
for(int i=1;i<list.size();i++){
Deal deal = list.get(i);
if (deal.getPrice()>=max){
max=deal.getPrice();
maxIndex=i;
}
if (deal.getPrice()<=max){
min=deal.getPrice();
minIndex=i;
}
}
System.out.println("Highest per unit sale:");
System.out.println(list.get(maxIndex));
System.out.println("Lowest per unit sale:");
System.out.println(list.get(minIndex));
Map<String, List<Deal>> groupMap = list.stream()
.collect(Collectors.groupingBy(Deal::getType));
groupMap.forEach(
(k,v)->{
int quantity=0;
double price=0,rating=0,duration=0;
for(Deal d:v){
quantity+=d.getQuantity();
price+=d.getPrice();
rating+=d.getRating();
duration+=d.getDuration();
}
System.out.println("Averages by "+k);
System.out.println("Quantity:"+quantity+"\nPrice:"+String.format("%.2f",price/v.size())+"\nRating:"+String.format("%.2f",rating/v.size())+"\nDuration:"+String.format("%.2f",duration/v.size()));
}
);
}
}