1

I am creating a program that takes various double arrays and displays them. The array is 10 elements and I am asked to get the values for elements 2 through 9 from the user input by using a loop. I have tried a for loop but I just don't understand how to get this done.

int c; 
for(c = 0; c >= 2 && c <= 9; c++){ 
  System.out.println("Enter a value for the elements 2-9: "); 
} 
System.out.println(" "); 
Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33

4 Answers4

2

If you have a Java array as follows:

double myarr[10];

You access elements in an array by index (assuming the array has been populated with data)

double somenum = myarr[3]; // extracts the *fourth* element from the list

To set a value in the array, you use the assignment operator and specify a value:

myarr[7] = 3.14159; // sets the *eighth* element to value '3.14159'

If you wish to iterate over a range of numbers, you can use a for-loop. For-loops have the following format:

for (initialization; condition; increase)

If you wanted to print all numbers between 1 and 10, you can write:

for (int i=1; i<=10; i++) {
    System.out.println(i);
}

The trick is to use the variable i in the for-loop and ensure the loop iterates over the proper range. Hint: You can use i as an array index.

Here are some good resources:

Community
  • 1
  • 1
xikkub
  • 1,641
  • 1
  • 16
  • 28
0

c needs to start at 1 (since you want the second elment) and stop at 8 (for the ninth) so for(int c=1;c<9;c++) should be the loop

When writing loops remember;

  • array indexes are 0 based, the first element is at 0, the second at 1 up to the last which is at the length of the array minus 1
  • if your loop increments, then the smallest value it can have is what ever it starts as, so you shouldn't check to make sure its greater then that, (ie if you start at 2 and increment then you don't need to check to if its greater than or equal to 2 because it always is)
vandale
  • 3,600
  • 3
  • 22
  • 39
  • So to display the contents would I then just use a System.out.print and put c inside of that? – user2939049 Oct 30 '13 at 20:50
  • So then now to sum those elements in a for loop would I use: int sum = 0; for(c = 0; sum++){System.out.println(" ");} – user2939049 Oct 30 '13 at 20:59
  • @user2939049, Please look at the format of a for-loop. We've provided links to helpful resources. – xikkub Oct 30 '13 at 21:01
  • @CollinJSimpson I interpreted the op as want the loop to go from the second element of the array (at index 1) to the ninth element of the array (at index 8) inclusive of the the end points. Thus shouldn't the loop counter start at 1 and go to 8? – vandale Oct 30 '13 at 21:04
  • @vandale, I had misread the starting index as 1. However, it's not entirely clear if "elements 2 through 9" implies using indexes or numerical positions. – xikkub Oct 30 '13 at 21:07
0

Here's a loop and a means for user input:

Scanner reader = new Scanner(System.in);

for(int i=2; i<8; i++){
    System.out.println("Enter Element "+i);
    a=reader.nextInt();
    //store "a" somewhere
}
Josh T
  • 564
  • 3
  • 12
0

Take a look at the syntax for for loops here

Console console = System.console();
double arr[10];
for(int c = 1; c<10; c++){ 
    String input = console.readLine("Enter a value for the elements 2-9: ");
    arr[c] = Double.parseDouble(input);
    System.out.println(arr[c]);
} 
lipeiran
  • 526
  • 13
  • 33