I have to create a program that asks the user for a series of numbers, and that saves the input in an array. Then later on allows user to search for a number in the array and prints the array. I am stuck because it does not let me store the input in the array. I also need help printing the array.
I am taking a Java class and the textbook we use is not helpful at all, does anyone have any recommendations on a good Java textbook?
This is my code so far:
import java.util.Scanner;
public class MyContainer {
private int [] values;
private int size;
public MyContainer() {
values = new int[50];
size=0;
}
public static void main(String [] args)
{
//load()
Scanner input = new Scanner(System.in);
double sum = 0;
System.out.print("Enter a positive number, negative number will end entry.");
double number = input.nextDouble();
int count = 0;
double average = 0;
int numofinput = 0;
while (number >= 0)
{
count++;
numofinput ++;
sum += number;
System.out.print("Enter a positive number, negative number will end entry.");
number = input.nextDouble();
}
sum = sum + number;
average = sum/numofinput;
System.out.print("The average of the numbers as of right now is "
+ average);
}
//search()
values[count] = number;in thewhileloop should store the numbers in the array. If not, could you expand on "does not let me store the input in the array"? To print the array, you could consider taking a similar approach to the way you've read the numbers in the first place. Since you'll know in advance how many numbers to print out, you could look upforloop in your textbook.main(), which is static and thereforevaluesis not accessible from there.