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

  • Home
  • SEARCH
  • 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 7574001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:19:27+00:00 2026-05-30T16:19:27+00:00

As part of a homework assignment, we are supposed to create an array that

  • 0

As part of a homework assignment, we are supposed to create an array that will resize itself if the user attempts to input more data to a new index that is out of bounds. We are not allowed to use any libraries like hashsets, arraylists, etc. My code works, however, the length of the array always ends up being one larger than what is required. I know the problem lies in the nature of the while loop because it will grow and then add, but I don’t know how I would fix it.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class DynamicArray
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        System.out.print("Enter a desired length for an array: ");
        String[] x = new String[kb.nextInt()];
        int index = 0;
        System.out.print("Enter as many Strings as desired, separated by a new line. Type in \"end\" to print out the contents of the array.");
        String input = kb.nextLine();
        while(!input.equalsIgnoreCase("end"))
        {
            if (index < x.length)
            {
                x[index] = input;
            }
            else
            {
                String[] temp = new String[x.length + 1];
                for (int i = 0; i < x.length; ++i)
                {
                    temp[i] = x[i];
                }
                temp[index] = input;
                x = temp;
            }
            ++index;
            input = kb.nextLine();
        }
        for (int i = 0; i < x.length; ++i)
        {
            System.out.println(x[i]);
        }
    System.out.println(x.length);
    }
}    
  • 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-05-30T16:19:28+00:00Added an answer on May 30, 2026 at 4:19 pm

    I know the problem lies in the nature of the while loop because it will grow and then add […]

    Not at all. The problem lies in the way Scanner.nextInt() and Scanner.nextLine() work. Scanner.nextInt() will read in an integer, but will not swallow the newline after the integer. So the first thing Scanner.nextLine() sees is that newline, and it thinks it sees an empty line, which is what it returns. So x[0] is a the empty string.

    You can see this a bit more clearly if you change this:

                System.out.println(x[i]);
    

    to this:

                System.out.println(i + ": " + x[i]);
    

    because then you’ll see that the first thing it prints is 0:.

    By the way, your approach in general is very inefficient, since it requires creating many more arrays than are actually needed. Instead of increasing the size of the array by one, it’s much more efficient to double the size of the array, and keep track of its length separately (rather than using x.length). (Admittedly, in your case, efficiency is probably a non-issue, since you’re taking input from the user, and there’s no way that the user will type in elements anywhere near as fast as Java can copy the array; but in general, that’s the best way to design a dynamically-resizeable array.)

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

Sidebar

Related Questions

I have a homework assignment which asks to have the user input a date
For my homework assignment, I'm supposed to create an ATM/Teller program which stores users
I have a homework assignment that asks to create an order form. The order
as part of a homework assignment for my security class, I'm supposed to add
As part of a homework assignment in Lisp, I am to use apply or
This is part of a homework assignment. I've got several questions asking find the
(this is indirectly a part of a much larger homework assignment) I have something
As part of a homework assignment, I have to program a simple chess game
I made a swing calculator as part of homework that I did and would
as a part of a homework assignment, I'm trying to read a single char

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.