Sandy
Sandy

Reputation: 55

Array concept on trying to get the no of lines in the file

I have below code which reads the input from a file using scanner class and sorts the input using comparable interface. This code works fine for a fixed array size but I want to count the no.of lines which are there in the file.

public class ReadItemData {

    public static void main(String[] args) throws Exception {

        Scanner aScanner = new Scanner(new FileReader(
                "src//chapter11//Items.txt"));

     double avg = 0;
         double sum = 0;
      Item[] itemArray = new Item[5];

            while (aScanner.hasNext()) 
        {
                String code = aScanner.next();
                String sku = aScanner.next();
                double qty = aScanner.nextDouble();

            itemArray[count] = new Item(code, sku, qty);
            System.out.println(itemArray[i]);
        i++;

        }
    Arrays.sort(itemArray[i]);
        System.out.println();
          for (Item it : itemArray) 
        {
            System.out.println(it);

            sum += qty;
        }
    avg = sum / 5;
    if(qty <= (avg - 10))
     {
     System.out.println("Quantity is 10 less than the average quantity");
     }
    }
    }
    class Item implements Comparable {
    private int qty;
    private String sku,code;

    public Item(String code, String sku,int qty) {
        this.code = code;
        this.sku = sku;
        this.qty = qty;
    }

    public String getCode() {
            return code;
    }
    public String getSku() {
        return sku;
    }
    public int getQty() {
        return qty;
    }


    public int compareTo(Object o) {
        Item i = (Item) o;
        return (this.sku).compareTo(i.sku);
    }
}

I tried declaring a int variable count and passed it to the array but I am getting ArrayIndexOutOfBoundsException

int count = 0;
Item[] itemArray = new Item[count];

One more issue I am facing is I am calculating the sum inside the loop,but I am not getting the desired results

My input file

PAP ENT 82
WAR HOT 79
TIM JUT 92
BON BLA 76
BON MAG 45

The sum of the quantity goes to 374,but in my code it's calculated to 225 and I am unable to figure out where I am doing wrong.

Any help is appreciated.

Thanks!!

Upvotes: 0

Views: 40

Answers (1)

jrowe08
jrowe08

Reputation: 391

Instead of adding these into a fixed array add them into a java list. Once you have filled the list up use listClass.size() to retrieve the size of the list.

Here's some implementation:

       List<Item> itemList = new List<Item>();
       while (aScanner.hasNext()) 
       {
            String code = aScanner.next();
            String sku = aScanner.next();
            double qty = aScanner.nextDouble();

        itemList.add(new Item(code, sku, qty));
        System.out.println(itemList.get(i));
    i++;

    }

Now use itemList.size() anywhere to retrieve how many entries. You are implementing arrays wrong btw. When you are getting the array out of bounds error is due to the fact that you are initializing the array with 0 positions.

Your sum is incorrect due to not getting the correct item value:

for (Item it : itemArray) 
    {
        System.out.println(it);
        //You must get the iterated item quantity (I assume you don't have setters and                                     
        //getters
         sum += it.qty;
    }

Upvotes: 1

Related Questions