0

Hello! So there's a problem.

We're given a file.txt with a lot of numbers (let's consider the amount is greater than 1000). On the first line we can see the amount of numbers. The next lines contain numbers (1 line = 1 number). So we need to write a code which will autofill arrays with all these numbers. We're not allowed to fill one array with more than 100 elements, because it will destroy our PC (I've read it somewhere).

Example of the file.txt:

5
78
67
56
45
23

I don't know how to auto create arrays depending on an amount of numbers we have.

I will be grateful for the help.

P. S. please don't write and suggest very difficult constructions, I won't understand them, because I am the beginner in programming :D

1
  • Welcome to Stack Overflow. Please read how to ask a question on this site. While you definitely can ask a question, this site is not a service to solve your home-works or tasks, when you don't invest in it, yourself. On this kind of questions, people will ask you: what have you tried? where did you struggle at? what did you research? what attempts did you give it to find something on the internet? have you read at least one little article on this? in the future, try things yourself, and then, if you can't - we'll be glad to help you. Commented Feb 20, 2021 at 19:41

2 Answers 2

2
  1. Read the first line and convert the result to a number that you store to count.
  2. Then add this line: int [] array = new int [count];
  3. Next setup a for loop: for( var i = 0; i < count; ++i ) and in this loop, you read the current line, convert the value to a number and store it to array [i].

Is this explanation simple enough?

For the concrete Java code, you should use your own brain, otherwise it will never change that you don't understand …


If this "100 entries" limit is relevant, you create a list of arrays (List<int[]> arrays) and the sequence of code looks a bit different:

  1. Get the amount of numbers in the file.
  2. Setup a while loop: while( count > 100 ).
  3. In that loop, you create an array for 100 values and store it to the list:
    int [] array = new int [100];
    arrays.add( array );
    
    Further you read the next 100 lines, convert the values to numbers and store them to array [i]; for that, you use a for loop as above.
  4. The last operation in the while loop is this: count -= 100;.
  5. After the while loop, you add the code from above the separator line, with one addition: after creating the array, you need to add it to the list of arrays before you start the for loop.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That was really helpful.
1
int[] array = new int[size];

Where size is the number on the first line of the text file. This is how you create an array, you don't have to know the size of the array at compile time, we can just use a variable for size.

Also, not sure where you heard that arrays greater than 100 in size destroy your computer, that's just not true!

1 Comment

Well, if we get around 10,000 numbers and we fill one array with all of them it literally says something about memory error, so I want to optimize my code. Like to fill several arrays and each array would contain up to 100 elements. But I appreciate for the help.

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.