How to Split a text file with no pattern

pwhockey61 :

I am given a messy set of data and have to split the data into categories. The first line is tax rate, The following line is supposed to be "items," "number of items," and price (columns). I just need help splitting the data accordingly. Any help would be appreciated.

0.05  5
Colored Pencils 3 0.59
Notebook 5 0.99
AAA Battery  5 0.99
Java Book 5 59.95
iPhone X 2 999.99
iPhone 8 3 899.99.
import java.io.*;
import java.util.Scanner;

public class ShoppingBagClient {

    public static void main(String[] args) {
        File file = new File("data.txt");
        shoppingBag(file);
    }

    public static void shoppingBag (File file) {
        Scanner scan;
        String itemName=" ";
        int quantity = 0;
        float price = 0;
        int count = 0;
        float taxRate;

        ShoppingBag shoppingBag = new ShoppingBag(6, .5f);
        try {
            scan = new Scanner(new BufferedReader(new FileReader(file)));
            String dilimeter;
            while(count < 1) {
                String line = scan.nextLine();
                String[] arr = line.split(" ");
                taxRate = Float.parseFloat(arr[0]);


            }

            while(scan.hasNextLine() && count > 0){
                String line = scan.nextLine();
                String delimeter;
                String arr[] = line.split();
                itemName = arr[0];
                quantity = Integer.parseInt(arr[1]);
                price = Float.parseFloat(arr[2]);
                Item item =``` new Item(itemName, quantity, price);
                shoppingBag.push(item);
                System.out.println(itemName);

            }

        }
        catch(IOException e) {
            e.printStackTrace();
        }

    }

}
James Birch :

@Henry's comment gives a good approach. If you know the structure of each line and that it is delimited in a consistent manner (e.g. single space-separated) then you can combine lastIndexOf and substring to do the job.

String delimiter = " ";

String line = scan.nextLine();              // "Colored pencils 3 0.59"

int last = line.lastIndexOf(delimiter);     // "Colored pencils 3_0.59"

String price = line.substring(last + 1);    // "0.59"

line = line.substring(0, last);             // "Colored pencils 3"

last = line.lastIndexOf(delimiter);         // "Colored pencils_3"

String quantity = line.substring(last + 1); // "3"

line = line.substring(0, last);             // "Colored pencils"

String product = line;

This can be refactored to be tidier but illustrates the point. Be mindful that if lastIndexOf returns the final character in the line then substring(last + 1) will throw a StringIndexOutOfBoundsException. A check should also be taken for if lastIndexOf does not find a match in which case it will return -1.

Edit: The price and quantity can then be converted to an int or float as necessary.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=115364&siteId=1