Skip to main content

Array Size!!!

2 replies [Last post]
Joined: 2008-09-28

Hi people,

My Question is to:
"Read integers from the keyboard until zero is read, storing them in input order in an array A. Then copy them to another array B doubling each integer.Then print B."

Which seems pretty simple in first glance, but has one problem. When initializing the array, I don't know it's size (and I don't want to ask the user to enter it's size). The first soloution that came to my mind was to initialize the array to a very big number. However this is not elegant programming, and is a waste of memory. If you know any other solution other than this then please do tell me. Please help me, im stuck in this program for the past 3 days but no one has given me any proper solution for this question.

Here goes my code:
I just don't know how to initialize the array when you don't know the size of the array. Just to get the answer i have initialized arrays A and B to 20 in the program below.


public class CopynDouble1
public static void main(String[] param){
BufferedReader n = new BufferedReader(new InputStreamReader(;

System.out.print("Input number: ");
String userInput = n.readLine();
int numbers = Integer.parseInt(userInput);

int[] A = new int[20];
int[] B = new int[20];

int i =0;

while(numbers != 0){
A[i] = numbers;
B[i] = 2 * A[i];
System.out.print("Input number: ");
userInput = n.readLine();
numbers = Integer.parseInt(userInput);

catch(Exception e){

Its just a small question as to how to intialize the array when u don't have the size of it but No one has come with a good answer/code yet with which i can sort out my code.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-10-03


I came up with some idea I think you can try it, if you haven't found a solution yet.

So I see that you read the inputted number as string here:

String userInput = n.readLine();

Why don't you make a quick string manipulation to count the number of symbols until 0 in the string? I can give you a hint - you find the indexOf("0") and then you can get the substring that is the actual number from the beginning to the end which is symbol 0 and then you can get length of that string and see what will be the size of your array and set it to the array. After that you can continue with the Integer.parseInt of the string which will now contain only the number without the end mark, you will go through it in the while (since you know how many symbols there are in the string).

Did you get my idea? I won't write you the code because you should do it by your self ;)


Joined: 2008-09-28

Hi iverlinka,

Thanks for replying. Well i am done with that program and i have used ArrayList for that. I have had never used ArrayList before so it took me a week, first to find out the solution i.e ArrayList and then to apply it which took me 2 more days because i didn't know how to exactly use this Arraylist as there are only methods in it and didn't know that how to double the numbers in the Array. So that was quite frustating for this kind of a small program. But anyways All is well that ends well-:)