亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果我有多個屬性,如何讀取txt文件?

如果我有多個屬性,如何讀取txt文件?

梵蒂岡之花 2022-11-30 13:26:21
我正在嘗試讓一個類用幾行來讀取我的 txt 文件,例如:洗面奶, 1 , 2, 0.1保濕乳液, 2, 3, 0.2爽膚水, 3, 4, 0.3蘆薈乳液, 4, 5, 0.4我創建了一個具有屬性名稱(字符串)、productNo(int)、productRating(int) 和 productDiscount(double) 的類調用 Lotion,我創建了另一個類調用 ListOfLotion 并添加到 Lotion 的數組列表中。我的問題是如何讓我的 ListOfLotion 類使用 txt 文件中的值并將其放入我的數組列表中。我嘗試使用 indexOf 作為名稱直到下一個,但出現錯誤,java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 17無論如何我也可以分開所有四個值并確保它們正確存儲,例如,面部乳液存儲為名稱,1 存儲為 prodcuctNo。public void addListOfLotion(){    ArrayList<Lotion> lotion = new ArrayList<Lotion>();    Scanner scanner = new Scanner("Desktop/Lotion.txt");    while(scanner.hasNext()){     String readLine = scanner.nextLine();     int indexProductNo = readLine.indexOf(',');     int indexOfProductRating = readLine.indexOf(',');     double indexOfProductDiscount = readLine.indexOf(',');      lotion.add(new Lotion(readLine.substring(0, indexOfProductNo),0,0,0));    }scanner.close();    }Got this error as result:   java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 17    at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)    at java.base/java.lang.String.substring(String.java:1874)    at ListOfVenues.addListOfLotion(ListOfLotion.java:42)是因為我把 readLine,indexOf(',') 作為每個 readLine,它只是停在第一個 ',' 處嗎?無論如何,我可以有效地讓 java 知道這個和這個索引之間是名稱,這個和這個索引之間是產品號?謝謝大家真的很感激。
查看完整描述

2 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

由于這些行是以逗號分隔的列表,您可以使用split()該行將行拆分為單個變量。

另一件需要考慮的事情是Scanner("file.txt")不讀取指定的文本文件,而只讀取給定的String. 您必須先創建一個File對象。


File input = new File("Desktop/Lotion.txt");

Scanner scanner;

scanner = new Scanner(input);

while(scanner.hasNext()){

    String readLine = scanner.nextLine();

    String[] strArray = readLine.split(",");

    int indexOfProductNo = Integer.parseInt(strArray[1].trim());

    int indexOfProductRating = Integer.parseInt(strArray[2].trim());

    double indexOfProductDiscount = Double.parseDouble(strArray[3].trim());

    lotion.add(new Lotion(strArray[0],indexOfProductNo,indexOfProductRating,indexOfProductDiscount));

}


查看完整回答
反對 回復 2022-11-30
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

您可以使用正則表達式(Demo):


([\w\s]+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+(?:\.\d+))

您可以將其定義為班級中的常量:


private static final Pattern LOTION_ENTRY = 

    Pattern.compile("([\\w\\s]+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+(?:\\.\\d+))");

然后你可以Matcher為每個條目創建一個并提取組:


Matcher matcher = LOTION_ENTRY.matcher(readLine);


if(matcher.matches()) {

    String name = matcher.group(1);

    int no = Integer.parseInt(matcher.group(2));

    int rating = Integer.parseInt(matcher.group(3));

    double discount = Double.parseDouble(matcher.group(4));


    // do something

} else {

    // line doesn't match pattern, throw error or log

}

不過請注意:如果輸入無效,則parseInt()andparseDouble可以拋出 a 。NumberFormatException所以你必須抓住那些并采取相應的行動。


查看完整回答
反對 回復 2022-11-30
  • 2 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號