Results 1 to 3 of 3
  1. #1
    sihyde is offline Member
    Join Date
    Apr 2012
    Rep Power

    Default Declaring variables on the fly


    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(;

    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?");
    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.


  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Question about declaring variables
    By salmontres in forum New To Java
    Replies: 2
    Last Post: 12-18-2011, 12:58 AM
  2. Why declaring Swing variables in longform
    By africanhacker in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 09:35 AM
  3. Declaring variables
    By Flashmatix in forum New To Java
    Replies: 12
    Last Post: 01-20-2009, 03:43 PM
  4. Declaring an ArrayList
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-31-2008, 07:36 PM
  5. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 12:11 AM

Posting Permissions

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