Calling a method from within an if/if else statement

51,059

Solution 1

You can of course call a method within an if or else block. But what you tried in your snippet is DECLARING a method within a block which is impossible.

fixed snippet:

Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);

if(choice == 1)
{
    this.theRegistry.addStudent(aStudent);
}

EDIT:

I think the code you want looks something like this:

public static void main(String[] args) {
    //some code
    Scanner in = new Scanner (System.in);
    char choice = in.next().charAt(0);

    if(choice == 1)
    {
        RegistryInterface.doAddStdent(student);
    }
    //some code
}

The RegistryInterface.java

public class RegistryInterface {
    private static void doAddStudent(Student aStudent) {
        this.theRegistry.addStudent(aStudent);
    }
}

Solution 2

Well you can.

Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);

if(choice == 1)
    this.theRegistry.addStudent(aStudent);
else if choice == 2)
    this.theRegistry.removeStudent(aStudent);
else
    System.out.println("Please enter a valid choice.");

Solution 3

Yes , create your method first , and then call them inside the if statement , Like this:

private static void doAddStudent(Student aStudent) 
        {
            this.theRegistry.addStudent(aStudent);
        }

then

 if(choice == 1)
    {
        doAddStudent(aStudent) ;////here you call the doAddStudent method

    }

Solution 4

In your code you're not just calling a method inside the if statement - you're trying to define a new method. And this is illegal.

I'm guessing you want something like this:

Scanner in = new Scanner (System.in);
char choice = in.next().charAt(0);
if(choice == '1') {
    this.theRegistry.addStudent(aStudent);
}

Also note that you were comparing char choise against an int 1. I suppose you want to compare against char '1'

Solution 5

Calling method is static

static TheRegistryClass theRegistry;
static void callingMethod(){
/// Some code here 
Scanner in = new Scanner (System.in);
    char choice = in.next().charAt(0);

    if(choice == 1)
    {
       doAddStudent(aStudent);
    }

//remaining code here 

}

The method called in if block Declard in same class but outside of calling method

 private static void doAddStudent(Student aStudent) 
        {
            theRegistry.addStudent(aStudent); // static methods do not have reference to this
        }

if Caller Method is non Static TheRegistryClass theRegistry; void callingMethod(){ /// Some code here Scanner in = new Scanner (System.in); char choice = in.next().charAt(0);

    if(choice == 1)
    {
       doAddStudent(aStudent);
    }

//remaining code here 

}



 private static void doAddStudent(Student aStudent) 
        {
            this.theRegistry.addStudent(aStudent); // this is optional
        }
Share:
51,059
Joe Perkins
Author by

Joe Perkins

amateur

Updated on July 05, 2022

Comments

  • Joe Perkins
    Joe Perkins almost 2 years

    Is it possible to call a method within an if statement, and then a separate method in an if else statement?

    I have created a scanner than reads keyboard input, and based on the option the user gives, a different method will be called. Can I say something along the lines of:

    Scanner in = new Scanner (System.in);
    char choice = in.next().charAt(0);
    
    if(choice == 1)
    {
        private static void doAddStudent(Student aStudent) 
        {
            this.theRegistry.addStudent(aStudent);
        }
    }
    

    any help would be much appreciated

  • Joe Perkins
    Joe Perkins about 11 years
    I see what you mean, but where would I declare the method? Outside of the if statement?
  • Philipp Sander
    Philipp Sander about 11 years
    within a class. if you are working with the main method: outside of the main methode
  • Joe Perkins
    Joe Perkins about 11 years
    when I type this out i get the error; "non-static variable this cannot be referenced from a static context"
  • Joe Perkins
    Joe Perkins about 11 years
    I'm not working with the main method yet, just within my RegistryInterface class. Can i declare and call the method within the RegistryInterface class, or do i have to call it from within the main method? Thanks for your help
  • rahul maindargi
    rahul maindargi about 11 years
    is callingMethod static in your code? static methods do not get this reference. the registry should be static too updated answer.
  • Alya'a Gamal
    Alya'a Gamal about 11 years
    You can create your method in any class you want , and call it in any class you want , Try this link to know How to create the Method
  • Joe Perkins
    Joe Perkins about 11 years
    we have been instructed for this assignment to put the main method in a different file; RegistryApp. RegistryInterface is just where we are declaring our methods.
  • Philipp Sander
    Philipp Sander about 11 years
    i updated my answer once again. i hope that helps. coudl you please update your question with more code and in which files the code is?
  • Alya'a Gamal
    Alya'a Gamal about 11 years
    @PhilippSander: i think that you need to create an object from RegistryInterface when you call it , Like : RegistryInterface r = new RegistryInterface (); r.doAddStdent(student); ????
  • Philipp Sander
    Philipp Sander about 11 years
    not if the method is static in the RegistryInterface class. but it is possible that assignment tells him to do so ;-) but i cant know that ;-) then your code would be right
  • Alya'a Gamal
    Alya'a Gamal about 11 years
    aha , i got it , sorry :)
  • Joe Perkins
    Joe Perkins about 11 years
    One more thing sorry, i have used the code you gave me, and i am getting one error in the RegistryApp, it cannot find the symbol aStudent within the line RegistryInterface.doAddStdent(student);
  • Philipp Sander
    Philipp Sander about 11 years
    you have to declare the student first. Student student = new Student();