Jump to content

Looping issues and when calling other method, string-double in same line wont work


sugafree

Recommended Posts

Basically what I am trying to do is, first you enter a number, which will be the number of time the loop should run. When all details entered, it should print out like a payslip for all entries, name, wage, marital status etc..Now the problem is, when I enter example 2 for the loop(numWorkers) at the last round, it will come up with some error in the line of

nameWorker[i] = input.next();
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20. Also got some problem with the wage obviously im trying to send a double to the wageCalc method, but just cant get it running. Tried plenty of different versions. Anyone could help me on this please?

 

import java.util.Scanner; public class morePay2 { public static double wageCalc(double ratE, double hourS){return (ratE * hourS);} public static void main(String[] args) { Scanner input = new Scanner(System.in); int numWorkers = 0, i = 1; double intWorkedHours = 0, intHourlyRate = 0;  System.out.print("Please type in the number of workers: "); numWorkers = input.nextInt();String[] nameWorker = new String[numWorkers];String[] hourlyRate = new String[numWorkers];String[] workedHours = new String[numWorkers];String[] maritalStat = new String[numWorkers];String[] wage = new String[numWorkers]; for (i = 1; i <= numWorkers; i++){System.out.print("Please type in the name: ");nameWorker[i] = input.next();System.out.print("Please type in hourly rate: ");hourlyRate[i] = input.next();System.out.print("Please type in worked hours: ");workedHours[i] = input.next();System.out.print("Please type in marital status(married/single): ");maritalStat[i] = input.next();intHourlyRate = Double.parseDouble(hourlyRate[i]);intWorkedHours = Double.parseDouble(workedHours[i]);wage[i] = wageCalc(intHourlyRate, intWorkedHours);} for (i = 1; i <= numWorkers; i++){System.out.println(nameWorker[i] + " has earned " + wage[i] );}  } } 
Link to comment
Share on other sites

  • 2 weeks later...

 

Try...

for (i = 0; i < numWorkers; i++){

 

Yes, and it would also make sense to be carefull with the variable naming. Notice you are using 'i' multiple times. That can lead to problems.

Try to read about the scope of variables and naming convention. It will make your code more readable.

Edited by panodil
Link to comment
Share on other sites

Here is a working solution:

 

Please notice I changed the array type from string to double

import java.util.Scanner; public class morePay2 { public static double wageCalc(double ratE, double hourS){return (ratE * hourS);} public static void main(String[] args) { Scanner input = new Scanner(System.in); int numWorkers = 0, i = 1; double intWorkedHours = 0, intHourlyRate = 0;  System.out.print("Please type in the number of workers: "); numWorkers = input.nextInt();String[] nameWorker = new String[numWorkers];String[] hourlyRate = new String[numWorkers];String[] workedHours = new String[numWorkers];String[] maritalStat = new String[numWorkers];double[] wage = new double[numWorkers]; for (i = 0; i < numWorkers; i++){System.out.print("Please type in the name: ");nameWorker[i] = input.next();System.out.print("Please type in hourly rate: ");hourlyRate[i] = input.next();System.out.print("Please type in worked hours: ");workedHours[i] = input.next();System.out.print("Please type in marital status(married/single): ");maritalStat[i] = input.next();intHourlyRate = Double.parseDouble(hourlyRate[i]);intWorkedHours = Double.parseDouble(workedHours[i]);wage[i] = wageCalc(intHourlyRate, intWorkedHours);} for (i = 0; i < numWorkers; i++){System.out.println(nameWorker[i] + " has earned " + wage[i] );}

Result:

Please type in the number of workers: 1Please type in the name: JohnPlease type in hourly rate: 100Please type in worked hours: 37Please type in marital status(married/single): marriedJohn has earned 3700.0

Check with more workers:

Please type in the number of workers: 2Please type in the name: JohnPlease type in hourly rate: 100Please type in worked hours: 37Please type in marital status(married/single): marriedPlease type in the name: LonePlease type in hourly rate: 50Please type in worked hours: 37Please type in marital status(married/single): marriedJohn has earned 3700.0Lone has earned 1850.0
Edited by panodil
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...