سؤال

I am reading from a comma seperated file and splitting the contents into an array. I am doing this in a while loop so I would want my arrays to have have a different name dynamically because the last line in the file end up overwriting my array content. Below is a fragment of my the code involved.

  TextView txt1;
  TextView txt2;

  Scanner in = new Scanner(result);
  in.nextLine(); //skip first line

  while(in.hasNextLine()){
    String line = in.nextLine();
    String []dataset = line.split(",");//store values in array

    txt1.setText(dataset[4]);//Should be 5th element of first line
    txt2.setText(dataset[4]);//Should be 5th element of second line
  }

As you can see from the above code I want to set the value of txt1 to a val in the first line of my file and similarly to the txt2. I read HashMaps or Maps or ArrayList would help me but I am not sure how to achieve this.

هل كانت مفيدة؟

المحلول

If I understand what you want to do correctly it is you want txt1 to equal the 5th element in the array on the 1st line and txt2 to have the text of the 5th element on the 2ed line (your comment says 3ed element but the code is pulling the 5th).

This can be achieved with conditionals and a counter.

Scanner in = new Scanner(result);
   in.nextLine(); //skip first line
   int count = 0;
   while(in.hasNextLine()){

      String line = in.nextLine();
      String []dataset = line.split(",");//store values in array
      if (count == 0){
          txt1.setText(dataset[4]);//Should be 3 element of first line
      }else if (count ==1){
          txt2.setText(dataset[4]);//Should be 3 element of second line
      }
    count++;
   }
  }

EDIT:

So now that I know you want an array of arrays the set up is easy. The outer array should be mutable if you dont know the amount of data you are processing before hand.

ArrayList<String[]> dataSet = new ArrayList(10000)//number should be a guess at the amount of data
Scanner in = new Scanner(result);
   in.nextLine(); //skip first line
   while(in.hasNextLine()){

      String line = in.nextLine();
      String []dataset = line.split(",");//store values in array
      dataSet.add(dataset);
   }
   txt1.setText(dataSet.get(0)[4]);
   txt2.setText(dataSet.get(1)[4]);
  }

if you wanted to you could also create these text labels and store handels to them in a similar manner and set the text as you go.

نصائح أخرى

You would need to add your dataset array to a list. So, before your while loop:

List datalist = new ArrayList();

Then, after:

String []dataset = line.split(",");//store values in array

Add:

datalist.add(dataset);

Was going to comment on Michael's post, but not enough room! If you were expecting an arbitrary number of lines from your input, and want to process them outside of the loop, you could combine Michael's approach with mine and end up with:

   Scanner in = new Scanner(result);
   in.nextLine(); //skip first line

   List txtList = new ArrayList(); //create list

   while(in.hasNextLine()) {
     String line = in.nextLine();
     String[] dataset = line.split(","); //store values in array

     Text txt1 = new Text(); //assuming txt1 is an instance of "Text"
     txt1.setText(dataset[4]); //Should be 5th element of first line

     txtList.add(txt1); //add 5th element of each line to list
   }

   /*
    * txtList now contains instance of Text containing the 5th value from each
    * line of input
    */
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top