-1

I am currently stuck on a java question where i have no idea how to proceed.. really appreciate any help!

The question is:

Write a java method, the method receives an integer number indicating the number of accounts, the array that contains the login names and the corresponding passwords and index of the account to be removed. It deletes the name and password of the account if removeindex is within the acceptable range. The method returns the updated number of accounts.

removeIndex: the index of the account to be removed

*Assuming arraysize is already created at other methods.

 public static int removeAccount(int count, String[] nameArr, String[] paswordArr, int removeIndex){

    for(int i=0; i<count;i++){
        String target = nameArr[removeIndex];
        if(target != null){
            // what should i do here?

        }else{
           System.out.println("id does not exist");

       }

    }
    return count;
}
  • 3
    so what are you stuck at? – Lrrr May 09 '15 at 07:23
  • What do you want to do ? – Paramvir Singh Karwal May 09 '15 at 07:25
  • You cannot modify the size of an array once it is created; the best you can do is set the matching values to null here. – fge May 09 '15 at 07:28
  • i cant seem to figure out how to make the method remove target from the array – user3003490 May 09 '15 at 07:28
  • You should avoid using `String` for passwords. Even though you are passing a mutable array of `String` objects to your method, the `Strings` inside the array are still immutable and will be available in the `String` literal pool. http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords – Chetan Kinger May 09 '15 at 07:30
  • Hi chetan, Appreciate for the note.. but it doesn't really solve my question.. – user3003490 May 09 '15 at 07:33
  • using `Collection` would be benificial here – singhakash May 09 '15 at 07:36
  • why do you subtract 1 from `count` in the for loop? And "deleting" the the value in an array can be done like @fge said with setting the matching values to `null` – Sebastian Walla May 09 '15 at 07:36
  • First -> Check if removeIndex is smaler than count, Second -> you only need to count-- if the name isnt allready deleated, Third -> try nameArr[removeIndex] = null to set the cooresponding field in the array to null, And Last -> is this homework? if so you should read up on Collections – Dude May 09 '15 at 07:51

2 Answers2

2

First, you can't "remove" anything from an array; once it is created, its size is fixed. Therefore it is assumed here that what you want is to set the matching value to null if it isn't already.

It is also assumed that the count is within bounds...

Code:

if (removeIndex >= nameArr.length)
    return count;

if (nameArr[removeIndex] == null)
    return count;

nameArr[removeIndex] = null;
passwordArr[removeIndex] = null;

return count - 1;

Now, the question is imprecise. Say you have an array with elements:

[ x, y, z ]

and the removal index is 1. What do you want the contents of the array to be after the operation? Is that [ x, null, z ] or [ x, z, null]? The code above assumes the former.

fge
  • 119,121
  • 33
  • 254
  • 329
  • do we need to shift element also?? as i think after removal you should compact the array – Prashant May 09 '15 at 07:57
  • @Prashant but you _can't_ "compact" the array... Java is pass by value! While you could modify the code to compact it within the method itself, it won't solve the problem that the caller cannot see the modifications anyway – fge May 09 '15 at 08:12
  • here compaction means all the null value should be in last. not null values in front array. may be i used wrong term here. – Prashant May 09 '15 at 08:14
  • 1
    @Prashant I think you may be right if the idea is to create a resizable array using a given array. In that case compacting should be used. We don't know if the indices need to be shifted but I agree that it is likely. The only way to know is to see more of the assignment. Blip, could you *update the question* with this information? – Maarten Bodewes May 09 '15 at 08:20
  • @Prashant Compacting is the perfect term for this, as far as I know. – Maarten Bodewes May 09 '15 at 08:27
  • @MaartenBodewes i am agree with you because that is the best way of removal. – Prashant May 09 '15 at 08:29
  • Sorry, the question was by user3003490, not Blib. My mistake. – Maarten Bodewes May 09 '15 at 08:35
  • Well, yeah, as to compaction, more precisions from the OP is indeed needed. – fge May 09 '15 at 08:36
  • @MaartenBodewes and fge you both are correct in pointing out the problem with my answer I feel you answer address the problem correctly and so I removed my answer as it is quite misleading to new users of Java. – Blip May 09 '15 at 09:39
0

Tested Code

Try this code. I've used an ArrayList to store accounts information.

public class Test {
public static ArrayList accounts=new ArrayList();
public static Scanner scan;
public static void main(String args[]){
    System.out.println("Enter number of accounts to add:");
    scan=new Scanner(System.in);
    int number=scan.nextInt();

    addAccount(number);

    System.out.println("Enter index(account no.) to remove account:\n(e.g. 1,2,3 etc)");
    int removeIndex=scan.nextInt();

    number=removeAccount(number,removeIndex);
    System.out.println("Number of accounts: "+number);
}
public static void addAccount(int n){
    scan=new Scanner(System.in);
    for(int i=0;i<n;i++){
        System.out.println("Enter Username for account "+(i+1)+":");
        accounts.add(scan.nextLine());
        System.out.println("Enter Password for account "+(i+1)+":");
        accounts.add(scan.nextLine());
    }
    System.out.println("Accounts added: "+accounts);
}
public static int removeAccount(int n, int index){
    String target = (String) accounts.get((index*2)-2);
    if(accounts.contains(target)){
        accounts.remove((index*2)-2);
        accounts.remove((index*2)-2);
    }
    System.out.println("After Deletion: "+accounts);
    return n-1;
}

}


How it works:

  1. It takes no. of accounts to be added.
  2. Adds username and password specified no. of accounts into ArrayList.
  3. Asks for account no. to be deleted.
  4. Deletes the record of given account no.
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
Muhammad Shuja
  • 642
  • 5
  • 18
  • Using `ArrayList` will work, but it seems to me that that is not the goal of the assignment in the question. – Maarten Bodewes May 09 '15 at 08:40
  • I've formatted your answer a bit. Hit the [edited xxx ago](http://stackoverflow.com/posts/30137815/revisions) to see the changes. Removed downvote as I didn't have any cofee yet. – Maarten Bodewes May 09 '15 at 08:44