0

I am trying to read the input from a text file using scanner class and pass into an array. I know how to read the input using scanner class. The only problem I am facing here is I am unable to pass into the array.

public class ReadItemData {

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

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

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

                System.out.println(code + " " + sku + " " + qty);

        }
    }

The above code works without the array concept. I want to extend the same concept to read the above data into a array of size 100. Any suggestions would be helpful. My final aim is to sort the input which is in array by code,sku

This is how I used comparable interface for sorting. How can I extend this concept for arrays?

I used something like this for sorting(without the array concept)

   class Item implements Comparable {
    private int qty;
    private String sku,code;

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

    public int getQty() {
        return qty;
    }

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

    public int compareTo(Object o) {
        Item i = (Item) o;
        if (this.getQty() < i.getQty())
        {
            return -1;
        }
        if (this.getQty() > i.getQty())
        {
            return 1;

            }
        return 0;
    }
}

Thanks!!

Sandy
  • 55
  • 1
  • 1
  • 9
  • 1
    Had you ever declared an array? You will need a two-dimensional array of 3 "columns" and "100" rows. – Smutje Mar 05 '14 at 20:18

2 Answers2

0
    String[] array = new String[100];
    int currentIndex = 0;     
    while (aScanner.hasNext()) {
            String code = aScanner.next();
            String sku = aScanner.next();
            double qty = aScanner.nextDouble();

            array[currentIndex] = code;
            array[currentIndex++] = sku;
            array[currentIndex++] = ""+qty;
            currentIndex++;

    }
Solace
  • 2,161
  • 1
  • 16
  • 33
  • This is not a true answer, since every line has three columns `code, sku, qty`, That's why I used 2D array, see my answer. – Caffe Latte Mar 05 '14 at 20:26
  • Hello. I agree with you that a 2-d array is much more suited for this scenario. But I think this is school work and OP is just learning about arrays. His text file is called "src//chapter11//Items.txt". – Solace Mar 05 '14 at 20:28
  • Yes, that's true. However, 1D array not give a true result, your code only reads 33 lines, since it's 100 rows and every iteration you fill in 3 indexes, right? That's why we should use 2D array. – Caffe Latte Mar 05 '14 at 20:37
0

As mentioned in the comments, you can use 2D array of 100 rows and 3 columns like this:

Object[][] array = new Object[100][3];
    int i=0,j=0;
    while (aScanner.hasNext()) {
         String code = aScanner.next();
         String sku = aScanner.next();
         double qty = aScanner.nextDouble();
         array[i][j++] = code; // array of row i and columns j 
         array[i][j++] = sku;
         array[i][j]   = qty;
         i++; // increment i since it's for rows
         j=0;//reset j because it's for columns

    } 
Caffe Latte
  • 1,693
  • 1
  • 14
  • 32
  • I was able to get the input using array from the above concept. Any suggestions on how I need to sort the i/p(sort by code,sku) which I got using Comparable interface? – Sandy Mar 05 '14 at 20:34
  • There are lot of approaches for the sort, I found this link [**java Arrays.sort 2d array**](http://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array) – Caffe Latte Mar 05 '14 at 20:39