Results 1 to 12 of 12
  1. #1
    SethLasky is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default How to keep variables through multiple methods

    I'm trying to figure out how to make it so the variables remain the same so that I can use the same variables in each method. I know I can declare them before the method but since I need to change them I was wondering if there is a more efficient way. Thank you for your help. Here is the code I am trying to do this with below.


    Java Code:
    import java.util.*;
    public class Test
    {
    
      public static void main(String [] args)
      {
           Scanner console = new Scanner(System.in);
        double amountDue = 0.0;
        double dayAmount = 0.0;
        double nightAmount = 0.0;
        String dayMinutes = null;
        String nightMinutes = null;
        String minutes = null;
        
        
        
           System.out.println("Enter your 10 Digit Account Number");
           String accountNumber = console.next();
           
           System.out.println(accountNumber);
        
           if (isNumeric(accountNumber) == true)
           { 
                System.out.println("Enter the type of service you have using the characters 'r' or 'R' for Regular Service and 'p' or 'P' for Premium Service");
                String typeOfService = console.next();
              if(typeOfService.matches("[pPrR]"))
                {
                if(typeOfService.matches("[rR]"))
                {
                  regularBill(typeofService);
                }
                else 
                 
                  premiumBill(typeofService);
              }
                   else
                     System.out.println("Invalid type of service");
           }
                     else 
                     System.out.println("Invalid account number");
      }
    public static void regularBill(String typeOfService1)            
    {            
                               
                     
                    typeOfService = "Regular";
                       System.out.println("How many minutes have you used?");
                       minutes = console.next();
                       if(minutes.matches("(\\d)(\\d)?(\\d)?(\\d)?(\\d)?(\\d)?"))
                       {
                         double minutes1=Double.parseDouble(minutes); 
                         if(minutes1 > 50)
                            amountDue = 10 + ((minutes1 - 50) *.10);
                         else
                             amountDue = 10;
                         System.out.printf("%s %s %n%s %s %n%s %s %n%s %.2f", "Account Number:", accountNumber, "Type of Service:", typeOfService, "Minutes used:", minutes, "Amount Due:$", amountDue);
                       }
                         else
                             System.out.println("Invalid Number of Minutes");
                         
                         
                         
                       
                       
                       
                  
                  
    }              
                  public static void premiumBill(String typeOfService1)
                 {
                     
                  
                  
                       
                       
                       typeOfService = "Premium";
                       System.out.println("How many minutes from 6:00 AM to 6:00 PM have you used?");
                        dayMinutes = console.next();
                       if(dayMinutes.matches("(\\d)(\\d)?(\\d)?(\\d)?(\\d)?(\\d)?"))
                       {
                         double dayMinutes1=Double.parseDouble(dayMinutes);
                         if(dayMinutes1 > 75)
                             dayAmount = (dayMinutes1 - 75) * .10;
                         else
                           dayAmount = 0.0;
                         System.out.println("How many minutes from 6:00 PM to 6:00 AM have you used?");
                       nightMinutes = console.next();
                       if(nightMinutes.matches("(\\d)(\\d)?(\\d)?(\\d)?(\\d)?(\\d)?"))
                       {
                          double nightMinutes1=Double.parseDouble(nightMinutes); 
                         if(nightMinutes1 > 100)
                         
                           
                          nightAmount = (nightMinutes1 - 100) * .05;
                         
                         else
                         {
                         nightAmount = 0.0;
                         amountDue = 25.0 + nightAmount + dayAmount;
                         double premMinutes= nightMinutes1 + dayMinutes1;
                         System.out.printf("%s %s %n%s %s %n%s %.0f %n%s%.2f", "Account Number:", accountNumber, "Type of Service:", typeOfService, "Minutes used:", premMinutes, "Amount Due:$", amountDue);
                         }
                          } 
                         else 
                         System.out.println("Invalid Number of Nighttime Minutes");
                       }
                       else 
                         System.out.println("Invalid Number of Daytime Minutes");
                    
           
              
                 }
                      
             
                   
      
                                                 
                  
                
              
                    
    public static boolean isNumeric(String accountNumber1)
    {
      if(accountNumber1.matches("(\\d{10})"))
        return true;
      else
        return false;
    }
    
         
    }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: How to keep variables through multiple methods

    Quote Originally Posted by SethLasky View Post
    I'm trying to figure out how to make it so the variables remain the same so that I can use the same variables in each method. I know I can declare them before the method but since I need to change them I was wondering if there is a more efficient way. Thank you for your help. Here is the code I am trying to do this with below.
    I'm a bit confused by your question. If you need to change the value of a variable, then you don't want the value of the variable to remain the same.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to keep variables through multiple methods

    Declaring them outside the method makes them a field of the class, and that's exactly what you want to do. The normal place to do it is at the top of the class, before the first method.
    Get in the habit of using standard Java naming conventions!

  4. #4
    SethLasky is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: How to keep variables through multiple methods

    Sorry, I'm very new at this so it's hard to be clear about what I'm trying to do. If you look at my code you'll see I'm trying to print an output of a bunch of variables that aren't in the method. Is there any way I could transfer variables from the main method to the other methods?

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to keep variables through multiple methods

    Either make them fields of the class, or pass them to the other methods as arguments.
    Get in the habit of using standard Java naming conventions!

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to keep variables through multiple methods

    BTW, your code is bizarrely indented and consequently very hard to read.
    Get in the habit of using standard Java naming conventions!

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: How to keep variables through multiple methods

    Quote Originally Posted by SethLasky View Post
    Is there any way I could transfer variables from the main method to the other methods?
    In that case, yes. As kjkrum said, you can pass them in as arguments of the method or make them fields of the class.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    SethLasky is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: How to keep variables through multiple methods

    Can anyone point me in the direction of a tutorial on how to do that?

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: How to keep variables through multiple methods

    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: How to keep variables through multiple methods

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: How to keep variables through multiple methods

    Quote Originally Posted by kjkrum View Post
    Either make them fields of the class
    That would be nice if everything wasn't static.

  12. #12
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to keep variables through multiple methods

    Quote Originally Posted by Junky View Post
    That would be nice if everything wasn't static.
    True that.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Multiple variables
    By spek in forum New To Java
    Replies: 5
    Last Post: 03-10-2012, 01:55 AM
  2. Calling Variables in Multiple Methods
    By PrimalScientist in forum New To Java
    Replies: 10
    Last Post: 02-07-2012, 11:26 AM
  3. Using variables in different methods
    By squirmytoad in forum New To Java
    Replies: 2
    Last Post: 01-10-2012, 09:04 PM
  4. using variables in multiple methods
    By cagipple in forum New To Java
    Replies: 2
    Last Post: 09-14-2011, 04:44 AM
  5. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 05:14 PM

Posting Permissions

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