Joe Perkins
Joe Perkins

Reputation: 271

Calling a method from within an if/if else statement

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

Upvotes: 3

Views: 65678

Answers (5)

Philipp Sander
Philipp Sander

Reputation: 10249

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);
    }
}

Upvotes: 6

rahul maindargi
rahul maindargi

Reputation: 5655

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
        }

Upvotes: 1

Mika
Mika

Reputation: 1245

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

Upvotes: 2

rzymek
rzymek

Reputation: 9281

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'

Upvotes: 1

Alya'a Gamal
Alya'a Gamal

Reputation: 5638

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

    }

Upvotes: 1

Related Questions