Declaring an array in a for loop (Java) or similar

In summary,The assignment was intended to get us comfortable with single dimensional, basic arrays. We haven't been taught anything regarding Vector or ArrayList yet, so the only way I see is to create a simple array.
  • #1
stripes
266
0

Homework Statement



I need to create a loop that asks the user to input double numbers into an array. The number of elements in the array is not predefined; the user will continue to enter numbers as long as they wish until a NEGATIVE number is entered, at which point the control will exit the loop used for inputting.

Homework Equations



None

The Attempt at a Solution



My problem is this: my understanding is that an array can't do anything, ie can't be used, until the control knows how much memory to allocate for that array.

import java.util.Scanner;

public class ClassifyScores2
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);

System.out.println("This program classifies scores into different ranges.");

System.out.println("Enter your scores now, enter a negative number when you wish to stop:");

for (ind = 0; new score[ind] >= 0 ; ind++)
{
score[ind] = input.nextInt();
//if(score[ind] > 100)
//System.out.println("This score is too high. Enter a score less than or equal to 100");
//else if(score[ind] < 0)
//System.exit(1);
//else
//score2[ind] = score[ind]

the commented if statements I can do myself, I'm not concerned with that. I will close the loop later on. My program runs and compiles if I define the array score somewhere earlier --> there are no syntax errors in my source code except what is in bold.

I am able to ask the user "How many numbers will you be inputting?" and then store that number, and then use that in the loop. That way, the control knows how many numbers will be entered. But the assignment strictly says "The user of your program can enter as many scores as (s)he wishes. The user identifies the end by entering a negative number for score; your program then should display the tabulated scores and exit."

This must be followed. The only way I can see this is by declaring my "score" array INSIDE my FOR loop. I have no clue how to do this! Is this possible? Is there any other way of doing this?

Thank you so much in advance!
 
Physics news on Phys.org
  • #2
Does the assignment specifically say that the values should be stored in an array?

If so, then you could just make the array very large (say, 1024 items). Then you can either store the index of the last valid score entered, or look for a negative number when playing the scores back.

If not, have a look at some of the other Java containers, like Vector and ArrayList.
 
  • #3
Something like ArrayList is easiest, since it dynamically resizes itself. If you are required to use an int[] then define it with a certain size. When it is full, create a new, larger, array, copy the existing entries across, and then point score to the new array.
 
  • #4
Also, you might want to look up break... it's slightly less rigorous than System.exit() :-)
 
  • #5
The assignment was intended to get us comfortable with single dimensional, basic arrays. We haven't been taught anything regarding Vector or ArrayList yet, so the only way I see is to create a simple array. I imagine this is the only way because how else can the user store as many variables as s/he wants?

Anyways, the assignment was due about an hour ago, so I ended up just prompting the user to input how many scores s/he intends to input, then the array is created with THAT many elements. Not what the assignment was asking for specifically, but it was the best I could do.

But just so I know, is there any way of doing it without asking the user how many scores will be entered, and just using a regular array?

Thanks again.
 
  • #6
CompuChip said:
Also, you might want to look up break... it's slightly less rigorous than System.exit() :-)

You mean a switch statement? Well i ended up removing the System.exit(); line anyways. I revamped the for statement body. But thanks :)
 
  • #7
No, I meant "break" to break out of a loop :-)
 
  • #8
CompuChip said:
Also, you might want to look up break... it's slightly less rigorous than System.exit() :-)

stripes said:
You mean a switch statement? Well i ended up removing the System.exit(); line anyways. I revamped the for statement body. But thanks :)
switch and break are completely different kinds of statements. A switch structure is a lot like an if ... then else if ... else block, while a break statement is similar to a goto.
 
  • #9
Mark, the confusion arises because you can also use break to break out of the switch:
Code:
switch(x)
{
  case 1: 
    // do something
    break; // no fall-through
  case 2:
    // something else
  // ...
}

I meant it in the context of
Code:
for(int i = 1; i < 10; ++i)
{
  if (i == 5)
  { 
    break;
  }
}
 

Related to Declaring an array in a for loop (Java) or similar

1. What is the syntax for declaring an array in a for loop in Java?

The syntax for declaring an array in a for loop in Java is:
for (int i = 0; i < array.length; i++) {
 array[i] = value;
}

2. Can I declare multiple arrays in a single for loop?

Yes, you can declare multiple arrays in a single for loop by using multiple index variables. For example:
for (int i = 0, j = 0; i < array1.length && j < array2.length; i++, j++) {
 array1[i] = value1;
 array2[j] = value2;
}

3. How can I declare an array with a specific size in a for loop?

To declare an array with a specific size in a for loop, you can use the keyword 'new' followed by the data type and the desired size. For example:
for (int i = 0; i < 5; i++) {
 int[] array = new int[5];
 array[i] = value;
}

4. Can I use a for loop to declare and initialize an array at the same time?

Yes, you can use a for loop to declare and initialize an array at the same time by using the curly braces after the array declaration. For example:
for (int i = 0; i < array.length; i++) {
 int[] array = {value1, value2, value3};
}

5. Is it possible to declare an array of objects in a for loop?

Yes, it is possible to declare an array of objects in a for loop by using the 'new' keyword followed by the object's data type and the desired size. For example:
for (int i = 0; i < 3; i++) {
 Object[] array = new Object[3];
 array[i] = new Object();
}

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top