# Thread: Declaring variables on the fly

1. Member
Join Date
Apr 2012
Posts
1
Rep Power
0

## Declaring variables on the fly

Hi,

I am learning java currently and very much new to it. I used to do a lot of shell scripting but I'm rather rusty at that too!

Anyway I have the following very basic script that I would like to do more with...

import java.util.Scanner;

// work out pay for 5 people, asking for their rate of pay and number of hours worked

class paySlips
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

double pay, hours, rate;
int person;
string emp1, emp2, emp3, emp4, emp5;

for (person =1; person <=5; person++)
{
System.out.println("How many hours worked for employee " +person + "?");
hours = input.nextDouble();
System.out.println("How much per hour?");
rate=input.nextDouble();
pay = (rate*hours);
System.out.println("Gross pay for employee" +person+ " is £"+pay);
emp(+person)=pay; // this is the bit where I'm trying to set the result of the calculation to an individual employee. And failed :)!

}
}
}

So what I'm trying to do is display gross pay for 5 employees. What I'd like to do is at the end of each loop, set the calculated pay agains the string (or double) of emp1 through to 5. The very basic attempt I have made clearly doesn't work and I was wondering what the best method of accomplishing this would be...

Thank you in advance for any help.

Simon

2. ## Re: Declaring variables on the fly

Variable names are defined when you type their names into the source with an editor. You can associate values with Strings by using a Map with the Mapkey = name & Mapvalue=value.

3. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Declaring variables on the fly

Or use an array.
An array of what, you would need to decide, but since 'pay' is a doubel then double[] might be a decent choice.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•