0

I have to read a text file into an integer array then take an array that already exist and populate it using the newly created array.

Now I have read the text file into an Integer array but can't figure out how to change an existing array so that it is identical to the newly created array.

Below is my code:

public static void loadGrades(int list[]) {
File f = null;
Scanner scan = null;
try{
   f = new File("Proj5Data.txt");
   scan = new Scanner(f);
}
catch(Exception e){
   System.exit(0);
}

ArrayList<Integer> grades = new ArrayList<Integer>();
//Assuming you know all your data on the file are ints
while(scan.hasNext())
   grades.add(scan.nextInt());

System.out.println(grades);
for (int i = 0; i < list.length; i++)
    list[i] = 1;
}
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
SynGaren NA
  • 21
  • 1
  • 4
  • What do you understand by "match"? Do you just want to check if the grades are the same? Or do you want to resize it to the size of the new array? Do you want to copy all the values from the new array to the existing one? – Alexandru Godri Mar 29 '15 at 00:14
  • I need to copy all the values from the new array to the existing one. – SynGaren NA Mar 29 '15 at 00:20
  • Please check this post for the solution: http://stackoverflow.com/questions/5785745/make-copy-of-array-java – Alexandru Godri Mar 29 '15 at 00:27
  • [`System.arraycopy`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29) – m0skit0 Mar 29 '15 at 00:33
  • Thank you for all the answers so far. I've have tried both methods. and they run and compile with no errors. But when I print the array to test to see if it worked. It just prints [I@5c647e05 – SynGaren NA Mar 29 '15 at 01:23

2 Answers2

0

Your problem (or more like bad practice) is that you are mixing the idea of list and array - which are clearly not the same.

Just to explain, you are using grades as an ArrayList, not an Array. What you can do whilst you are scanning for the integers in your while loop:

int i = 0; // initialise your array index earlier (assuming you have declared your "list" array somewhere beforehand
while(scan.hasNext())
{
int p = scan.nextInt();
grades.add(p);
list[i++]=p; 
}

Again, I am assuming from your code snippet that you have declared your list array somewhere before.

By simply improving upon your code snippet:

List<Integer> grades = new ArrayList<Integer>(); // List object with ArrayList impl.
//Assuming you know all your data on the file are ints
while(scan.hasNext())
   grades.add(scan.nextInt());

System.out.println(grades);
Integer[] n_array = new Integer[grades.size()]; // create the new array
grades.toArray(n_array); // Fill it up

BTW....this toArray() thingy works if your new array type is Integer i.e. reference types. For primitives like int you need to use traditional approach e.g.

int i = 0;
for (int x: grades)
    n_array[i++] = x;
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Thank you for all the answers so far. I've have tried both methods. and they run and compile with no errors. But when I print the array to test to see if it worked. It just prints [I@5c647e05 – SynGaren NA Mar 29 '15 at 01:23
0

With the assumption that your existing array has enough space to allocate all the integers read from the file, this solution will work.

 private void loadGrades(int[] list) throws IOException{

    ArrayList<Integer> grades = new ArrayList<Integer>();

    Scanner scanner = new Scanner(new File("Proj5Data.txt"));

    while(scanner.hasNextInt()){
        grades.add(scanner.nextInt());
    }

    for (int i : list) {
        list[i] = grades.get(i);
    }
}
ravindrab
  • 2,712
  • 5
  • 27
  • 37
  • Thank you for all the answers so far. I've have tried both methods. and they run and compile with no errors. But when I print the array to test to see if it worked. It just prints [I@5c647e05 – SynGaren NA Mar 29 '15 at 01:23
  • @SynGaren NA to see the contents of array use `System.out.println(Arrays.toString(list))`. What you are seeing is just the object reference of the array, not the contents. – ravindrab Mar 29 '15 at 01:33
  • Ahh yes, I picked up on that a few minutes after I posted that last comment. But For some reason it is only grabbing the first integer out of 15. Please excuse my ignorance as I'm very new to coding. – SynGaren NA Mar 29 '15 at 01:36
  • does it read all the entries in the file? Make sure `grades` get populated with all the entries in the file first before copying to the other array. – ravindrab Mar 29 '15 at 01:58