How to use Scanner get store input in one integer

Viewed 136

How can I make it so the there will be the same number of questions as the user inputs the number of jobs. For example, lets say you had worked in 5 jobs, I want the program to ask the user to ask, Your salary for job 1 Job 2 Job 3 Job 4 Job 5, and so on. My code right now is.

 import java.util.Scanner;
public class Personal_Info {
public static void main(String[] args) {


     Scanner Scanner = new Scanner(System.in);

    int Salary1;
    int Salary2;
    int Salary3;
    int TotalJobs;
    int Average;
    String Name;
    int Largest;
    int Smallest;
    int Sum;


     System.out.print("What is your first name?: ");
    Name = Scanner.nextLine();

    System.out.print("How many jobs have you had?: ");
    TotalJobs = Scanner.nextInt();

    System.out.print("Enter income from job #1: ");
    Salary1 = Scanner.nextInt();

    System.out.print("Enter income from job #3: ");
    Salary2 = Scanner.nextInt();

    System.out.print("Enter income from job #3: ");
    Salary3 = Scanner.nextInt();
    Sum = Salary1 + Salary2 + Salary3;
    Average = Sum / TotalJobs;

     Largest = Salary1;
    Smallest = Salary1;

    if(Salary2 > Largest)
        Largest = Salary2;

    if(Salary3 > Largest)
        Largest = Salary3;

    if(Salary2 < Smallest)
        Smallest = Salary2;

    if (Salary3 < Smallest)
        Smallest = Salary3;

    System.out.println("Hello " + Name + "You've had " + TotalJobs + " jobs. " + "The highest paying job paid is " + Largest + ". The lowest paying job paid is "+ Smallest + ". The average is " + Average + ".");

but it wouldn't work cause it'll only ask the user three times, job 1, 2, and 3.

If I try,

for (int x = 0; x<TotalJobs; x++){ 
 System.out.print("How many jobs have you had?: "); 
 number = Scanner.nextInt();

I don't know how to get the highest/smallest/average from that stored value which would be 'number'.

3 Answers

Hope this will help You

    class SalaryCalculate{
    public static void main(String args[]){
    int totalJobs,jobcounter,maxSalary,minSalary,averageSalary=0;;
    int[] jobsincome;
    String name;
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter your name");
    name=sc.nextLine();
    System.out.println("How many jobs you had");
    totalJobs= sc.nextInt();
    jobsincome= new int[totalJobs];
    for(int i=0;i<totalJobs;i++){
        jobcounter=i+1;
        System.out.println("Enter the income for your job "+jobcounter);
        jobsincome[i]=sc.nextInt();
        averageSalary=averageSalary+jobsincome[i];
    }
    jobsincome=average(jobsincome);
    maxSalary=jobsincome[totalJobs-1];
    minSalary=jobsincome[0];
    averageSalary=averageSalary/totalJobs;
    System.out.println("Hi "+name+" your min salary is "+minSalary+" max salary is "+maxSalary+" average salary is "+averageSalary);
    }
    public static int[] average(int jobsincome[]){
        int length=jobsincome.length;
        for(int i=0;i<length;i++){
            for(int j=i+1;j<length;j++){
                if(jobsincome[i]>jobsincome[j]){
                    int temp=jobsincome[j];
                    jobsincome[j]=jobsincome[i];
                    jobsincome[i]=temp;
                }
            }
        }
        return jobsincome;
    }
    }

Use an array.

Also only capitalize classes, not variables

System.out.print("How many jobs have you had?: ");
int totalJobs = scanner.nextInt();
int[] salaries = new int[totalJobs];

// Loop here 
// System.out.print("Enter income from job #"+x);
// salaries[x] = scanner.nextInt();

With this list, you can get the length directly and use separate loops for the sum, min, max. Or streams

With the sum and length, find an average

use an integer arraylist to keep salaries and loop through inputs

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    ArrayList<Integer> sallaries = new ArrayList<>();
    int TotalJobs;
    double average;
    String name;
    int largest;
    int smallest;

    System.out.print("What is your first name?: ");
    name = input.nextLine();


    System.out.print("How many jobs have you had?: ");
    TotalJobs = input.nextInt();

    for (int i= 0; i<TotalJobs ; i++){
        System.out.print("Enter income from job #"+(i+1)+" : ");
        sallaries.add(input.nextInt());
    }

    average = sallaries.stream().mapToInt(Integer::intValue).average().orElse(-1);

    largest = Collections.max(sallaries);
    smallest = Collections.min(sallaries);


    System.out.println("Hello " + name + "You've had " + TotalJobs + " jobs. " + "The highest paying job paid is " + largest + ". The lowest paying job paid is "+ smallest + ". The average is " + average + ".");
} 
Related