以下是我的代码。当我执行 'System.out.println(e.getBrowserInfo().getBrowser()); ' 在 Webstat 类的最后一行代码中。结果给了我 8 个“Chrome”的结果。这是错误的,因为它假设给出的答案是“Chrome”、“IE”、“Chrome”、“Firefox”“、Chrome”、“Safari”、“Chrome”和“IE”。我有问题弄清楚和请帮助如何获得正确的结果。
- -代码 - - - - - - - - - - - - - - - - -
package problem;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.security.Timestamp;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import d1Assignment.Helper;
public class WebStat {
private static int actionId = 100;
private static String browser;
private static double ConnectionSpeed;
private String[] webinfo;
private static ArrayList<SiteEntry> siteEntrydetails = new ArrayList<SiteEntry>();
private static ArrayList<SiteExit> siteExitdetails = new ArrayList<SiteExit>();
private static ArrayList<PageHop> pageHopdetails = new ArrayList<PageHop>();
public static void main(String[] args) {
WebStat wl = new WebStat();
wl.start();
}
private void start() {
int option = -1;
while (option != 6) {
updateArrayList();
menu();
option = Helper.readInt("Enter choice > ");
if (option == 1) {
// AddNewSiteEntry();
} else if (option == 2) {
// AddNewSiteExit();
}else if (option == 3) {
// AddPageHop();
} else if (option == 4) {
// ViewBrowseActions();
}
else if (option == 6) {
System.out.println("Thank you for using our Service!");
}
}
}
private void menu() {
Helper.line(60, "=");
System.out.println("WELCOME TO MY WEB STAT PORTAL");
Helper.line(60, "=");
System.out.println("1. Add New Site Entry");
System.out.println("2. Add New Site Exit");
System.out.println("3. Add New Page Hop");
System.out.println("4. List Browse Actions by Date Range");
System.out.println("5. Export with Common Statistics Format");
System.out.println("6. Quit");
}
private void updateArrayList() {
siteEntrydetails.clear();
siteExitdetails.clear();
pageHopdetails.clear();
try {
BufferedReader br = new BufferedReader(new FileReader(new File("visitdetails.txt"))); //read file from items.txt
String line = br.readLine();
while(line != null){
browser = "";
ConnectionSpeed = 0.00;
webinfo = line.split(",");
webinfo[0] = webinfo[0].trim();
actionId = Integer.parseInt(webinfo[0]);
webinfo[1] = webinfo[1].trim();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH:mm");
LocalDateTime dateTime = LocalDateTime.parse(webinfo[1], formatter);
webinfo[6] = webinfo[6].trim();
double d = Double.parseDouble(webinfo[6]);
BrowseInfo browserInfo = new BrowseInfo (webinfo[5],d);
//System.out.println(String.format("%-10d, %-10s, %-10s %-10s %-10s", actionId,dateTime,webinfo[2],browserInfo.getBrowser(),browserInfo.getconnectionSpeed()));
if(webinfo[2].trim().equalsIgnoreCase("entry")) {//save entries with entry in visitdetails.txt into ArrayList
SiteEntry SiteEntryObj = new SiteEntry(actionId,dateTime,webinfo[3],webinfo[4],browserInfo);
siteEntrydetails.add(SiteEntryObj);
browser = "";
ConnectionSpeed = 0.00;
}
if(webinfo[2].trim().equalsIgnoreCase("exit")) {//save entries with exit in visitdetails.txt into ArrayList
SiteExit SiteExitObj = new SiteExit(actionId,dateTime,webinfo[3],webinfo[4],browserInfo);
siteExitdetails.add(SiteExitObj);
browser = "";
ConnectionSpeed = 0.00;
}
if(webinfo[2].trim().equalsIgnoreCase("hop")) {//save entries with hop in visitdetails.txt into ArrayList
PageHop PageHopObj = new PageHop(actionId,dateTime,webinfo[3],webinfo[4],webinfo[5],d);
pageHopdetails.add(PageHopObj);
browser = "";
ConnectionSpeed = 0.00;
}
line = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
for(SiteEntry e: siteEntrydetails) {
System.out.println(e.getBrowserInfo().getBrowser() ); //problem lies here
}
}
}
---代码结束-------------------------------------------- - - - - -输出 - - - - - - - - - - - - - - - -
铬
铬
铬
铬
铬
铬
铬
铬
--------输出结束--------------------------------
以下是 visitdetails.txt 的内容:
101 ,2021040903:07 ,entry ,www.google.com.sg ,home ,Chrome ,55.78
102 ,2021040914:43 ,entry ,www.google.com ,home ,IE ,99.11
103 ,2021040915:26 ,entry ,www.soccernet.com.sg ,home ,Chrome ,34.45
104 ,2021040915:41 ,entry ,www.google.com ,home ,Firefox ,233.00
105 ,2021040915:45 ,exit ,Join us ,www.google.com ,Chrome ,99.11
106 ,2021040917:37 ,exit ,Shop ,www.yahoo.new.com.sg ,Safari ,39.11
107 ,2021040917:43 ,hop ,Join ,Shopping ,Firefox ,88.11
108 ,2021040917:44 ,hop ,Check Out ,Join Us ,IE ,88.19
109 ,2021040918:03 ,entry ,www.google.com.sg ,Home ,Chrome ,88.11
110 ,2021040918:06 ,entry ,www.manutd.com ,home ,Safari ,99.11
111 ,2021041001:07 ,entry ,www.Liverpool.com ,Find ,Chrome ,89.11
112 ,2021041001:07 ,exit ,Shop ,www.mancity.com ,Firefox ,111.99
113 ,2021041013:57 ,entry ,www.google.com ,home ,IE ,71.77
114 ,2021041013:58 ,exit ,Check Out ,www.google.com ,Chrome ,38.19
115 ,2021041014:00 ,hop ,Join Us ,www.maskgowhere.com.sg ,Safari ,55.89