4

I am just trying to call the methods to the main for each switch when it happens, but i just get the error message everytime i try to call any methods, not trying to return anything. ex. if the user enters a or A i want to call the add method to main

public static void main(String[] args) 
{ 
    char character; 

    Scanner keyboard = new Scanner(System.in); 

    while (character != 'E' || character != 'e') 
    {

    System.out.println(" A:Addition \n S:Subtraction \n M:Multiplication \n D:Division \n R:Modulus \n E:exit");
    switch (character)
    {
        case 'a':
        case 'A':
            System.out.println("your choice A");
            add(); 
            break; 

        case 's':
        case 'S':
            System.out.println("your choice S");
            subtraction();
            break; 

        case 'm':
        case 'M':
             System.out.println("your choice M");
             multiplication();
             break; 

        case 'd':
        case 'D':
             System.out.print("your choice D");
             division();
             break;

        case 'r':
        case 'R':
             System.out.println("your choice R");
             modulus();
             break;

        default: 
             System.out.println("Error: please enter a valid letter");
             break;

    } 
   }
 } 
public static void add(Scanner keyboard) 
{
    int a,b; 
    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();

     int total = a + b; 
    System.out.println(a + "plus" + b + "is" + total );
}
public static void subtraction(Scanner keyboard) 
{
    int a,b;

    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();
     int total = a-b;
    System.out.println(a + "minus" + b + "is " + total);
}
public static void multiplication(Scanner keyboard) 
{
    int a,b; 
    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();

    int total = a*b; 
    System.out.println(a + "times" + b + "is " + total);
}
public static void division(Scanner keyboard) 
{
    int a,b;

    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();
    int total = a/b;
    System.out.println(a + "divided" + b + "is " + total);
}
public static void modulus(Scanner keyboard) 
{
    int a,b;

    //get integer 1
    System.out.println("enter integer 1");
    a = keyboard.nextInt (); 

    //get integer 2
    System.out.println("enter integer 2"); 
    b = keyboard.nextInt();
    int total= a%b; 
    System.out.println(a + "modulus" + b + "is " + total);

    System.out.println("The program is terminating");
 }

}

2
  • What line are you getting this error on? Commented Feb 11, 2015 at 3:05
  • 1
    you define add to take in a Scanner object, but call it without any arguments... Commented Feb 11, 2015 at 3:05

4 Answers 4

1

you're calling the method but you didn't include an argument

take a look at this.

public static void add(Scanner keyboard) 

you have an argument, so you must include an argument when calling this method

so

you must call the method like this.

add(keyboard);
Sign up to request clarification or add additional context in comments.

Comments

0

You have defined the method which takes Scanner as argument but you are calling the methods with no args.

Comments

0

All the method you are using are supposed to receive a Scanner object while you pass no argument.

For example you call add(); while it signature is

public static void add(Scanner keyboard)

Which is why you get the error.

Instead, use add(keyboard) and repeat the same for substraction, multiplication, division and modulus methods.

So that your switch would now look like

switch (character) {
    case 'a':
    case 'A':
        System.out.println("your choice A");
        add(keyboard);
        break;

    case 's':
    case 'S':
        System.out.println("your choice S");
        subtraction(keyboard);
        break;

    case 'm':
    case 'M':
        System.out.println("your choice M");
        multiplication(keyboard);
        break;

    case 'd':
    case 'D':
        System.out.print("your choice D");
        division(keyboard);
        break;

    case 'r':
    case 'R':
        System.out.println("your choice R");
        modulus(keyboard);
        break;

    default:
        System.out.println("Error: please enter a valid letter");
        break;

}

Comments

0

You are missing the arguments in the method call.

 case 'a':
    case 'A':
        System.out.println("your choice A");
        add(keyboard); // Add arguments.
        break;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.