Search code examples
javaread-write

How do I split up a line in tex file in read/write?


I currently have a text file that has the following:

1 Commercial & Enterprise  5   SLICE    59.99  IP MICRO 
2 Commercial & Enterprise  5   SLICE    59.99  MULTI-USE SWITCH
.
.
.
.
18 Government & Military   6   TCP      15.00  TCP 

I am trying to split the line so that I can have the following:

Product number:  18
Category:        Government & Military
Product name:  TCP
Units in stock: 6
Price: $15.00
Total value: $90.00
Fee: $4.50
Total value: $94.50  

I currently have the following code:

while ((line = lineReader.readLine()) != null) {

            StringTokenizer tokens = new StringTokenizer(line, "\t");

            p = new ActionProduct();
            add(p);
            String category = p.getCategory();
            String name = p.getName();
            category = tokens.nextToken();
            int item = p.getItem();
            double price = p.getPrice();
            int units = p.getUnits();

            while (tokens.hasMoreTokens()) {
            item = Integer.parseInt(tokens.nextToken());
            price = Double.parseDouble(tokens.nextToken());
            units = Integer.parseInt(tokens.nextToken());
            }

            System.out.println("Category: " + category);
            System.out.println("Product number:  " + item);
            System.out.println("Product name:  " + name);
            System.out.println("Units in stock: "+ units);
            System.out.println("Price: $" + String.format("%.2f", price)); 
            System.out.println("Total value: $" + String.format("%.2f",p.value()));
            System.out.println("Fee: $" + String.format("%.2f", p.fee()));

            System.out.println("Total value: $" + String.format("%.2f", value()));
        }

And I am getting this output instead:

Category: 1 Commercial & Enterprise  5   SLICE    59.99  IP MICRO             
Product number:  0
Product name:  null
Units in stock: 0
Price: $0.00
Total value: $0.00
Fee: $0.00
Total value: $0.00
Category: 2 Commercial & Enterprise  5   SLICE    59.99  MULTI-USE SWITCH     
Product number:  0
Product name:  null
Units in stock: 0
Price: $0.00
Total value: $0.00
Fee: $0.00
Total value: $0.00

So my questions is…what must I do to split up the line, so that I can print each value of my textile individually?? Thanks in advance guys, would really appreciate some direction!

Here is my text file:

1 Commercial & Enterprise  5   SLICE    59.99  IP MICRO             
2 Commercial & Enterprise  5   SLICE    59.99  MULTI-USE SWITCH     
3 Commercial & Enterprise  4   SLICE    59.99  2100                 
4 Commercial & Enterprise  6   SLICE    59.99  IP                   
5 Commercial & Enterprise  4   HDX      45.00  HYBRID CARRIER       
6 Commercial & Enterprise  10  TRANSip  45.00  IP Technology Suite  
7 Commercial & Enterprise  5   GUI      30.00  LINK COMMAND SYS     
8 Commercial & Enterprise  5   GUI      30.00  MAUI                 
9 Commercial & Enterprise  6   RCP      20.00  RCP                  
10 Government & Military   5   SLICE    60.00  IP MICRO             
11 Government & Military   5   SLICE    60.00  MULTI-USE SWITCH     
12 Government & Military   4   SLICE    60.00  2100                 
13 Government & Military   6   SLICE    55.00  IP                   
14 Government & Military   4   HDX.C    35.00  HYBRID CARRIER       
15 Government & Military   10  TRANSip  30.00  IP Technology Suite  
16 Government & Military   5   GUI      20.00  LINK COMMAND SYS     
17 Government & Military   5   GUI      20.00  MAUI                 
18 Government & Military   6   TCP      15.00  TCP  

Solution

  • Take a good look at the data. Are you getting more data, or is this the only file?

    If you're getting more data, you need to have some kind if spec, so you can be sure, that your parser will continue working.

    If you have fixed positioning of the data, then you can use

    String part = line.substring(beginIndex, endIndex)
    

    This data file is almost with fixed positions, except when the product number increases..

    Instead you can try with regex or line.split(delimitor)

    Don't use regex too much before you really understand them.

    If this was the only file, I think I would start with a

    String[] parts = line.split("  ") //two spaces
    

    and then parse from the string array you get.

    The first part, parts[0], would contain both product number and category, but you can split that as well.