Hello, I'm currently working on a small database project with various search algorithms. Right now I seem to be a little stumped. Basically what I need to is to read a huge file of strings, such as these 2 lines:

Abram,Smith,70,M,Nursing Unit,324b
Ace,Lewis,7,M,Intensive Care Unit,324c

I then need to delimit these into an array. From this I need to create a 2D array, so after the delimit(on comma) I would have an array that looks like this:

[Abram, Smith, 70, M, Nursing Unit, 324b]

And the end game being an array of arrays of each line so I can access each dept etc by merely using array[x][4]

[[Abram, Smith, 70, M, Nursing Unit, 324b],[Ace,Lewis,7,M,Intensive Care Unit,324c]]

I then need to add this to the 2D array and then use a bubble sort to sort by department (Nursing Unit, Intensive Care, etc). The above list is always separated by a carriage return with commas in between first name, last name, and so on, and the indexes for the subjects are the same. i.e. Department is always at index 4. I'm having trouble taking all of the delimited single arrays after reading the file and adding them as they are read into a 2D array, heres my code:

){

    File medicalfile= new File("medical_database.txt");
    String getinput;
    String delimiter[];
    int count=0;
    int x=0, y=0;
    Scanner scan=null;
    patients=new String[999][6];
    try
    {
      scan=new Scanner(medicalfile);
    }
    catch(Exception e){}
    while(scan.hasNext())
    {
      getinput=scan.nextLine(); 
      delimiter=getinput.split(",");
      System.out.println(Arrays.toString(delimiter));
      for(int i=0;i<999;i++)
      {
        for(int j=0;j<delimiter.length;j++)
        { 
          patients[i][j]=delimiter[j];
          
        }
      }
    }
    
    
  
  }

Now I know that the last for loop will leave me with the last entry scanned covering the entire 2D array, but I've just exhausted a lot of options, any suggestions? I've done so much debugging so I know almost how everything prints out but the reading once per line is really confusing me

Edited 5 Years Ago by Procx: n/a

If you want to keep it that way, you need to get rid of the for(int i=0... loop. Instead of that loop, declare a variable type int and initial it to 0 outside the while-loop ( i.e. int cnt=0; ). Then use it in place of "i" variable in the while-loop and remember to increase it by 1 at the end of the loop. Then you should see that the number of variable you created outside the loop is the number of rows used in the array.

int cnt=0;
while(scan.hasNext(){
  for (int j=0; j<delimiter.length; j++) {
    patients[cnt][j] = ...
  }
  cnt++;
}

PS: There could be a problem if your input has different delimiter length. However, if they are all the same length, it is OK to do so in this case.

Edited 5 Years Ago by Taywin: n/a

If you want to keep it that way, you need to get rid of the for(int i=0... loop. Instead of that loop, declare a variable type int and initial it to 0 outside the while-loop ( i.e. int cnt=0; ). Then use it in place of "i" variable in the while-loop and remember to increase it by 1 at the end of the loop. Then you should see that the number of variable you created outside the loop is the number of rows used in the array.

int cnt=0;
while(scan.hasNext(){
  for (int j=0; j<delimiter.length; j++) {
    patients[cnt][j] = ...
  }
  cnt++;
}

PS: There could be a problem if your input has different delimiter length. However, if they are all the same length, it is OK to do so in this case.

I might be mistaken but something like that would then not give you matching lines:

cnt=0 j=0:
first scan:
patients[0][0]=delimiter[0]
second scan:
patients[1][1]=delimiter[1]
third scan:
patients[2][2]=delimiter[2]
fourth scan:
patients[3][3]=delimiter[3]
fifth scan:
patients[4][4]=delimiter[4]

So scanning a line such as this:


Ace,Lewis,7,M,Intensive Care Unit,324c

patients array = [[Ace],[null, Lewis],[null,null,7]] and so forth but I actually need

[[Ace, Lewis, 7, M, Intensive Care Unit, 324c],[nextline array]]

Am I incorrect on this assumption? I also get an out of bounds error, which makes sense if I'm reading the code correctly. Heres what I updated too:

public void sortByDepartment(){

    File medicalfile= new File("medical_database.txt");
    String getinput;
    String delimiter[];
    int count=0;
    int x=0, y=0;
    Scanner scan=null;
    patients=new String[999][6];
    try
    {
      scan=new Scanner(medicalfile);
    }
    catch(Exception e){}
    int cnt=0;
    while(scan.hasNext())
    {
      getinput=scan.nextLine(); 
      delimiter=getinput.split(",");
        for(int j=0;j<delimiter.length;j++)
        { 
          patients[cnt][j]=delimiter[j];
          
        }
        cnt++;
      }
    System.out.println(Arrays.toString(patients));
    }

Edited 5 Years Ago by Procx: n/a

I might be mistaken but something like that would then not give you matching lines:

cnt=0 j=0:
first scan:
patients[0][0]=delimiter[0]
second scan:
patients[1][1]=delimiter[1]
third scan:
patients[2][2]=delimiter[2]
fourth scan:
patients[3][3]=delimiter[3]
fifth scan:
patients[4][4]=delimiter[4]

So scanning a line such as this:


Ace,Lewis,7,M,Intensive Care Unit,324c

patients array = [[Ace],[null, Lewis],[null,null,7]] and so forth but I actually need

[[Ace, Lewis, 7, M, Intensive Care Unit, 324c],[nextline array]]

Am I incorrect on this assumption? I also get an out of bounds error, which makes sense if I'm reading the code correctly. Heres what I updated too:

public void sortByDepartment(){

    File medicalfile= new File("medical_database.txt");
    String getinput;
    String delimiter[];
    int count=0;
    int x=0, y=0;
    Scanner scan=null;
    patients=new String[999][6];
    try
    {
      scan=new Scanner(medicalfile);
    }
    catch(Exception e){}
    int cnt=0;
    while(scan.hasNext())
    {
      getinput=scan.nextLine(); 
      delimiter=getinput.split(",");
        for(int j=0;j<delimiter.length;j++)
        { 
          patients[cnt][j]=delimiter[j];
          
        }
        cnt++;
      }
    System.out.println(Arrays.toString(patients));
    }

Hah I was reading it wrong, thanks!

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