import java.util.Scanner;
public class Sales {
public static void main(String[] args) {
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales() {
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = new int[input.nextInt()];
for (int i = 0; i < temp.length; i++) {
System.out.print("Enter sales for salesperson " +
(i + 1) + ": ");
temp[i] = input.nextInt();
}
return temp;
}
private static void printSales(int[] s) {
System.out.println();
System.out.println("Salesperson Sales");
System.out.println("----------- -----");
for (int i = 0; i < s.length; i++) {
System.out.printf("%6d%12d\n", i + 1, s[i]);
}
}
private static void printSummary(int[] s) {
int sum = 0;
int max_sale = 0; // Salesperson with the most sales
int min_sale = 0; // Salesperson with the least sales
for (int i = 0; i < s.length; i++) {
sum = (s[i] + sum);
if (s[i] > max_sale)
max_sale = s[1];
else if (s[i] > min_sale)
s[i] = min_sale;
}
System.out.println();
System.out.println("Total sales: " + sum);
System.out.println("Average sales: " + (double)sum / s.length);
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
s[min_sale]);
}
}
The purpose of the application is to take the number of salespeople as input, along with their sales and then display individual sales, total sales, and average. That is working fine, but it's also supposed to display which salesperson had the max and minimum sales and what they were (lines 51 - 54). At the moment, any time the max is greater than the number of salespeople I get an ArrayIndexOutOfBoundsException and for whatever reason can't figure it out.
1 - Modify your for loop to get the max and min without modifying the array
2 - Try to print max and min instead of printing sum[max] and some[min] (which can throws IndexOutOfBoundsException)
3 - min_sale should be greater than 0, actually a value enough large (because you can have only positive values in your array)
To summarize :
int sum = 0;
int max_sale = Integer.MIN_VALUE; // Salesperson with the most sales
int min_sale = Integer.MAX_VALUE; // Salesperson with the least sales
for (int i = 0; i < s.length; i++){
sum = (s[i] + sum);
if (s[i] > max_sale)
max_sale = s[i];
else if (s[i] < min_sale)
min_sale = s[i];
}
System.out.println("Salesperson " +
" had the maximum sale with " +
max_sale);
System.out.println("Salesperson " +
" had the minimum sale with " +
min_sale);
if (s[i] > max_sale)
max_sale = s[1];
else if (s[i] > min_sale)
s[i] = min_sale;
Here you are trying to assign the value in s[1] to max_sales. whereas you should be assigning max_sale = i. and the if condition should be
if(s[i] > s[max_sale] )
Updated code:
for (int i = 0; i < s.length; i++)
{
sum = (s[i] + sum);
// Finds the index of the sales person with best sales
if (s[i] >= s[max_sale])
max_sale = i;
// If this sales person is not the best, then check if he is last
else if (s[min_sale] > s[i])
min_sale = i;
}
the specific problem that's causing your error is here,
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
s[max_sale]);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
s[min_sale]);
you're using your result as though it were an index
change it to the following
System.out.println("Salesperson " + (max_sale + 1) +
" had the maximum sale with " +
max_sale);
System.out.println("Salesperson " + (min_sale + 1) +
" had the minimum sale with " +
min_sale);
Related
import java.util.*;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size ??");
int n = sc.nextInt();
int[] marks = new int[n + 1];
for (int i = 0; i < n; i++) {
System.out.println("Enter " + i + " number ??");
marks[i] = sc.nextInt();
}
System.out.println("The following numbers are : ");
for (int j = 0; j < marks.length - 1; j++) {
System.out.println(marks[j] + " ");
}
int max = marks[0];
int min = marks[0];
int s = marks.length;
for (int i = 0; i < s; i++) {
if (marks[i] > max) {
max = marks[i];
}
if (marks[i] < min) {
min = marks[i];
}
}
System.out.println("Max is " + max + " Min is " + min);
}
Output:
Enter the size ??2
Enter 0 number ??
56
Enter 1 number ??
56
The following numbers are :
56
56
Max is 56 Min is 0
Hello and welcome to StackOverflow. Next time, febore you jump into the internet for help, please try this approach. It will solve your problem much quicker.
for (int i = 0; i < s; i++) {
System.out.println("DEBUG: number in index " + i + " is " + marks[i]);
if (marks[i] > max) {
System.out.println("DEBUG: number is greater than current max " + max);
max = marks[i];
}
if (marks[i] < min) {
System.out.println("DEBUG: number is smaller than current min " + min);
min = marks[i];
}
}
This process is called "debugging". It can be done by adding spurious amount of logging like above or by stepping through the code with a debugger.
The size of mark array is 3 in the above example
int[] marks = new int[n + 1];
Let total number of marks is (n) : 2
We are creating the array with size of 3.
By default value for int primitives in java is 0
marks array is created with size 3
{56, 56, 0}
As per the logic the minimum value among these three elements are 0
I have this program:
int i;
double array[] = new double[10] ;
Scanner input = new Scanner (System.in) ;
for (i = 0; i < 10; i++) {
System.out.println("Give " + (i+1) + " number") ;
array[i] = input.nextDouble();
}
double max = array[0], min =array[0];
int negative = 0 ;
for (i = 0; i < 10; i++){
if(array[i] > max){
max = array[i] ;
}
if (array[i] < min){
min = array[i];
}
}
//Difference between Biggest to Smaller number
for (i = 0; i < 10; i++){
// ...
}
for (i = 0; i < 10; i++){
if(array[i] < 0){
negative = negative + 1;
}
for (i = 0; i < 10; i++){
System.out.println("The double of " + (i+1) + " is: " + (array[i] * 2) );
}
System.out.println("\n" + "The Maximum is: " + max);
System.out.println("The Minimum : " + min);
System.out.println("The Difference between Biggest to Smaller is : ");
System.out.println("Τhe count of negative objects are : " + negative);
I want to print the bigger to smaller number difference with values I have now.
I forgot what I need to add. I searched online, but the most of answers that I found, conflict with the whole program.
Any suggestion here?
Please help to understand why, in the following, I didn't get output of minimum value in array. Using scanner for input value, the program should output also the minimum number.
** without using .sort.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] list;
list = new int[10000];
int sum = 0;
double avr = 0;
int min = list [0];
int max = list [0];
int x = 0;
int value;
Scanner input = new Scanner(System.in);
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
while (x <= list.length && (value != -1 || x == 0)) {
list[x] = value;
x++;
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
}
for (int i = 0; i < list.length; i++) {
sum += list[i];
avr = sum / x;
if(list[i] > max)
max = list[i];
if( list[i] < min )
min = list[i];
}
System.out.println("The sum of all values are: " + sum);
System.out.println("The average value of the numbers are: " + avr);
System.out.println("The maximum value of the numbers are: " + max);
System.out.println("The minimum value of the numbers are: " + min);
}
}
I will suggest two improvements at the bare minimum.
Initialize your min to the first value. Right now you have min initialized to 0 and user never enters any number smaller than 0. That's why you keep getting 0 for min.
Don't run your loops 1000 times. Run it as many times as there are non zero elements.
See the following working snippet:
int[] list;
list = new int[10000];
int sum = 0;
double avr = 0;
int min = list [0];
int max = list [0];
int x = 0;
int value;
Scanner input = new Scanner(System.in);
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
min = value;
while (x <= list.length && (value != -1 || x == 0)) {
list[x] = value;
x++;
System.out.print("Add number " + (x + 1) + ": ");
value = input.nextInt();
}
for (int i = 0; i < list.length && list[i] !=0; i++) {
sum += list[i];
avr = sum / x;
if(list[i] > max)
max = list[i];
if( list[i] < min )
min = list[i];
}
System.out.println("The sum of all values are: " + sum);
System.out.println("The average value of the numbers are: " + avr);
System.out.println("The maximum value of the numbers are: " + max);
System.out.println("The minimum value of the numbers are: " + min);
I am unsure on how to get the maximum average, minimum average, and average of everything in total. I already set variables with values for them in my code, however I am not sure how to input the averages in my code. Any help would be great! Thanks.
import java.util.Scanner;
public class Averages {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int max = 0;
int min = 9000;
int score = 0;
int n;
int c;
int rowMaxNumber = 0;
int rowMinNumber = 0;
int columnMax = 0;
int columnMin = 0;
int averageMin = 0;
int averageMax = 0;
double average = 0;
System.out.println("How many rows?");
n = scan.nextInt();
for (int row = 1; row <= n; row++) {
System.out.println("How many student in row " + row + "?");
c = scan.nextInt();
for (int column = 1; column <= c; column++) {
System.out.println("Score for student " + column + " in row "
+ row + "?");
score = scan.nextInt();
rowMaxNumber = row;
columnMax = column;
rowMinNumber = row;
columnMin = column;
}
if (score >= max) {
max = score;
}
if (score < min) {
min = score;
}
}
System.out.println("Student " + columnMax + " of row " + rowMaxNumber
+ " was highest with " + max);
System.out.println("Student " + columnMin + " of row " + rowMinNumber
+ " was lowest with " + min);
System.out.println("Row " + rowMaxNumber + " had highest average with "
+ averageMax);
System.out.println("Row " + rowMinNumber + " had lowest average with "
+ averageMax);
System.out.println("Class average is " + average);
}
}
public class Averages {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int max = 0;
int min = 9000;
int score = 0;
int n;
int c;
int rowsum = 0;
int rowmaxnumber = 0;
int rowminnumber = 0;
int columnmax = 0;
int columnmin = 0;
int averagemin = 9999;
int averagemax = 0;
double average = 0;
System.out.println("How many rows?");
n = scan.nextInt();
for (int row = 1; row <= n; row++) {
System.out.println("How many student in row " + row + "?");
c = scan.nextInt();
rowsum = 0;
for (int column = 1; column <= c; column++) {
System.out.println("Score for student " + column + " in row "
+ row + "?");
score = scan.nextInt();
rowsum += score;
rowmaxnumber = row;
columnmax = column;
rowminnumber = row;
columnmin = column;
if (score >= max) {
max = score;
}
if (score < min) {
min = score;
}
}
int avg=rowsum/c;
if( avg >= averagemax){
averagemax=avg;
}
if( avg <= averagemin){
averagemin=avg;
}
}
System.out.println("Student " + columnmax + " of row " + rowmaxnumber
+ " was highest with " + max);
System.out.println("Student " + columnmin + " of row " + rowminnumber
+ " was lowest with " + min);
System.out.println("Row " + rowmaxnumber + " had highest average with "
+ averagemax);
System.out.println("Row " + rowminnumber + " had lowest average with "
+ averagemin);
System.out.println("Class average is " + average);
}
}
I have restructured some code to calculate sum of scores as we see them in a row, and calculate the average on end of scanning row, also avg will give the average by dividing rowsum by no. of students in row, and then avg is compared with the previous highest and previous lowest averages. Just like you do it with score! Good luck.
I figured out how to get the maximum average, minimum average, and total average. Thanks for anybody that helped!
import java.util.Scanner;
public class Averages
{
public static void main ( String[] args )
{
Scanner scan = new Scanner(System.in);
int max = -9999;
int min = 9999;
int score = 0;
int n;
int c;
int rowsum = 0;
int rowMaxNumber = 0;
int rowMinNumber = 0;
int columnMax = 0;
int columnMin = 0;
double averageMin = 9999;
double averageMax = 0;
double avg = 0.0;
double avgTotal = 0.0;
double totalScore = 0;
double totalStudent = 0;
System.out.println("How many rows?");
n = scan.nextInt();
for (int row = 1; row <= n; row++) {
System.out.println("How many student in row " + row + "?");
rowsum=0;
c = scan.nextInt();
totalStudent += c;
for (int column = 1; column <= c; column++) {
System.out.println("Score for student " + column + " in row " + row + "?");
score = scan.nextInt();
rowsum += score;
totalScore += score;
if (score >= max) {
max = score;
columnMax = column;
}
if (score < min) {
min = score;
columnMin = column;
}
}
avg = rowsum/c;
if( avg >= averageMax){
averageMax=avg;
rowMaxNumber = row;
}
if( avg < averageMin){
averageMin=avg;
rowMinNumber = row;
}
}
avgTotal = totalScore/totalStudent;
System.out.println("Student " + columnMax + " of row " + rowMaxNumber + " was highest with " + max);
System.out.println("Student " + columnMin + " of row " + rowMinNumber + " was lowest with " + min);
System.out.println("Row " + rowMaxNumber + " had highest average with " + averageMax);
System.out.println("Row " + rowMinNumber + " had lowest average with " + averageMin);
System.out.println("Class average is " + avgTotal);
}
}
Sorry if I'm being bothersome. But I am stuck on another part of my HW problem. I am now tasked at instead of having an salesperson 0, salespersons start with 1. I have tried to solve this by parsing the strings i to an integer and adding 1. Seemed like it would work. However, for some reason the calculating of min is not correct. It stays at 0. I've tried stepping through the code and seeing why, but I cannot see it.
import java.util.Scanner;
public class Cray {
public static void main(String[] args){
final int SALESPEOPLE = 5;
int[] sales = new int[SALESPEOPLE];
int sum, randomValue, numGreater = 0;
int max = sales[0];
int min = sales[0];
int maxperson = 0;
int minperson = 0;
Scanner scan = new Scanner(System.in);
for (int i=0; i<sales.length; i++)
{
//To attempt print out the information of salesperson 0 and salesperson 1, I returned the integer value of i and added one.
System.out.print("Enter sales for salesperson " + Integer.valueOf(i+1) + ": ");
sales[i] = scan.nextInt();
//max if statemnent works fine and is correct
if(sales[i] > max){
max= sales[i];
maxperson = i;
}
//For some reason max is calculating but not min.
if(sales[i] < min){
min = sales [i];
minperson= i;
}
}
System.out.println("\nSalesperson Sales");
System.out.println("--------------------");
sum = 0;
for (int i=0; i<sales.length; i++)
{
System.out.println(" " + Integer.valueOf(i+1) + " " + sales[i]);
sum += sales[i];
}
System.out.println("\nTotal sales: " + sum);
System.out.println("Average sales: " + sum/5);
System.out.println();
System.out.println("Salesperson " + Integer.valueOf(maxperson+1) + " had the most sales with " + max );
System.out.println("Salesperson " + Integer.valueOf(minperson+1) + " had the least sales with " + min);
System.out.println();
System.out.println("Enter any value to compare to the sales team: ");
randomValue = scan.nextInt();
System.out.println();
for(int r=0; r < sales.length; r++){
if(sales[r] > randomValue){
numGreater++;
System.out.println("Salesperson " + Integer.valueOf(r+1) + " exceeded that amount with " + sales[r]);
}
}
System.out.println();
System.out.println("In total, " + numGreater + " people exceeded that value");
}
}
The problem is that the min is never set as all positive sales will be > 0
You should initialise min as a large value so that a positive sales figure can be less then the minimum value specified in your if statement:
if (sales[i] < min) {
You could use:
int min = Integer.MAX_VALUE;