-2

I am in the process of writing code that asks the user for their name, how many jobs they have, and the income of those jobs. Then the code finds the highest and lowest paying incomes, and the average of the jobs they entered.

Im having issues with the highest and lowest paying portion, along with finding the average, while still maintaining what the user entered in order to recite it later.

Ex: Inputs: 10000 30000 50000 "Hello Audrey. You have had 3 jobs. The highest paying job paid $50000. The lowest paying job paid $10000. The average pay for the jobs entered is $30000

**** heres the code I have edited, but it is not running properly. I believe it has to do with int and double. Im not sure which code should be double and which ones should be int.****

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class JobIncome {


public static void main(String[] args) throws FileNotFoundException {

Scanner input = new Scanner (System.in);
System.out.println("What is your first name? ");
String firstName = input.nextLine();

Scanner scan = new Scanner (System.in);
System.out.println("How many jobs have you had? ");
int jobNum = scan.nextInt();

//Declarations
int total = 0;
int average = 0;

//for loop asks for the incomes of the user's previous 
//jobs and stores them into an array
int arrayOfIncomes[] = new int[jobNum];
for(int i = 1; i <= arrayOfIncomes.length; i++){
    System.out.println("Enter the income of job #" + i + " : ");
    arrayOfIncomes[i] = scan.nextInt();
    total = total + arrayOfIncomes[i];
}

average = total/jobNum;

//Start of the code that will find the min and max
int min = arrayOfIncomes[0];
int max = arrayOfIncomes[0];

for (int i = 1; i < arrayOfIncomes.length; i++) {
    if (arrayOfIncomes[i] > max) {
        max = arrayOfIncomes[i];
    }
}

for (int i = 1; i < arrayOfIncomes.length; i++) {
    if (arrayOfIncomes[i] < min) {
        min = arrayOfIncomes[i];
    }
}

//Print statement that gives the user all their information
System.out.println("Hello, " + firstName + ". You have had" + jobNum + 
"jobs. The highest paying job paid $" + max + 
". The lowest paying job paid $" + min + 
". The average pay for the " + jobNum + "jobs entered is $" + average + ".");


//Prompt asking the user if they would like to print their info into a text file
System.out.println("Would you like to output your information into a text file, yes or no? ");
String yesNo = input.nextLine();
if(yesNo.equals("yes")){
    System.out.println("");
} else {
    System.out.println("Goodbye.");
}


//Output code
Scanner console = new Scanner(System.in);
System.out.print("Your output file: ");
String outputFileName = console.next();
PrintWriter out = new PrintWriter(outputFileName);


//This code prints the information into the output text file
out.print("Hello, " + firstName + ". You have had" + jobNum + 
"jobs. The highest paying job paid $" + max + 
". The lowest paying job paid $" + min + 
". The average pay for the " + jobNum + "jobs entered is $" + average + ".");

out.close();
}
Aud
  • 5
  • 5

2 Answers2

0

Create an int variable sum. Add the sum as you get input from the user then divide the sum with the variable jobNum while casting at least one variable to double.

Example:

double ave = (double) sum / jobNum;
Leopold Joy
  • 4,524
  • 4
  • 28
  • 37
cemnura
  • 11
  • 2
  • I added more to the code and the problem I am having now is line 27 where is reads arrayOfIncomes[i] = scan.nextInt(); it says that the array index is out of bounds – Aud Dec 05 '17 at 03:05
  • Due to the Array index starts from zero your last element index will be 15. Where as the arrayOfIncomes.length returns 16 therefore your are trying to access a array element that is not there. Change your loop statements to for(int i = 0; i < arrayOfIncomes.length; i++){//doStuff} which is a classic way to iterate threw a array.Please check this helpful link https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – cemnura Dec 05 '17 at 06:51
0

Fix the code by instantiating an array, jobIncomes with the length equivalent to the number of jobs. You could keep a running average, but you would lose precision when rounding. You will also need to instantiate three integer variables: total, max and min, each at zero.

For every iteration of the loop, you should add the following code:

jobIncomes[i-1]=s.nextInt();
if (jobIncomes[i-1]<min)
{ min=jobIncomes[i-1];}
if (jobIncomes[i-1]>max
{max=jobIncomes[i-1];}
total+=jobIncomes[i-1];

When you print the average, you can cast the formula average = total/jobNum. You might also want to change the array values to read from 0 to jobNum-1, if you want to index simply by i in the if statements.