Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Just need some help with my program ... i really dont know how to get the total to print in my for loop :(((((((((((((((((

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Just need some help with my program ... i really dont know how to get the total to print in my for loop :(((((((((((((((((

    Write a program to calculate the telephone charges for four customers. The data for
    each customer consists of
    NAME, PREVIOUS METER READING, PRESENT METER READING
    The difference between the two readings gives the number of units used by the
    customer for the period under consideration. The amount due for each customer is
    calculated by:
    UNITS USED * RATE PER UNIT + RENTAL CHARGE
    The rate per unit and rental charge is assumed to be the same for all customers and
    must be input once only.
    Your program must:
    a. Input the rate and rental charge
    b. Read the data for each customer and calculate the amount due
    c. Print the information under suitable headings
    d. Calculate and print the total amount due to the telephone company
    ************************************************** *************************


    java.jpg
    ************************************


    I just need the totalX in my for loop to print the total but the total i want to print it is in the while loop



    can someone help me plzzz would greatly be thankful for any help


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Just need some help with my program ... i really dont know how to get the total to print in my for loop :(((((((((((((((((

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    The graphic you posted is difficult to see. Read the above link and copy/paste your code between code or highlight tags as described there.

    If the total is being calculated, but it's local in scope to a while loop, then declare the variable that holds the total outside the while loop where it will be available to other parts of the program.

    Giving you more specific guidance will be easier after seeing the code.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Just need some help with my program ... i really dont know how to get the total to print in my for loop :(((((((((((((((((

    this is my java code.. plz feel free to make any changes ...

    Im new to this kind of programming so i will be thankful for any help i can get


    package Project;

    import java.io.*;
    public class Question3 {
    private static String amount;
    public static String total;
    String names=null;


    /**
    * @param args the command line arguments
    * @throws java.lang.Exception
    */
    public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String names [];//This is the declaration
    names = new String[5];//Create an array

    //While loop
    int x=0;
    while(true){

    System.out.println("Please enter client name");
    names [x] = br.readLine();
    //enter rate
    System.out.println("Please enter rate");
    float rate= Float.parseFloat(br.readLine());
    System.out.println("Please enter rental");
    //rental
    float rental = Float.parseFloat(br.readLine());
    //Prev meter reading
    System.out.println("Please enter previous meter reading: ");
    float prevMeter = Float.parseFloat(br.readLine());
    //Present meter reading
    System.out.println("Please enter present meter reading: ");
    float presMeter= Float.parseFloat(br.readLine());
    //Unit used
    float unitUsed =prevMeter-presMeter;
    //calculation of total amount
    float totalX = rental+(unitUsed*rate);
    System.out.println("$"+totalX+" is the amount due for "+names[x]);


    //ask to enter another
    String data;
    System.out.println("Would like to enter another client ");
    data = br.readLine();
    if(data.equals("no")){
    break;
    }//close if statement
    x++;


    }//end while loop
    //for loop
    for(int k=0; k<2; k++){
    System.out.println(totalX +" is the total amount for "+ names[x]);
    }



    }


    }

    --- Update ---

    I just want it to print like this for example :

    This is the amount for jack (amount of cash).
    This is the amount for jane (amount of cash)

Similar Threads

  1. for loop that calculates the total of a series of numbers
    By JessicaCouture95 in forum Loops & Control Statements
    Replies: 8
    Last Post: November 3rd, 2013, 07:38 PM
  2. [SOLVED] How Do I Obtain a Grand Total From My Loop
    By Norm in forum Loops & Control Statements
    Replies: 0
    Last Post: February 4th, 2013, 06:20 PM
  3. Program should display number of positives\negatives\total\average
    By alias22 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 21st, 2012, 03:21 PM
  4. [SOLVED] (Total begginner) simple GUI program nt displayin
    By moneyman021 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 10th, 2012, 06:26 PM
  5. program wont renew total
    By that_guy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 01:00 PM

Tags for this Thread