简体   繁体   中英

How to read and from a text file and store in a class object in java

I am trying to read and split data from a text file into a class object as an array. I have a class ProductRecords:

class ProductRecord
{
    public String productCode;
    public String category;
    public String description;
    public int aisleNumber;
    public int currInventory;

    public ProductRecord(String code, String category, String description, int number, int currInventory)
    {
        this.productCode = code;
        this.category = category;
        this. description = description;
        this.aisleNumber = number;
        this.currInventory = currInventory;
    }

    public String getProductCode() {
        return productCode;
    }


    public String getCategory() {
        return category;
    }

    public String getDescription() {
        return description;
    }

    public int getAisleNumber() {
        return aisleNumber;
    }

    public int getCurrInventory() {
        return currInventory;
    }
}

and I am trying to read from this file

Thornton Hardware Store
7
P24-Qaa-1354 "hammer" "Bailey one-piece steel ripping hammer" 6 2
P24-Qbw-2495 "hammer drill" "Holsinger hammer drill, 8.5A 1/2-in" 7 1
P33-Qes-4782 "screwdriver" "Bailey 18-piece screwdriver set" 5 2
P25-Taa-1244 "nail" "Yolen framing nails, 50-count 3-1/4-in 28-degree" 6 20
P25-Tab-3509 "nail" "Yolen finish nails, 1000-count 18-guage 2-in" 6 9
P25-Tab-3506 "nail" "Yolen finish nails, 1000-count 16-guage 2-1/2-in" 6 14
P25-Tac-3672 "nail" "Yolen roofing nails, coil 1-1/4-in" 8 5
LC "nail"
LC "plant"
LP P24-Qbw-2495
LP P62-Aaa-1387
S P25-Tab-3506 5
S P24-Qbw-2495 1
R P25-Tab-3509 10

The first information is the name of a store and the second is the total number of items in the inventory. So for example on the third line to be stored in the class

P33-Qes-4782 "screwdriver" "Bailey 18-piece screwdriver set" 5 2

P33-Qes-4782  --> productCode
"screwdriver" --> category
"Bailey 18-piece screwdriver set" --> description
5 --> aisle number
2 --> current inventory

when i tried splitting it, it splits the whitespace between the category and the description and I don't want that. Here is my code.

is there a way to split this without affecting the white spaces in the braces with scanner?

Scanner input = new Scanner(System.in);

System.out.println("Enter the input fileName: ");
String fileName = input.nextLine();

try {
    Scanner file = new Scanner(new File(fileName));

    System.out.println();

    //number of products in the inventory
    String inLine = file.nextLine(); // stores the name of the store
    String line = file.nextLine();
    int numberOfItems = Integer.parseInt(line);

    System.out.println(numberOfItems);

    System.out.println("*************************");
    while (file.hasNextLine()) {

        String[] tokens = file.nextLine().split(" ");
        //String[] tokens = file.nextLine().
        for (int i = 0; i < 5; i++) {
            System.out.println(tokens[i]);
            //System.out.println("\n");
        }
        System.out.println("\n");
    }

    /*ProductRecord[] records = new ProductRecord[file.nextInt()];
    records[0].productCode = file.next();
    System.out.println(records[0]);*/
    /* for(int i =0; i < numberOfItems;i++)
     {

         //records[i] = new ProductRecord([])
     }*/

} catch (IOException ioe) {
    System.out.println("file not found");
}

By using String[] tokens = file.nextLine().split("\\"") instead of String[] tokens = file.nextLine().split(" ") , it will successfully remove " " , as well as maintain the structure of the string enclosed.

However, changes will need to be made to your logic to account for this change, as the debugging statements lose their current formatting by using this method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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