Help with method calling

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3895 Views - Last Post: 24 July 2019 - 04:02 AM Rate Topic: -----

#1 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Help with method calling

Posted 15 July 2019 - 05:03 AM

Hey All, new to java here.

For some context, I am trying to write some code that will ask the user to select an option, and the once selected will ask how many tickets that they want to buy. example, they want to buy 5 adult tickets, so they would enter 2 and then enter 5. The code will tell them the cost and then ask if they want more tickets. Printing out the total at the end. Problem is I am unable to get the method calling right to call the method once they have inputted the number.. All I can think of is using if/else statements to call the right one that is called. But currently it will just run through all of them. Can I have some help to determine why it isn't working? it has worked before in a Caesar cipher that I wrote but not here :(/>

package zootickets;

import java.util.Scanner;
public class ZooTickets {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
       //Code goes here 
       
        Scanner input = new Scanner(System.in);
        
        System.out.println("@@@@@   Welcome to Zoos Victoria   @@@@@"); // Just a menu
        System.out.println("               MAIN MENU                ");
        System.out.println("Zoo has the following ticketing options:");
        System.out.println("          1 = Child (4-5 yrs)           ");
        System.out.println("          2 = Adult (18+ yrs)           ");
        System.out.println("          3 = Senior (60+ yrs)          ");
        System.out.println("Which option would you like to pick?");
        int ticketOption = input.nextInt();
        
        if (ticketOption == 1);{ //to go to child method
            ChildTicketTotal();}
            
        else if (ticketOption == 2);{ //to go to adult method
               AdultTicketTotal();}
        
        else if (ticketOption == 3);{ //to go to senior method
               SeniorTicketTotal();} 
        
    }
    static double ChildTicketTotal() {
        
       
        double Childtix = 18;
        Scanner input = new Scanner(System.in);
        int number_childtix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_childtix = input.nextInt();
        
        System.out.println("You are purchasing " + number_childtix + " child tickets at $" + Childtix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_childtix*Childtix);
            double childCost = number_childtix*Childtix;
        
        return childCost;
    }
    
    static double AdultTicketTotal() {
        
       
        double Adulttix = 36.00;
        Scanner input = new Scanner(System.in);
        int number_adulttix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_adulttix = input.nextInt();
        
        System.out.println("You are purchasing " + number_adulttix + " child tickets at $" + Adulttix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_adulttix*Adulttix);
            double adultCost = number_adulttix*Adulttix;
        
        return adultCost;
    }
    
    static double SeniorTicketTotal() {
        
       
         double Seniortix = 32.50;
        Scanner input = new Scanner(System.in);
        int number_seniortix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_seniortix = input.nextInt();
        
        System.out.println("You are purchasing " + number_seniortix + " child tickets at $" + Seniortix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_seniortix*Seniortix);
            double seniorCost = number_seniortix*Seniortix;
        
        return seniorCost;
    }
    
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with method calling

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Help with method calling

Posted 15 July 2019 - 05:10 AM

Can you copy the contents of the console window and paste it here so we can see what you are talking about? Add some comments that describe what is wrong and show what you want to be different.

One suggestion is to add an else at the end of the if/else if chain that prints a message when none of the above if statements were true and shows what the unrecognized value was.

Another option would be to use a switch statement in place of the if/else if statements.

This post has been edited by NormR: 15 July 2019 - 05:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Re: Help with method calling

Posted 15 July 2019 - 05:17 AM

View PostNormR, on 15 July 2019 - 05:10 AM, said:

Can you copy the contents of the console window and paste it here so we can see what you are talking about? Add some comments that describe what is wrong and show what you want to be different.



Currently the console prints as follows:

run:
@@@@@   Welcome to Zoos Victoria   @@@@@
               MAIN MENU                
Zoo has the following ticketing options:
          1 = Child (4-5 yrs)           
          2 = Adult (18+ yrs)           
          3 = Senior (60+ yrs)          
Which option would you like to pick?
5
Enter the number of tickets you would like to purchase:
5
You are purchasing 5 child tickets at $18.0 each!
Press 1 to confirm purchase
1
Total Amount for Child tickets: $90.0
Enter the number of tickets you would like to purchase:
1
You are purchasing 1 child tickets at $36.0 each!
Press 1 to confirm purchase
1
Total Amount for Child tickets: $36.0
Enter the number of tickets you would like to purchase:
1
You are purchasing 1 child tickets at $32.5 each!
Press 1 to confirm purchase
1
Total Amount for Child tickets: $32.5
BUILD SUCCESSFUL (total time: 21 seconds)



