-1

I'm sorry if this is a weird question but I've just started OOP and ran across this problem for a simple menu driven math program that I was supposed to make. I cleared all the errors the compiler gave me but now it's given me about 14 fresh errors most of which are described as 'cannot find symbol.' Here's my code:

import java.util.Scanner;


public class MathMenu
{


//MENU METHOD
private static void menu(String args[])
{
int choice;

System.out.printf("Enter '1' to add");
System.out.printf("Enter '2' to subtract");
System.out.printf("Enter '3' to exit");

System.out.printf("\nPlease enter your choice: ");


choice=input.nextInt();

if (choice==1)
sum(n,m);

if (choice==2)
dif(n,m);

else if(choice==3)
return;

}



//SUM
private static int sum(int a, int b)
{
return n+m;
}


//DIFFERENCE 
private static int dif(int a, int b)
{
if(n<m)
return m-n;

else
return n-m;
}





public static void main(String args[])
{


int n=15;
int m=8;

Scanner input = new Scanner(System.in);

menu();

}


}

And here's the NEW compiler output:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Shahraiz Tabassam>cd c:\java\bin

c:\java\bin>javac MathMenu.java
MathMenu.java:7: error: no suitable constructor found for Scanner()
private static Scanner input = new Scanner();
                               ^
    constructor Scanner.Scanner(ReadableByteChannel,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(ReadableByteChannel) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path,Charset) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Path) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File,CharsetDecoder) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(File) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(InputStream,String) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(InputStream) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Readable) is not applicable
      (actual and formal argument lists differ in length)
    constructor Scanner.Scanner(Readable,Pattern) is not applicable
      (actual and formal argument lists differ in length)
MathMenu.java:64: error: method menu in class MathMenu cannot be applied to give
n types;
menu();
^
  required: String[]
  found: no arguments
  reason: actual and formal argument lists differ in length
2 errors

c:\java\bin>
Shahraiz T.
  • 338
  • 4
  • 13
  • Where are you definining the `m` and `n` variable that you are using in the method called `menu`? – João Silva Sep 17 '12 at 15:04
  • I used m and n because I thought I could pass their values to the methods regardless of their names as long as the parameter types were defined in the method structure. – Shahraiz T. Sep 17 '12 at 15:15

3 Answers3

1

All your functions get arguments named a & b, but work with n & m. Change one of those. For example:

private static int sum(int n, int m)
{
    return n+m;
}
MByD
  • 135,866
  • 28
  • 264
  • 277
  • Also, @Nambari's answer is complementary to mine (+1). – MByD Sep 17 '12 at 15:06
  • Did that and it helped narrow down the errors to 6. Some of them are still related to symbols not being found, and a stray one says that the actual and formal arguments lists differ in length. – Shahraiz T. Sep 17 '12 at 15:08
1

You didn't define input in your program, but calling

choice=input.nextInt();

Assuming you want to get input from user, you need to have

Scanner input = new Scanner(System.in)  

right before choice=input.nextInt();

kosa
  • 65,990
  • 13
  • 130
  • 167
1

You never defined your input variable in the body of the menu method. Try adding Scanner input = new Scanner(System.in) within the menu method. Simply defining the variable in main does not give menu access to it. If you want to avoid creating a Scanner instance multiple times, you could do something like

import java.util.Scanner;

public class MathMenu {
    private static Scanner input = new Scanner(System.in);
    ...
}

Then you could use input from all of your methods.


EDIT: I just noticed something similar for m and n: you have to define them within the method in which they are being used, or make them static fields. If it was up to me I'd do it like this:
import java.util.Scanner;

public class MathMenu {
    private static Scanner input = new Scanner(System.in);
    private static int n = 15;
    private static int m = 8;

    // ...
    // your other methods unchanged
    // ...

    public static void main(String[] args) {
        menu(args);  // or just "menu()" if you remove the arguments from the menu method declaration.
    }
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Going to try that right away. Thank you! – Shahraiz T. Sep 17 '12 at 15:07
  • I think you're onto something. Could you put up a code sample? – Shahraiz T. Sep 17 '12 at 15:20
  • That was very effective bro. That helped narrow it down to two errors - I've updated the compiler output again. If you could have a look at that. That would be very helpful! – Shahraiz T. Sep 17 '12 at 16:11
  • My mistake, forgot to add the `System.in` argument to the `Scanner` constructor and `menu` method argument - see the edit. Should all work now! :-P – arshajii Sep 17 '12 at 16:19
  • Ah the constructor! :) Here's what it's showing now: c:\java\bin>javac MathMenu.java MathMenu.java:64: error: method menu in class MathMenu cannot be applied to give n types; menu(); ^ required: String[] found: no arguments reason: actual and formal argument lists differ in length 1 error c:\java\bin> What does that mean? Formal and actual argument lists differ in length? – Shahraiz T. Sep 17 '12 at 16:22
  • On second thought delete your `menu` argument as it is never used: `private static void menu() {...}` – arshajii Sep 17 '12 at 16:26
  • Great! Thank you so much bro! Lots of prayers your way! Stay blessed! :) – Shahraiz T. Sep 17 '12 at 16:33