简体   繁体   English

如果我有多个属性,如何读取txt文件?

[英]How do I read txt file if I have multiple attributes?

I am trying to get a class to read my txt file with a few lines, for example: 我正在尝试获取一个类以几行读取我的txt文件,例如:

Facial Lotion, 1 , 2, 0.1 面部乳液,1,2,0.1

Moisturiser Lotion, 2, 3, 0.2 保湿乳液,2,3,0.2

Toner Lotion, 3, 4, 0.3 化妆水3,4,0.3

Aloe Vera Lotion, 4, 5, 0.4 芦荟乳液4、5、0.4

I created a class call Lotion with attributes name(string), productNo(int), productRating(int), and productDiscount(double, and I create another class call ListOfLotion and add in an arraylist of Lotion. 我创建了一个具有属性名称(字符串),productNo(int),productRating(int)和productDiscount(double)的类调用Lotion,并创建了另一个类ListOfLotion并添加了Lotion的数组列表。

my problem is how do i get my ListOfLotion class to use the values in txt file and put it in my arraylist. 我的问题是如何获取ListOfLotion类以使用txt文件中的值并将其放入我的arraylist中。

I tried to use indexOf for name till the next one but i got error, java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 17 我尝试使用indexOf作为名称,直到下一个,但是出现错误java.lang.StringIndexOutOfBoundsException:开头为0,结尾为-1,长度为17

also is there anyway i could separate all four value and make sure they are store properly for example, Facial Lotion is store as the name and 1 is store as prodcuctNo. 无论如何,我也可以将所有四个值分开,并确保它们正确存储,例如,Facial Lotion作为名称存储,而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)

Is it beccause I put readLine,indexOf(',') as every readLine, it just stop at the first ','? 是否因为我将readLine,indexOf(',')用作每个readLine,而只是在第一个','处停止了? Anyway I could effectively let java know that between this and this index is for name, and between this and this index is for productNo? 无论如何,我可以有效地让java知道此索引与该索引之间是名称,而此索引与该索引之间是productNo吗?

thanks guys really appreciate it. 谢谢大家真的很感激。

You could use a regex ( Demo ): 您可以使用正则表达式( Demo ):

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

Which you could define as a constant in your class: 您可以在类中将其定义为常量:

private static final Pattern LOTION_ENTRY = 
    Pattern.compile("([\\w\\s]+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+(?:\\.\\d+))");

Then you can just create a Matcher for every entry and extract the groups: 然后,您可以为每个条目创建一个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
}

A note though: the parseInt() and parseDouble can throw a NumberFormatException if the input is not valid. 不过请注意:如果输入无效, parseInt()parseDouble可以引发NumberFormatException So you'd have to catch those and act accordingly. 因此,您必须抓住这些并采取相应措施。

Since the lines are comma-separated lists you could use split() to split the line into the single variables. 由于行是逗号分隔的列表,因此您可以使用split()将行拆分为单个变量。
Another thing to consider is that Scanner("file.txt") doesn't read the indicated text file but just the given String . 要考虑的另一件事是Scanner("file.txt")不会读取指示的文本文件,而只会读取给定的String You have to create a File object first. 您必须先创建一个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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM