0

For a project in school (intro to java), we have to make a program using arrays. I decided to make a login program that stores logins. It works perfectly, except when deleting items. This is my code

    public void delete() throws FileNotFoundException{
    int p;
    c.clear();
    c.print("Please enter a website to delete it's login info: ");
    String delete_name = c.readLine();
    Boolean found = false;

    // Search for the search key, and display the matching elements
    c.println("Searching for " + delete_name + "...");
    for (int i = 0; i < pass.length; i++)
        if (pass[i][0].equals(delete_name)) {
            c.println("Deleting login for " + pass[i][0]);


            String new_array[][] = new String[pass.length - 1][3];
            //remove an element
            for (int w = 0; i < new_array.length; i++)
                for (int j = 0; j <= 2; j++) {
                    p = w;
                    if (i >= p) {
                        new_array[w][j] = pass[w + 1][j];

                    } else {
                        new_array[w][j] = pass[w][j];


                    }
                }

            found = true;
            pass = new_array;
        }

    if (found == false) {
        c.println("No luck! " + delete_name + " wasn't found, please try again.");
        delete();
    }
        fileWriter();

}

When it writes to the file, anything after the part that should have been deleted gets changed to "null".

Sorry if the format is awful, I'm just starting with java :) Any help is greatly appreciated, thanks!

  • 2
    You cannot set one array (pass) equal to another (new_array). See this thread: http://stackoverflow.com/questions/7882074/how-do-you-set-one-arrays-values-to-another-arrays-values-in-java – Arman May 22 '16 at 22:59
  • Btw I'm referring to the line "pass = new_array". Instead try "pass = new_array.clone();" – Arman May 22 '16 at 23:01
  • @Arman I do it in my code other places which works fine. It was actually in the course `// Step 3: assign the new array to the old array grocery_list = new_array;` – Bryce Thompson May 22 '16 at 23:03
  • That is not correct though, is this in a book? For example, having one array (arr1) and another (arr2), this "arr1 = arr2" will NOT copy arr2 into arr1. Instead it makes the two arrays refer to the same thing. Read the accepted answer in this thread : http://stackoverflow.com/questions/5617016/how-do-i-copy-a-2-dimensional-array-in-java – Arman May 22 '16 at 23:07
  • @Arman I changed it to .clone() but it still overwrites anything following the deleted section with "null" Also it is on the website in sample code where it shows that – Bryce Thompson May 22 '16 at 23:07

1 Answers1

0

When deleting a row of a 2D array in Java, you can use this shortcut (no for-loops needed)

List<String[]> tempArr = new ArrayList<String[]>(Arrays.asList(pass));
//Remove row at index of "delete_name":
for(int i = 0; i < pass.length; i++){
  if(pass[i][0].equals(delete_name)){
    tempArr.remove(i);
  }
}
String[][] new_array = tempArr.toArray(new String[][]{});

However, this solution only works if you are only deleting one object in the List. I would suggest looking into "iterators" to make this solution better.

EDIT:

Here is an example with an iterator

    String[][] pass = new String[][]{{"Name","data1","data2"}};
    List<String[]> tempArr = new ArrayList<String[]>(Arrays.asList(pass));
    for (Iterator<String[]> iterator = tempArr.iterator(); iterator.hasNext();) {
        String id = iterator.next()[0];
        if (id.equals(delete_name)){
            iterator.remove();
        }
    }
Arman
  • 655
  • 2
  • 7
  • 23
  • Thanks, I used the first one since I need to get this project done ASAP. I will definitely look into iterators though. Got it working with a few modifications. Thank you! – Bryce Thompson May 23 '16 at 03:05