So it looks like it is calling each method one after another no matter what the input actually is. Rather then only executing the option selected, but keeping the text of ChildTicketTotal(), but the variables of the following methods????

import java.util.Scanner;
public class ZooTickets {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
       //Code goes here 
       
        Scanner input = new Scanner(System.in);
        
        System.out.println("@@@@@   Welcome to Zoos Victoria   @@@@@"); // Just a menu
        System.out.println("               MAIN MENU                ");
        System.out.println("Zoo has the following ticketing options:");
        System.out.println("          1 = Child (4-5 yrs)           ");
        System.out.println("          2 = Adult (18+ yrs)           ");
        System.out.println("          3 = Senior (60+ yrs)          ");
        System.out.println("Which option would you like to pick?");
        int ticketOption = input.nextInt();
        
        if (ticketOption == 1);{ //to go to child method
            ChildTicketTotal();}
            
        if (ticketOption == 2);{ //to go to adult method
               AdultTicketTotal();}
        
        if (ticketOption == 3);{ //to go to senior method
               SeniorTicketTotal();} 
        
    }
    static double ChildTicketTotal() { //only to be run if 1 is selected
        
       
        double Childtix = 18;
        Scanner input = new Scanner(System.in);
        int number_childtix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_childtix = input.nextInt();
        
        System.out.println("You are purchasing " + number_childtix + " child tickets at $" + Childtix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_childtix*Childtix);
            double childCost = number_childtix*Childtix;
        
        return childCost; //to return the total cost of the tickets to be added to a total after all tickets are confirmed 
    }
    
    static double AdultTicketTotal() { //only to be run if 2 is selected
        
       
        double Adulttix = 36.00;
        Scanner input = new Scanner(System.in);
        int number_adulttix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_adulttix = input.nextInt();
        
        System.out.println("You are purchasing " + number_adulttix + " child tickets at $" + Adulttix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_adulttix*Adulttix);
            double adultCost = number_adulttix*Adulttix;
        
        return adultCost; //to return the total cost of the tickets to be added to a total after all tickets are confirmed 
    }
    
    static double SeniorTicketTotal() {  //only to be run if 3 is selected
        
       
         double Seniortix = 32.50;
        Scanner input = new Scanner(System.in);
        int number_seniortix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_seniortix = input.nextInt();
        
        System.out.println("You are purchasing " + number_seniortix + " child tickets at $" + Seniortix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_seniortix*Seniortix);
            double seniorCost = number_seniortix*Seniortix;
        
        return seniorCost; //to return the total cost of the tickets to be added to a total after all tickets are confirmed 
    }
    
}



I have added some comments. Sorry I am really new to this and just trying to wrap my head around why it wont work.
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Help with method calling

Posted 15 July 2019 - 05:24 AM

You should get a compiler warning when you compile the code

Quote

warning: [empty] empty statement after if

Be sure the compiler is using the -Xlint option to get that warning.

Another issue with the code is that the values returned by the methods are not saved in a variable.

This post has been edited by NormR: 15 July 2019 - 05:25 AM

Was This Post Helpful? 1
  • +
  • -

#5 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Re: Help with method calling

Posted 15 July 2019 - 05:27 AM

I'm using Netbeans and I'm not getting that error.. Even if I did the above would that fix the code?
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Help with method calling

Posted 15 July 2019 - 05:30 AM

I get this message with the commandline I use to compile similar code:
Running: C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe -cp . -Xlint -Xdiags:verbose TestCode25.java

TestCode25.java:944: warning: [empty] empty statement after if
      if(2 == 1); {int x  = 3;}
                ^
1 warning

1 error(s)


I don't use Netbeans and can not recommend what settings are needed for it to find those errors.

This post has been edited by NormR: 15 July 2019 - 05:32 AM

Was This Post Helpful? 1
  • +
  • -

#7 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,718
  • Joined: 20-September 08

Re: Help with method calling

Posted 15 July 2019 - 05:35 AM

You have semicolons immediately after your if statements. This will end the statements abruptly
Was This Post Helpful? 1
  • +
  • -

#8 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Re: Help with method calling

Posted 18 July 2019 - 03:36 AM

View Postg00se, on 15 July 2019 - 05:35 AM, said:

You have semicolons immediately after your if statements. This will end the statements abruptly



Awesome! That worked!

import java.util.Scanner;
public class ZooTickets {

    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
       //Code goes here 
       
        Scanner input = new Scanner(System.in);
        
        System.out.println("@@@@@   Welcome to Zoos Victoria   @@@@@"); // Just a menu
        System.out.println("               MAIN MENU                ");
        System.out.println("Zoo has the following ticketing options:");
        System.out.println("          1 = Child (4-5 yrs)           ");
        System.out.println("          2 = Adult (18+ yrs)           ");
        System.out.println("          3 = Senior (60+ yrs)          ");
        System.out.println("Which option would you like to pick?");
        int ticketOption = input.nextInt();
        
