简体   繁体   中英

Java Error When attempting to use the return from a method as an item in an if statement

I keep getting the following errors: Cannot find symbol Variable find

cannot find symbol method getdata(int)

I am sure I am making this way more difficult than it is, but I am not sure how make this work so that the return from searching through the array, can be seen and evaluated by the if statement.

                   //assigns manager identification 
                    manID = keyboard.nextInt();

        //Fibonacci binary array for passwords
        int[] passWArray = {00000000,00000001,00000001,00000010,00000011,00000101,00001000,00001101};

        //item = find.getdata(manID);

        if (getdata(manID) != -1) 
        {
        //Do work here
        dblPayRate = 10.85;
        dblGrossPay =  (intHours * dblPayRate) + (15.00);
        dblTaxes = dblGrossPay * 0.19;
        dblGrossPay -= dblTaxes;

        //Print information to user
        System.out.print("\n\n$" + df2.format(dblTaxes) + 
        " was withheld from this paycheck in taxes after working "+ intHours + " hours.\n\n");
        System.out.print("The amount \"Employer Here\" owes you is $" + df2.format(dblGrossPay) + "\n");
        }
        else
        {
        // Dialog box for incorrect password
        JOptionPane.showMessageDialog(null, "Invalid Entry! Contact the BOFH!");
        //exits program (Note: needed for any JOptionPane programs)
        System.exit(0); 
        }
    }// end of long if statement for >50 hours
}//end of main method

 public int find(int[] passWArray, int manID) 
{
    //search for manID in passWArray array
    for (int index = 0; index < passWArray.length; index++) 

        if ( passWArray[index] == manID )

            return manID;
    //-1 indicates the value was not found      
    return -1;

}// end of find method  

Change

if (getdata(manID) != -1) 

into

if (find(passWArray , manID) != -1) 

BTW those numbers don't magically become binary because they only contain 0's and 1's. Here's a hint:

int thirteen = Integer.parseInt("00001101", 2)

EDIT: in response to your next error

For now make the method static:

public static int find(int[] passWArray, int manID) 

Eventually you might want to think about your 'Object-Oriented design' and just use the main() method as an entry point. Within main you create an instance of a class and let it do its work. In this way you can use the powers of OO like encapsulation and inheritance and don't have to make everything static.

EDIT2: Afterthought

Your program seems to have the following 'actions':

  • user interaction
  • authentication
  • calculation

And there seem to be the following 'things' in your domain:

  • user
  • password
  • keyboard
  • display (command line and screen)
  • calculation

A good rule of thumb for an OO design is to convert some of the 'things' and 'actions' already present in your domain into classes. A good class has a single responsibility and shares as little as possible of its data and methods with other classes (this is called information hiding).

Here's a class diagram that comes to mind:

  • User (represents a user, contains a single field 'password')
  • Authenticator (authenticates a user, contains the list of allowed passwords)
  • Console (all user interaction, either use System.out/in or Swing, but don't mix them)
  • Calculator (it calculates shit)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM