I'm writing a program that will ask the user for input and then store it into an array until called to the output screen. here is an example of my code so far. I have no idea how to get the input to store in the array.

import java.util.Scanner;

public class Sales
{
        //main method begins
    public static void main( String[] args )
    {
        //declaring arrays
        int[] salary;
        int[] value;

        //create arrays
        salary = new int [ 10 ];
        value  = new int [ 11 ];

        //init Scanner and get user input
        Scanner sc = new Scanner( System.in );

        System.out.print("Enter sales amount: " );              //prompt
        salary = input.nextInt();


        while (salary[0] != -1 )
        {
            for (int index = 0; index < (salary.length); index++)
            {
                if ( 0 <= (int)Math.floor((( salary[index] * (.9) + 200 ) * .01 )))
                    ++value[0];
                else
                    ++value[(int)Math.floor((( salary[index] * (.9) + 200 ) * .01 ))];
            }
                System.out.printf( "%s:%20s:\n", "Index", " Value");  //column heading

                for (int index1 = 2; index1 < value.length - 1; index1++)
                      System.out.printf("$%5d-%3d: %10d\n", index1 * 100, index1 * 100 + 99, value[index1]); 

                for (int index2 =11; index2<= value.length; index2++ )
                      System.out.printf( "$%5d%5s: \n", index2*100-100, " and over", value[10]);

                System.out.print("Enter sales amount: ");               //prompt
                salary = input.nextInt();
        }
    } // end main
} // end class Salary

Any help would be greatly appreciated.

Recommended Answers

All 3 Replies

You just need an int to hold the number of entries already in the array(s) - initial value 0. This is also the correct index to use for the first available array element. Increment it each time you add something to the array.

int numEntries = 0;
while (user is still entering data) {
   array[numEntries] = the user's input
   numEntries ++; // count number of entries
}

You can use loops to put values into arrays like this

for(int i=0;i<10;i++)
{
salary[i]=sc.nextInt();
}

Thanks for the help. I finished it late last night and am on to another. I appreciate the input.

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.