0

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()
2
  • Something like values[count] = number; in the while loop 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 up for loop in your textbook. Commented Feb 19, 2013 at 0:40
  • Ah, I'd been confused by the indenting and had missed the point that the numbers were being read into main(), which is static and therefore values is not accessible from there. Commented Feb 19, 2013 at 2:07

2 Answers 2

1

Briefly summarizing the changes I made:

1) You had 3 variables doing the same thing so I got rid of 2 of them. (numOfInputs and count).

2) You were going through the loop asking the user for numbers but you never added the numbers to the array.

3) I added a condition to the loop so that after 50 numbers the loop stops so you never go over the size of the array.

4) Moved most of main to another method. Java (and most other languages) don't allow static methods to access non-static variables. So main creates a new instance of MyContainer then calls a method which does the adding.

5) I added a print list method which just goes through the list using a loop.

import java.util.Scanner;

public class MyContainer {

private double[] values;
private int size;
public MyContainer() {
    values = new double[50];
    size = 0;
}

public void run(){
    // 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; //count removed double average = 0;

    while (number >= 0 && size < values.length) {
        values[size] = number; //Line added
        size++;
        sum += number;
        System.out.print("Enter a positive number, negative number will end entry.");
        number = input.nextDouble();
    }

    sum = sum + number;
    average = sum / size;
    System.out.println("The average of the numbers as of right now is " + average);

}

public void printList(){
    System.out.print("[");
    for (int i = 0; i <= size; i++){
        System.out.print(values[i]+", ");
    }
    System.out.println("end]");
}

public static void main(String[] args) {
    MyContainer cont = new MyContainer();
    cont.run();
    cont.printList();
}

}

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

2 Comments

+1: Good point about the value of putting the bulk of the code into the non-static member function, thereby allowing access to values, which is inaccessible from main() because main() is static.
Thank you for the help! it was really helpful. Simon - I tried storing using the " values[count] = number; "format before and when I do it gives me an error saying ']' is expected and its an illegal start of expression. I'm not sure why.
0

You reall should search for something like this, there are many examples on how to use an array like this: How to put a Scanner input into an array... for example a couple of numbers

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.