I need help figuring out how to put the following logic into code:

I have a text file that I am reading from, I have been able to code so that it reads four seperate columns; now I need to make one of the columns read (into an array) the label while the other three become the input for my label.

For example on Day 1 I need to be able to display the hours I slept, the number of books I read and how many cups of water I drunk (these are not the exact parameters needed). So for each day I need to pick the corresponding elements for each activity (activities have been read into three different arrays).

Can some help me please. I have been trying really hard to figure it out.


First wrong thing you are doing is " Using array for this purpose ".
java does not allow variable-sized arrays so it would be a wastage of memory. Since your file size would not be fixed.

Instead of that you should you arraylist or vector.

I will give you an example using arraylist

Suppose this is your text file

Name Jack Williams Sam
Chemistry 10 9 8
Physics 11 12 13
Maths 14 15 16

To read this and store this in 4 arraylist named (name, chem , phy , maths) you can do like this

class FileRead {
 public static void main(String[] args) {
  try {

[INDENT]File f = new File("c://hello.txt");[/INDENT][INDENT]FileReader file = new FileReader(f);[/INDENT][INDENT]char[] c = new char[200];[/INDENT][INDENT]ArrayList<String> name = new ArrayList<String>();[/INDENT][INDENT]ArrayList<String> chem = new ArrayList<String>();[/INDENT][INDENT]ArrayList<String> phy = new ArrayList<String>();[/INDENT][INDENT]ArrayList<String> maths = new ArrayList<String>();[/INDENT][INDENT]int k = file.read(c);[/INDENT][INDENT]StringBuffer b = new StringBuffer();[/INDENT][INDENT]int counterNxArray = 0;[/INDENT][INDENT]for (int i = 0; i < c.length; i++) {[/INDENT][INDENT] int counter = 0;[INDENT] if (c[i] == ' ' || c[i] == '\n' || i == k) {[/INDENT][INDENT]  switch (counterNxArray) {[/INDENT][INDENT][INDENT]case 0:[/INDENT][INDENT]   name.add(b.toString());[/INDENT][INDENT]   System.out.println(b.toString());[/INDENT][INDENT]   break;[/INDENT][INDENT]  case 1:[/INDENT][INDENT]   chem.add(b.toString());[/INDENT][INDENT]   System.out.println(b.toString());[/INDENT][INDENT]   break;[/INDENT][INDENT]  case 2:[/INDENT][INDENT]   phy.add(b.toString());[/INDENT][INDENT]   System.out.println(b.toString());[/INDENT][INDENT]   break;[/INDENT][INDENT]  case 3:[/INDENT][INDENT]   maths.add(b.toString());[/INDENT][INDENT]   System.out.println(b.toString());[/INDENT][INDENT]   break;[/INDENT]}
     b.replace(0, b.length(), "");
     counter = counter + 1;
     if (i == k)
 if (c[i] == '\n') 
     counterNxArray = counterNxArray + 1;

   System.out.println("*** The array contains following values ****");
   for (int i = 0; i < name.size(); i++)

    System.out.println(name.get(i) + "  \t " + chem.get(i) + "\t "[INDENT]    + phy.get(i) + "\t " + maths.get(i) + "\t ");[/INDENT]

[/INDENT]} catch (FileNotFoundException e) {[INDENT]e.printStackTrace();[/INDENT]
} catch (Exception e) {

[INDENT][INDENT] e.printStackTrace();[/INDENT]

This article has been dead for over six months. Start a new discussion instead.