Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrays w/classes help

    'm trying to write a program using an array to use user entered integers to perform mathematical equations (sum, avg. high-low, mean, etc.). I've written out a class and a main method, but I can't get it to work.
    This is all I get when i run the program:

    Intro
    Enter number of entries:
    2

    Then nothing. No prompt to enter the integers, no nothing. What have I done wrong?
    Here's my class file, Stats

    Code:
    import java.util.*;
    
    public class Stats 
    {
    	private int[] array;
    	private int highest;
    	private int lowest;
    	private int sum;
    	private int avg;
    	Scanner keyboard = new Scanner(System.in);	
    	
    	public Stats()
    	{	
    		array = new int[5];
    	}
    	
    	public Stats(int numberOfEntries)
    	{
    		this.set(numberOfEntries);
    	}
    	
    	private void set(int numberOfEntries)
    	{
    		array = new int[numberOfEntries];
    	}
    
    	public int getSum()
    	{
    		int count;
    		int next;
    		for(count = 0; count < array.length; count++)
    		{
    			next = array[count];
    			sum = sum + next;
    		}
    		return sum;
    	}
    
    	public int getAvg()
    	{
    		int count;
    		int next;
    		for(count = 0; count < array.length; count++)
    		{
    			next = array[count];
    			sum = sum + next;
    			avg = sum/array.length;
    		}
    		return avg;
    	}
    	
    	public int getMin()
    	{
    		int count;
    		int newLow;
    		newLow = array[0];
    		for(count = 1; count < array.length; count++)
    		{
    			if (newLow < lowest)
    				lowest = newLow;
    		}
    		return lowest;
    	}
    	
    	public int getMax()
    	{
    		
    		int count;
    		int newHigh;
    		newHigh = array[0];
    			for(count = 1; count < array.length; count++)
    			{
    				if (newHigh > highest)
    					highest = newHigh;
    			}
    		return highest;
    	}
    	
    	public void readInput()
    	{
    		String ans;
    		int count;
    		for(count = 0; count < array.length; count++)
    		{
    			array[count] = keyboard.nextInt();
    			if (array[count] <= 0)
    			{
    				System.out.println("Invalid entry, please re-enter.");
    				array[count] = keyboard.nextInt();
    			}
    			System.out.println("You entered " + array[count] + "Is this correct? (y/n)");
    			ans = keyboard.next();
    			if (ans.trim().equalsIgnoreCase("n"))
    			{
    				System.out.println("Please enter a positive integer");
    				array[count] = keyboard.nextInt();
    			}
    		}
    	}
    	
    	public void userInterface()
    	{
    		String ans;
    		int choice;
    		System.out.println("Please select an option: (1, 2, 3, 4, 5, 6, 7)");
    		System.out.println("1.Get sum");
    		System.out.println("2.Get average");
    		System.out.println("3.Get minimum");
    		System.out.println("4.Get maximum");
    		System.out.println("5.Get standard deviation");
    		System.out.println("6.Get variance");
    		System.out.println("7.Quit");
    		choice = keyboard.nextInt();
    			if (choice <=0)
    				{
    					System.out.println("Invalid entry, please re-enter");
    					choice = keyboard.nextInt();
    				}
    		System.out.println("You entered " + choice + ". Is this correct? (y/n)");
    		ans = keyboard.next();
    			if (ans.trim().equalsIgnoreCase("n"))
    			{
    				System.out.println("Please select an option: (1, 2, 3, 4, 5, 6, 7)");
    				choice = keyboard.nextInt();
    			}
    		switch (choice)
    		{	
    			case 1:
    				getSum();
    				System.out.println("The sum is " + sum);
    				break;
    			case 2:
    				getAvg();
    				System.out.println("The average is " + avg);
    				break;
    			case 3:
    				getMin();
    				System.out.println("The minimum is " + lowest);
    				break;
    			case 4:
    				getMax();
    				System.out.println("The highest is " + highest);
    				break;
    			case 7:
    				System.exit(0);
    				break;
    		}
    		
    	}
    }
    And the main method:

    Code:
    import java.util.*;
    
    public class Main 
    {
    	public static void main(String[] args)
    	{
    		String ans;
    		int numberOfEntries;
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("Intro");
    		System.out.println("Enter number of entries: ");
    		numberOfEntries = keyboard.nextInt();
    		if (numberOfEntries <= 0)
    		{
    		System.out.println("Number of entries must be greater than zero. Please re-enter.");
    		numberOfEntries = keyboard.nextInt();
    		}		
    		Stats array = new Stats(numberOfEntries);
    				
    		array.readInput();
    		do
    			{
    			array.userInterface();
    			System.out.println("Would you like to do something else?");
    			ans = keyboard.next();
    			}while (ans.trim().equalsIgnoreCase("n"));
    					
    		}
    
    }

  • #2
    Regular Coder mjlorbet's Avatar
    Join Date
    Jan 2008
    Location
    Milwaukee, WI
    Posts
    724
    Thanks
    8
    Thanked 96 Times in 95 Posts
    hmm, you didn't put in a prompt to enter the numbers... should just be a flashing cursor waiting for the number of input elements you requested. try this
    Code:
     
    for(int a = 0; a < numberOfEntries; a++){
    System.out.println("Please enter value #" + (a + 1) + ": ");
    array[a] = keyboard.nextInt();
    }
    instead of array.readInput();
    -Mike
    "Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •