0

I need to enter a String input, format it, and display the output in a certain format. I'm supposed to use the Run > Run Configuration > Arguments > Program Argument and I entered in any string (ie. "test") in the Program arguments section. When I try to run the java application, I keep getting an error message in the Console window. How do I use the Commandline arguments? What should I enter in the program arguments? Any help with this, would be greatly appreciated. Thanks.

Below is the error message that I get in the Console window:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at a00752124.data.InventoryReader.read(InventoryReader.java:26)
    at a00752124.Lab2.<init>(Lab2.java:38)
    at a00752124.Lab2.main(Lab2.java:28)

Below is my sourcecode for the main class:

public class Lab2 {

    /**
     * Main method of Lab2 class
     * 
     * @param args
     */
    public static void main(String[] args) {
        new Lab2(args[0]);
    }

    /**
     * Constructor for Lab2 class
     * 
     * @param itemCount
     */
    public Lab2(String itemCount) {

        Item[] items = InventoryReader.read(itemCount);

        System.out.println(Arrays.toString(items));

        InventoryReport.display(items);
        }

    }

Here's the source code for my InventoryReader's read method:

public static Item[] read(String input) {
        String[] rows = input.split(":");
        Item[] items = new Item[rows.length];

        int i = 0;
        for (String row : rows) {

            String[] element = row.split("\\|");
            items[i] = new Item(element[0], element[1], Integer.valueOf(element[2]), Float.valueOf(element[3]));
            i++;


        }

        return items;
    }
4
  • 2
    The exception occurred in InventoryReader.read() method. Can you show us that method? The exactl location is InventoryReader.java line 26. Commented Apr 20, 2014 at 18:38
  • Hi Thank you for your input. I added the source code for my InventoryReader's read method above. Commented Apr 20, 2014 at 18:43
  • It seems u need to give input with : as row delimiter and | as column delimiter with 4 elements in row Commented Apr 20, 2014 at 18:55
  • Thanks I tried that but it's still giving errors. I'm still getting an exception thrown from the line "items[i] = new Item(element[0], element[1], Integer.valueOf(element[2]), Float.valueOf(element[3]));" Commented Apr 20, 2014 at 20:42

1 Answer 1

2

You have:

String row = "";

then:

String[] element = row.split("\\|");

What is the size of element after you split an empty string?

Right after you assume that element has at least 4 items.

items[i] = new Item(element[0], element[1], Double.valueOf(element[2]), Double.valueOf(element[3]));

Clear enough?

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

2 Comments

Thanks for your help. I fixed the InventoryReader's read method but I'm still getting errors. It's throwing an exception on the line "items[i] = new Item(element[0], element[1], Integer.valueOf(element[2]), Float.valueOf(element[3]));" Any ideas?
I believe it's still the same error. To test your assumption about the size of your element array place System.out.println(element.length); before the problematic statement or (even better) use a debugger and put a breakpoint on this line.

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.