Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8708799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:13:17+00:00 2026-06-13T04:13:17+00:00

This is for class homework. She is making us use array and array list

  • 0

This is for class homework. She is making us use array and array list in two parts. Basically she is showing us how using arraylist is alot easier than arrays.

I am having alot of trouble getting the array part to work.

  1. Create a class called CustomerLister1 with a main method that instantiates an array of String objects called customerName. The array
    should have room for six String objects. Use an initializer list to
    put the following names into the array:

    Chris  
    Lois  
    Meg  
    Peter  
    Stewie
    
  2. Write an enhanced for loop to display the array of names. What is displayed for the last array element? Why is it that value?

  3. Add the Strings “Meg” and “Brian” into index 3, and 4, respectively, so that the array contains the following elements:

    Chris  
    Lois  
    Meg  
    Meg  
    Brian  
    Peter  
    Stewie
    
  4. Write an enhanced for loop to display the array of names.

  5. Write a second, traditional for loop that checks each element for the String “Meg”, if found in the array, remove it, shift the
    remaining elements, and display the array of names. Are both instances
    of “Meg” removed correctly from the array?

This is my code

public class CustomerLister1
{

public static void main(String[] args)
{
    String[] customerName = new String[7];
    customerName[0] = "Chris";
    customerName[1] = "Lois";
    customerName[2] = "Meg";
    customerName[3] = "Peter";
    customerName[4] = "Stewie";

    for (int i = customerName.length-1;i > 3; i--)
    {
        customerName[i] = customerName[i - 2];
    }
    customerName[3] = "Meg";
    customerName[4] = "Brian";

    for (int m = 0; m <= customerName.length-1; m++)
    {
        if(customerName[m].equals("Meg"))
        {
            for(int j = m+1;j < customerName.length-1;j++)
            {
                customerName[j]= customerName[j-1];

            }

        }

    }

    for (String element : customerName)
    {

        System.out.println(element);
    }

}
}

I have tried messing with the element position. It won’t print right because I have meg back to back and it replaces the first meg with the second meg. What can i do here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:13:18+00:00Added an answer on June 13, 2026 at 4:13 am

    Change your if statement like this:

    if(customerName[m].equals("Meg"))
    {
        for(int j = m;j < customerName.length;j++)
        {
            if(j < customerName.length-2) {
                customerName[j]= customerName[j+1];
            } else {
                customerName[j]="";
            }
        }
    m--;
    }
    

    By decrementing i by 1, you’ll hit the same index again.

    And also AmitD is correct; you need customerName[j+1], not [j-1].

    Oh, and you’ll need to handle the end of the array properly; don’t want to try and set customerName[6] = customerName[7] when there is no customerName[7].

    Edit: Ah, you’ll need to start j at m, not m+1. That way you remove the “Meg” at customerName[m] as well as moving everything ahead of it backwards.

    Edit: And the while part of the loop was skipping the last element in the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my algorithm design class homework came this brain teaser: Given a list of
This is homework I'm working on implementing a linked list class for my C++
So I got this question in data structures class' homework, I'm translating so I
This is homework. Beginning Java class. Still wrapping my head around this stuff. The
I have been using this carousel to load images dynamically in my homework project
THIS IS HOMEWORK: I have a program that consists of two winforms and three
(This is not a CS class homework, even if it looks like one) I'm
(This isn't a homework question, as it's above and beyond the class just for
This is my homework question: Expand on your Circle class by adding a method
Doing some homework in my CSC 2310 class and I can't figure out this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.