Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: problem with deleting from an array

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem with deleting from an array

    Hi, I have a problem with a method from my work, which I can't seem to find.
    So here's the problem: - I have a method called deleteGroup(String name) and, as the name says it's supposed to delete a group from a given array, if the array isn't empty and if the group has the same name has the given one.
    Here's the code:

    public void deleteGroup(String nameG) {
    if (gCounter != 0) {
    if (groupExists(groups[0].getName(), nameG)) {
    this.groups = new Cgrupo[1];
    gCounter = 0;
    }
    throw new NonExistantGroupException();
    }
    throw new NonExistantGroupException();
    }

    (yes it's a one value array, since this is just the 1st phase of the work, next ones will be using maps and list, etc)

    well here's the groupExists method, since it appears to block there and throw the NonExistantGroupException:

    private boolean groupExists(String name1, String name2) {
    return name1.equalsIgnoreCase(name2);

    }

    So this is my problem, I'm pretty sure it must be an incredible stupid mistake. I hope any of you can point it out, because I have lost all hope in solving this.

    Kind regards and thanks for help.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with deleting from an array

    Point out what? You haven't provided enough details of what your problem is.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with deleting from an array

    this if: if (groupExists(groups[0].getName(), nameG)), appears to be the problem, but I'm not quite sure what is wrong in there.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with deleting from an array

    Neither are we unless you can provide more details. Do you say to your mechanic "my car doesn't work" and expect him to know what the problem is?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem with deleting from an array

    The only thing I can do is put here all the pieces of code that affect or are affected by this method, because if I knew what the problem was I wouldn't be asking, I just know it doesn't delete the array as I wanted.

    Constructor:

    public class CsocialNet implements IsocialNet {

    private Iutilizador[] users;
    private Igrupo[] groups;
    private int uCounter;
    private int gCounter;

    public CsocialNet() {
    this.users = new Cutilizador[1];
    this.groups = new Cgrupo[1];
    this.uCounter = 0;
    this.gCounter = 0;
    }

    @Override
    public void deleteGroup(String nameG) {
    if (gCounter != 0) {
    if (groupExists(groups[0].getName(), nameG)) {
    this.groups = new Cgrupo[1];
    gCounter = 0;
    }
    throw new NonExistantGroupException();
    }
    throw new NonExistantGroupException();
    }

    private boolean groupExists(String name1, String name2) {
    return name1.equalsIgnoreCase(name2);

    }

    And here's the class that creates the object stored in the array:


    package gestorAmigos;

    public class Cgrupo implements Igrupo {

    private String name;
    private String description;

    // mudar para fase 2
    private String[] members;
    private int counter;

    public Cgrupo(String name, String description) {

    this.name = name;
    this.description = description;
    this.counter = 0;
    this.members = new String[1];
    }

    @Override
    public String getName() {
    return this.name;
    }

    @Override
    public String getDescription() {
    return this.description;
    }

    @Override
    public void addMember(String login) {
    if (counter == 0) {
    members[counter++] = login;
    } else {
    counter = 0;
    addMember(login);
    }
    }

    // mudar proxima fases.
    public String getMembers() {
    return members[0];
    }

    public String toString() {
    return this.name + "\n" + this.description + "\n";
    }

    // mudar para outras fases
    public boolean seekMember(String login) {
    return members[0].equalsIgnoreCase(login);

    }

    // mudar para outras fases
    @Override
    public void kickMember(String login) {
    if (members[counter].equalsIgnoreCase(login)) {
    this.counter = 0;
    this.members = new String[1];
    }
    }

    public boolean hasMembers() {
    if (counter == 0) {
    return false;
    }
    return true;
    }

    }

    EDIT: Here's the main call for this method, though I'm pretty sure it's not the problem:

    private static void deleteGroup(Scanner in, IsocialNet sc) {

    String name = "";

    try {

    name = in.next();
    sc.deleteGroup(name);
    System.out.println(MESSAGES.GROUPDELSUCC + "\n");

    } catch (NonExistantGroupException e) {
    System.out.println(MESSAGES.GHOSTGROUP + "\n");
    }
    }

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: problem with deleting from an array

    You may not know what is causing your problem but you can explain/describe it.

    Do you get errors when you compile or run your code? Then post the exact message here.
    Does the program not behave as expected? Then post what your input was, what in correct output or behaviour you got and what you expected to happen instead.

    The more details you provide the easy it is for us to help you.
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] database connection problem adding,deleting,updating,selecting :(
    By jabaman in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 6th, 2012, 01:05 PM
  2. finding and deleting from an array?
    By 93tomh in forum Java Theory & Questions
    Replies: 19
    Last Post: August 13th, 2012, 09:39 AM
  3. referential integrity in deleting problem!!
    By yanikapausini:) in forum Object Oriented Programming
    Replies: 1
    Last Post: January 24th, 2012, 04:59 PM
  4. [Help] Problem with Class arrays, adding/editing and deleting
    By Grant_Searle in forum Object Oriented Programming
    Replies: 7
    Last Post: December 16th, 2011, 10:10 AM
  5. Deleting Array Records
    By rainexel in forum Collections and Generics
    Replies: 3
    Last Post: September 26th, 2011, 11:40 AM