2

I have several commands created to ask a user to add numbers to an array. I now need to be able to have the user select a specific element in the array and place an * on the selected element when printed out. I have the program separated into two classes: one class for storing and managing the array and the other handles user input and output.

For example here is the code in the class that handles input/output:

else if (cmd.equals("add"))
        {
            // add x command
            int x = input.nextInt();
            list.add(x);
            list.print();
        }

and here is the part in the class that deals with the array:

public void add(int x)
{
    // Expand the list capacity if necessary
    if (count >= list.length)
    {
        // Allocate a new longer list
        int[] newList = new int[list.length + 5];

        // Copy existing numbers to new list
        for (int i = 0; i < list.length; i++)
        {
            newList[i] = list[i];
        }

        // Reassign the list to be the new one
        list = newList;
    }

    // Add x to the end of the list
    list[count] = x;
    count++;
}

This is the command created to add entries to the array(and enlarge the array if necessary), now I just need help creating a command to allow a user to select a specific entry in the array and place an * in front of it.

2
  • 3
    Since it's an int array, you're not going to be able to add a "" in front of anything. You could keep track of the selected index and when required to print it, insert the "" into the output Commented Feb 25, 2015 at 3:47
  • Good to know, im mostly just at a loss on how to create the command to select a specific index. Commented Feb 25, 2015 at 3:54

2 Answers 2

1

Prompt the user, then read in and store the selected index in a variable. Do a check in your printing for-loop.

Scanner kb = new Scanner(System.in);
System.out.println("Enter the index of an element:");
int selectedElement = kb.nextInt();

Then when printing..

for (int i = 0; i < list.length; i++) { if(i == selectedElement) // and then print out the * in front of it }

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

Comments

0

Well, you could always print out the array, using the index to display which value you are displaying. You could also display it as index + 1, so it's more user-friendly (that way, the user isn't wondering why the numbering starts at 0). For example (this would be what the output might look like):

1) First value.

2) Second value.

3) Third value.

Please enter the number that you would like to print out, then press enter.

Once the user enters a value, you can subtract 1 from it to get the index of the array, and then print it out appropriately.

I hope I understood your question correctly, and I hope this helps.

2 Comments

I tried to make sense out your answer but it is not clear what you try to convey to me.
It would just be a matter of printing the array out to the user, allowing the user to enter an index that they would like to print, and printing out the array as a list with a * next to the chosen index.

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.