        if (ticketOption == 1){ //to go to child method
            ChildTicketTotal();}
            
        else if (ticketOption == 2){ //to go to adult method
               AdultTicketTotal();}
        
        else if (ticketOption == 3){ //to go to senior method
               SeniorTicketTotal();} 
        
    }
    static double ChildTicketTotal() { //only to be run if 1 is selected
        
       
        double Childtix = 18;
        Scanner input = new Scanner(System.in);
        int number_childtix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_childtix = input.nextInt();
        
        System.out.println("You are purchasing " + number_childtix + " child tickets at $" + Childtix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_childtix*Childtix);
            double childCost = number_childtix*Childtix;
            
                System.out.println("Would you like to continue?");
        
        return childCost; //to return the total cost of the tickets to be added to a total after all tickets are confirmed 
    }
    
    static double AdultTicketTotal() { //only to be run if 2 is selected
        
       
        double Adulttix = 36.00;
        Scanner input = new Scanner(System.in);
        int number_adulttix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_adulttix = input.nextInt();
        
        System.out.println("You are purchasing " + number_adulttix + " child tickets at $" + Adulttix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_adulttix*Adulttix);
            double adultCost = number_adulttix*Adulttix;
            
            System.out.println("Would you like to continue?");
             
                 
        
        return adultCost; //to return the total cost of the tickets to be added to a total after all tickets are confirmed 
        
            
    }
    
    static double SeniorTicketTotal() {  //only to be run if 3 is selected
        
       
         double Seniortix = 32.50;
        Scanner input = new Scanner(System.in);
        int number_seniortix;
        
        System.out.println("Enter the number of tickets you would like to purchase:");
        number_seniortix = input.nextInt();
        
        System.out.println("You are purchasing " + number_seniortix + " child tickets at $" + Seniortix + " each!");
        
        System.out.println("Press 1 to confirm purchase");
        int answer = input.nextInt();
        
        if (answer == 1)
           System.out.println("Total Amount for Child tickets: $" + number_seniortix*Seniortix);
            double seniorCost = number_seniortix*Seniortix;
            
            System.out.println("Would you like to continue?");
        
        return seniorCost; //to return the total cost of the tickets to be added to a total after all tickets are confirmed 
    }
    
}



Now I have my second problem.. I Need the program to loop back around if the user wants to buy more tickets. So if they chose child tickets the first time and now they want to buy adult tickets. So it needs to just load the program up again. Once they have confirmed all the tickets that they want, the program will give the total cost of all tickets selected. I am really stuck and I don't know how to do it :( Can someone point me in the right direction??
Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Help with method calling

Posted 18 July 2019 - 03:51 AM

Quote

Need the program to loop back around

Wrap the code that you want to repeat inside of a while loop.
Ask the user if he wants to exit the loop.
Don't exit the loop until the user says to.
Was This Post Helpful? 1
  • +
  • -

#10 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Re: Help with method calling

Posted 19 July 2019 - 09:26 PM

View PostNormR, on 18 July 2019 - 03:51 AM, said:

Quote

Need the program to loop back around

Wrap the code that you want to repeat inside of a while loop.
Ask the user if he wants to exit the loop.
Don't exit the loop until the user says to.



Awesome! Thank you heaps.

But that wouldn't work the way that I wanted it to. Method's are a bit out of my reach atm though.. So I changed my code a bit.

package zoov2;


