...

View Full Version : Arrays w/classes help



Rapt0r1
04-19-2008, 04:26 AM
'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


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:


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"));

}

}

mjlorbet
04-19-2008, 06:20 AM
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


for(int a = 0; a < numberOfEntries; a++){
System.out.println("Please enter value #" + (a + 1) + ": ");
array[a] = keyboard.nextInt();
}

instead of array.readInput();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum