0

I have the following code in java, which takes in input from the user. It is basically a simple database system.

ArrayList<String> commands = new ArrayList<String>();
ArrayList<ArrayList<String>> blocks = new ArrayList<ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();

System.out.println("Enter the transaction commands.\n");

Scanner scan = new Scanner(System.in);

while(!(line = scan.nextLine()).toLowerCase().equals("end"))
{
    commands.add(line);
}

for(String com : commands)
{
    String split[] = com.split(" ");
    if(!split[0].toLowerCase().equals("get") && !split[0].toLowerCase().equals("numequalto") && !split[0].toLowerCase().equals("rollback") && !split[0].toLowerCase().equals("commit"))
    {
        if(split[0].toLowerCase().equals("begin"))
        {
            if(!list.isEmpty())
            {
                blocks.add(list);
                System.out.println(blocks.get(0));
                list.clear();
            }
            else
            {
                continue;
            }
        }
        else
        {
            list.add(com);
            continue;
        }
    }
}
System.out.println(blocks.get(0));

The input I give for this program is:

set a 10
set b 20
begin
get a
get b
end

While the expected output is:

[set a 10, set b 20]
[set a 10, set b 20]

I get the output as:

[set a 10, set b 20]
[]

The problem seems to be that the value of the ArrayList> blocks, seems to be overwritten. The last print statement prints the value as an empty ArrayList. I cannot find the exact source of error. Any help in finding out the error will be greatly appreciated.

1
  • 1
    Have you tried stepping through your code with the debugger? Commented Nov 13, 2013 at 7:18

3 Answers 3

3

I believe the following code is the culprit:

blocks.add(list);
System.out.println(blocks.get(0));
list.clear();

You add the list to blocks. Note, when you are adding your list object to blocks, you are not copying the list.

So when you clear the list, it clears the list object that is also referenced in the blocks list.

To avoid this you could just:

blocks.add(list);
System.out.println(blocks.get(0));
list = new ArrayList<String>();

This will create a new list object and leave the one in your blocks list untouched.

Sign up to request clarification or add additional context in comments.

Comments

3

Your second output is got from last line of System.out.println(blocks.get(0));. Have a look at your code, you add blocks.add(list); after a while you clear the list. As List is mutable, so blocks List is empty. So, your second output print nothing.

 blocks.add(list); //list has added here with values
 System.out.println(blocks.get(0));
 list.clear();  // list here without values.

3 Comments

Doesn't the line blocks.add(list); do that?
Yes. After addition, you clear list as well as block list. Because, List is mutable.
Thanks. I totally missed that one.! :)
1

This giving correct answer as you expected.

remove that list.clear() statement

import java.util.ArrayList;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        ArrayList<String> commands = new ArrayList<String>();
        ArrayList<ArrayList<String>> blocks = new ArrayList<ArrayList<String>>();
        ArrayList<String> list = new ArrayList<String>();

        System.out.println("Enter the transaction commands.\n");
        String line;
        Scanner scan = new Scanner(System.in);

        while (!(line = scan.nextLine()).toLowerCase().equals("end")) {
            commands.add(line);
        }

        for (String com : commands) {
            String split[] = com.split(" ");
            if (!split[0].toLowerCase().equals("get")
                    && !split[0].toLowerCase().equals("numequalto")
                    && !split[0].toLowerCase().equals("rollback")
                    && !split[0].toLowerCase().equals("commit")) {
                if (split[0].toLowerCase().equals("begin")) {
                    if (!list.isEmpty()) {
                        blocks.add(list);
                        System.out.println("list :" + blocks.get(0));
                        // list.clear();
                    } else {
                        continue;
                    }
                } else {
                    list.add(com);
                    continue;
                }
            }
        }
        System.out.println("output :" + blocks.get(0));
    }

}

Answer i got is

list :[set a 10, set b 20]
output :[set a 10, set b 20]

Comments

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.