0

I've got a problem here that's been giving me some real trouble and I really cant even get an idea of what to do. here's the assignment and my code so far.

Create a system using an ArrayList which stores and manipulates names. Using standard input constantly prompt user for the following ..

Enter command or quit: (if they enter quit -- quit program)

Commands:

  • add <name>: add the String <name> to ArrayList;
  • change <name> <newName>: change all items in ArrayList which have <name> to <newName>;
  • delete <name>: delete all items in Arraylist which are <name>;
  • print: print the ArrayList;
  • amount: display the amount of items in ArrayList.

System must work... and have proper error messages..

import java.util.*;

public class NameManipulation {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("enter a command, or quit!");
        ArrayList<String> names = new ArrayList<String>();
        String command = console.next();
        int size = names.size();
        for (String x = null; size; x++) {
            if (command == "add") {
                String assignment = console.next();
                names.add(assignment);
            }
            if (command == "change") {
                String newname = console.next();
                names.set(names.size, newname);
            }
            if (command == "delete") {
                String delete = console.next();
                if (delete == names)
                    ;
                names.remove();
            }
            if (command == "print") {
                System.out.println(names);
            }
            if (command == "amount") {
                amount = (names.size - 1);
                System.out.println(amount);
            }

            if (command == "quit") {
                System.out.println("You just quit!");
                break;
            } else
                System.out.println("command not found!");

            System.out.println(names);
        }
    }
}
3
  • 6
    Don't compare String values using ==. Use equals(). Commented Feb 27, 2014 at 19:54
  • 2
    possible duplicate of How do I compare strings in Java? Commented Feb 27, 2014 at 19:55
  • I formatted your code a little. You should always do it by yourself to see scope of variables and possible mistakes with unwanted ; (like in if (delete == names) case ). Commented Feb 27, 2014 at 20:13

4 Answers 4

3

Don't use == (in Java that tests reference equality); you want to test for object value equality (and I suggest case-insensitivity) so you want to use String.equalsIgnoreCase and you should probably use else if for the other tests - for one example,

if(command.equalsIgnoreCase("add")) {
  String assignment = console.next();
  names.add(assignment);
} else if // ...

Also, this is just wrong;

for (String x = null; size; x++) // null++ is going to give you a bad time.

I think you wanted

for (int x = 0; x < size; x++)
Sign up to request clarification or add additional context in comments.

2 Comments

Definitely use else if - otherwise you'll get the "command not found!" message for any command other than "quit"
@DavidWallace Or OP could add break; at the end of every if. I wouldn't do (or recommend) that, but OP used it in the "quit" check.
2

Just looking at this it seems like it has a lot of problems...

In the for loop you're initializing a String to null and then trying to increment it (x++). I don't think that's legal syntax. Also, your for loop condition is set to size, which will initially be equal to 0. I'd have to test it, but the 0 may evaluate to false, which means the loop would never execute.

You don't want a for loop anyway, probably a do-while loop that runs until the command is equal to "quit" do{}while(!command.equals("quit"));

You should be using .equals() instead of '==' as was mentioned by Elliot Frisch. Also ignoring case is good, and you should be using else ifs.

In the change command you should be parsing out two parameters -- both the name to replace and the new name, and then perform the replacement. Right now you have the first parameter as names.size, which I think will be outside the bounds of the list (names.size() - 1 should be the last element). Instead you should get the index of the name you're replacing.

Depending on Java's toString implementation of ArrayList it may print out names nicely or it might be something like "@ArrayList Object" - I think Java has a nice ArrayList toString method though so that may work.

On the print amount, you should be using names.size() instead of names.size() - 1 (because names.size() - 1 will give you one less item than what is actually in the list)

3 Comments

+1! This is (currently) the best answer here. I believe it covers all of the errors, not just one or two of them. It's possible that there are other errors too, but if you fix all the things listed in this post, you'll be well on the way to a working program.
i did everything you said and now in my code i am getting error messages saying "NameManipulation.java:20: error: size has private access in ArrayList names.set(names.size-1, newname);" what does this mean? what am i doing wrong
Change that to names.set(names.size() - 1, newname); - see the parentheses?
0

you could try and use a switch block that could stream line your control statements. As stated above learn when to use == and when to use the .equals(). One compares a reference (==) the other compares the memory location, the important thing to take away from this, is that a String is an object and when you create a new String, it compares the address rather than a value (forgive me if i am wrong).

switch(command){
case "add": names.add(assignment);
break;
case "change": ..... etc.

}

1 Comment

Worth noting that this applies to Java 7 and up, not to earlier versions.
0

If I were trying to accomplish this task I would use a List, not an ArrayList. I hope this method helps! This is C# code. I don't know the exact Java syntax but it seemed as if a lot of it was similar. I hope this methodology helps!

static void Main(string[] args)
    {
        string statement = "";
        bool executed_command_properly;
        while (statement != "quit")
        {
            executed_command_properly = false;
            statement = Console.ReadLine();
            string[] my_statement_elements = statement.Split(' ');
            string command = my_statement_elements[0];

            //could possibly use an array for inputs
            string input1 = my_statement_elements[1];
            string input2 = my_statement_elements[2];

            switch(command)
            {
                case "add":

                    // do stuff

                    executed_command_properly = true;

                    break;

                //other cases
            }
            if (executed_command_properly != true)
            {
                //error messages
            }  
        }
    }

1 Comment

Did you notice this was a Java question? Your code looks like C# to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.