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(" "); 
3
  • can you add your code to the post? Commented Oct 30, 2013 at 20:42
  • This is what I tried but it didn't work Commented Oct 30, 2013 at 20:43
  • 2
    that's not how you write loops :( Commented Oct 30, 2013 at 20:45

4 Answers 4

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:

Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

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)

5 Comments

So to display the contents would I then just use a System.out.print and put c inside of that?
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, Please look at the format of a for-loop. We've provided links to helpful resources.
@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, 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.
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]);
} 

Comments

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.