import java.util.Scanner;
public class ZooV2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         Scanner input = new Scanner(System.in);
         
         double childTicket = 18.00;
         double adultTicket = 36.00;
         double seniorTicket = 32.50;
         double totalChildPrice;
         double totalAdultPrice;
         double totalSeniorPrice;
         int numChildTicket;
         int numAduldTicket;
         int numSeniorTicket;
         int confirmPurchase1;
         int confirmPurchase2;
         int confirmPurchase3;
         
        do {
        System.out.println("@@@@@   Welcome to Zoos Victoria   @@@@@"); 
        System.out.println("               MAIN MENU                ");
        System.out.println("Zoo has the following ticketing options:");
        System.out.println("          1 = Child (4-5 yrs)           ");
        System.out.println("          2 = Adult (18+ yrs)           ");
        System.out.println("          3 = Senior (60+ yrs)          ");
        System.out.println("Which option would you like to pick?");
        int ticketOption = input.nextInt();
        
        if (ticketOption == 1){ //to go to child method
            System.out.println("Enter the number of tickets:");
              numChildTicket = input.nextInt();
              System.out.println("You are purchasing " + numChildTicket + " Child tickets at $18 each");
              System.out.println("Press one to confirm");
                confirmPurchase1 = input.nextInt();
                if (confirmPurchase1 == 1){
                    totalChildPrice = numChildTicket * childTicket;
                    System.out.println("Total amount for Child ticket is $" +totalChildPrice);
                }
                else{ 
                    System.out.println("Invalid input");
                }
        if (ticketOption == 2){ //adult ticlets
            System.out.println("Enter the number of tickets:");
              numAduldTicket = input.nextInt();
              System.out.println("You are purchasing " + numAduldTicket + " adult tickets at $36 each");
              System.out.println("Press one to confirm");
                confirmPurchase2 = input.nextInt();
                if (confirmPurchase2 == 1){
                    totalAdultPrice = numAduldTicket * adultTicket;
                    System.out.println("Total amount for Child ticket is $" +totalAdultPrice);
                }
                else{ 
                    System.out.println("Invalid input");    

    }
                if (ticketOption == 3){ //senior ticlets
            System.out.println("Enter the number of tickets:");
              numSeniorTicket = input.nextInt();
              System.out.println("You are purchasing " + numSeniorTicket + " senior tickets at $32.50 each");
              System.out.println("Press one to confirm");
                confirmPurchase3 = input.nextInt();
                if (confirmPurchase3 == 1){
                    totalSeniorPrice = numSeniorTicket * seniorTicket;
                    System.out.println("Total amount for Child ticket is $" +totalSeniorPrice);
                }
                else{ 
                    System.out.println("Invalid input"); }
                }
                System.out.println("Do you wish to continue (y/n)");
                char continueSelection = input.next().charAt(0); 
                
        }while(continueSelection == 'y') { //Givving error cannot find symbol, why?
            
            double totalAmountPayable = totalChildPrice + totalAdultPrice + totalSeniorPrice; //saying that my variables haven't been initialized when they have been through out the code? 

       System.out.println("Total amount payable today is: $" +totalAmountPayable);

       System.out.println("Have a Nice Day!");
        }
        }
        }



I'm getting errors on line 88 an 90.. I have marked them in comments. I'm sorry if I am being annoying but I just want to get this last assignment done.

Thank you all for your help.

View PostMrNewToJava, on 19 July 2019 - 09:23 PM, said:

I'm getting errors on line 88 an 90.. I have marked them in comments. I'm sorry if I am being annoying but I just want to get this last assignment done.

Thank you all for your help.


Sorry, line 80 and 82.
Was This Post Helpful? 0
  • +
  • -

#11 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Help with method calling

Posted 20 July 2019 - 03:51 AM

16
         double seniorTicket = 32.50;
17
         double totalChildPrice;


Line 16 has been initialized. Line 17 has not.

Declare continueSelection outside of the loop.

This post has been edited by NormR: 20 July 2019 - 03:51 AM

Was This Post Helpful? 1
  • +
  • -

#12 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,718
  • Joined: 20-September 08

Re: Help with method calling

Posted 20 July 2019 - 03:54 AM

Quote

//saying that my variables haven't been initialized when they have been through out the code?

I can see several doubles that haven't
Was This Post Helpful? 1
  • +
  • -

#13 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Re: Help with method calling

Posted 22 July 2019 - 04:56 AM

View PostNormR, on 20 July 2019 - 03:51 AM, said:

16
         double seniorTicket = 32.50;
17
         double totalChildPrice;


Line 16 has been initialized. Line 17 has not.

Declare continueSelection outside of the loop.


the totalChildPrice variable gets initialized on line 44.. or am I missing something? I thought that that works because it is getting assigned a value there? because the value isn't fixed.
Was This Post Helpful? 0
  • +
  • -

#14 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,718
  • Joined: 20-September 08

Re: Help with method calling

Posted 22 July 2019 - 05:13 AM

Quote

.. or am I missing something?


Probably ;) There's really no mystery here - all variables local to a method (your case) should be given an initial value. Just make sure they all are. Floating point to 0.0 to hold calculation results
Was This Post Helpful? 1
  • +
  • -

#15 MrNewToJava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-July 19

Re: Help with method calling

Posted 22 July 2019 - 05:40 AM

View Postg00se, on 22 July 2019 - 05:13 AM, said:

Quote

.. or am I missing something?


Probably ;)/> There's really no mystery here - all variables local to a method (your case) should be given an initial value. Just make sure they all are. Floating point to 0.0 to hold calculation results



So even if I declare them at the start, they can still be changed? I was under the impression that they can't be
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2