Hey guys, I'm trying to make this program that allows a person to set the size of an array to any value they choose. Then, the program will ask the user to input n values to be stored in the array at the corresponding index. Then the program modifies each array element by multiplying it with by its index. And at the end, prints the finals contents of the array. This is what I have so far:

package dastrap;

import java.util.Scanner;
public class DASTRAP {

    public static void main(String[] args) {

        Scanner s = new Scanner (System.in);


        System.out.print(" Enter the size of the array: ");
        int sizeOfArray = s.nextInt(); 

        for(int index = 0; index < sizeOfArray; index++){       
        System.out.print(" Input value: ");


        }
    }
}

How do I store the inputs of the user into the array?

How do I store the inputs of the user into the array?

By using an assignment statement: theArray[theIndex] = theValue;

Ok, so I was able to set the size of the array from the input with this code

  Array = new int[sizeOfArray];

Now, my only problem is storing the inputs for each index. Like say the inputted size of the array was 4; and so I input the values 1, 2, 3, 4. How do I store my inputted values (1,2,3,4) into the indexes?

        Array = new int[sizeOfArray];

        System.out.println(Array.length);

        for(int index = 0; index < sizeOfArray; index++){       
        System.out.print(" Input value: ");
        int value = s.nextInt();

The index to the array is changed by the for loop.
The value from the user is in the variable: value
Use a statement like I posted: theArray[theIndex] = theValue;
inside the loop.
Read and store the number in the array on each time around the loop.

BTW Coding standards say variable names should begin with lowercase: array vs Array

I tried this code but I got an error "incompatible types"

array[sizeOfArray] = s; 

How do I fix this? I'm sorry, still new to this array thing :D Thanks for taking time to help me out

Put the index variable inside of the []s

got an error "incompatible types"

Please post the full text of the error message. It has important information that you have left off.

Also post the full text of the current version of the program.

Here's the full code I have so far:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dastrap;

import java.util.Scanner;
public class DASTRAP {

    public static void main(String[] args) {

        Scanner s = new Scanner (System.in);
        int[] array;

        System.out.print(" Enter the size of the array: ");
        int sizeOfArray = s.nextInt(); 

        array = new int[sizeOfArray];

        System.out.println(array.length);

        for(int index = 0; index < sizeOfArray; index++){       
        System.out.print(" Input value: ");
        int value = s.nextInt();

        array[sizeOfArray] = s; 

        }
    }
}

And the error I got was:

incompatible types
reqcuired: int
found: Scanner

Line 26, you are assigning the s which is an object of Scanner class into an int[] datatype. Also, you are using a wrong index. You will get an array out of bound exception as well because array uses 0-index. In other words, an array size n will have indices from 0 to n-1. What you need to do is changing it to array[index] = value. The index is the index you want to use in your loop and the value is what you want.

Line 26 has several problems.
The index to the array should be the for statement's control variable: index
It needs to change each time around the loop. Your code uses an index to the array that is never changed in the loop.
The value read in from the user is in the variable: value. That is what you want to save in the array. s is an instance of the Scanner class and is what you are using to read data from the user.

I typed it wrong in the earlier post. NOTE: That is a problem with using single letter variable names, it is easy to get them confused because the name has no meaning. If the name had been scnr I might have noticed.

OMG THANKS! I finally stored the values into the array. My only problem now is how can i modify each array element by multiplying it with its array position (index) and then printing the final contents of the array. Do I do the multiplying inside the for loop?

how can i modify each array element by multiplying it with its array position (index)

You can access each element in an array like this: theArray[theIndex]
that expression will return the value in theArray at element theIndex.

Make another loop that assigns to each element in the array, the value that is there times the current value of the index. You know how to code each part of that, now put it all together.

what do you mean by "assigns to each element in the array"?

you make a for loop then in each iteration, you get the value inside that index and times the index itself, then print it out.

Thank you everyone for helping me out! I'm finally done with this program. Here's the finalized code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dastrap;

import java.util.Scanner;
public class DASTRAP {

    public static void main(String[] args) {

        Scanner s = new Scanner (System.in);
        int[] array;
        int index, value; 

        System.out.print(" Enter the size of the array: ");
        int sizeOfArray = s.nextInt(); 

        array = new int[sizeOfArray];

        for(index = 0; index < sizeOfArray; index++){       
        System.out.print(" Input value: ");
        value = s.nextInt();
        array[index] = value;

        }

        for (index = 0; index < sizeOfArray; index++) {
            int product = array[index] * index;
            System.out.println(product);
        }



    }
}

Thank you again for helping me out! :D

In the first post you said:

the program modifies each array element by multiplying it with by its index.

what do you mean by "assigns to each element in the array"?

I assumed that modify and assigns have the same meaning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.