0

I am creating an example login app. For some reason when I call the method checkFinal in my main method it is giving me an error. It says that it needs to call the booleans which check the username and the password. Which I did. It says that they can not be passed through. I do not know if this is an pass by value or a pass by reference issue. I have all of the other code working.

import java.util.Scanner; public class program { private static Scanner a; private static String inputusername; private static Scanner b; private static String inputpassword; private static String validusername; private static String validpassword; public static void main(String[] args) { greeting(); questiona(); questionb(); username(); password(); checkOne(validusername, inputusername); checkTwo(validpassword, inputpassword); checkFinal(usernamecheck, passwordcheck); } public static void greeting() { System.out.println("Hello!"); System.out.println("Note: All Things Are Case Sensitive!"); } public static String questiona() { System.out.println("What Is Your Username?"); a = new Scanner(System.in); inputusername = a.next(); return inputusername; } public static String questionb() { System.out.println("What Is Your Password"); b = new Scanner(System.in); inputpassword = b.next(); return inputpassword; } public static String username() { validusername = "username"; return validusername; } public static String password() { validpassword = "password"; return validusername; } public static boolean checkOne(String validusername, String inputusername) { boolean usernamecheck = false; if (validusername == inputusername) { usernamecheck = true; } return usernamecheck; } public static boolean checkTwo(String validpassword, String inputpassword) { boolean passwordcheck = false; if (validpassword == inputpassword) { passwordcheck = true; } return passwordcheck; } public static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) { boolean checkFinal = false; if (usernamecheck == true && passwordcheck == true) { checkFinal = true; } else { checkFinal = false; } return checkFinal; } public static void compile(String[] args) { } public static void server(String[] args) { } } 
1
  • change inside your main to this -checkFinal(checkOne(validusername, inputusername),checkTwo(validpassword, inputpassword)); Commented May 3, 2018 at 19:18

4 Answers 4

2

You have to assign these two method results to variables:

boolean usernamecheck = checkOne(validusername, inputusername); boolean passwordcheck = checkTwo(validpassword, inputpassword); checkFinal(usernamecheck, passwordcheck); 
Sign up to request clarification or add additional context in comments.

Comments

1

usernamecheck is a local variable in checkOne

passwordcheck is a local variable in checkTwo

in your main checkFinal(usernamecheck, passwordcheck); the two arguments are not initialised.

it looks like you want to pass the outputs of checkOne and checkTwo as the arguments

public static void main(String[] args) { greeting(); questiona(); questionb(); username(); password(); boolean usernamecheck = checkOne(validusername, inputusername); boolean passwordcheck = checkTwo(validpassword, inputpassword); checkFinal(usernamecheck, passwordcheck); } 

3 Comments

on a side note, when you checking booleans such as in if (usernamecheck == true && passwordcheck == true) you do not need to check if booleans are true, the if statement above is the same as if (usernamecheck && passwordcheck) . If you want to check if a boolean is false you can use if (! foo) instead of if (foo = false)
Thanks, it worked. But was it a pass by value issue or a pass by reference issue. I would like to just get more knowledge.
this thread may answer your question. When a variable is declared in a method, it is only "alive" until that method returns. If you compiled the code you would have seen and error along the lines of cannot find symbol with an arrow pointing at usernamecheck this tells you that the variable has not been defined in the scope of your method you called it in.
1

From the other answers, you should get an idea of what you need to do to fix your error. One other way is to shorten your method to call the checkFinal method with the other two methods as parameters so you don't need to create another variable.

checkFinal(checkOne(validusername, inputusername), checkTwo(validpassword, inputpassword)); 

A few additional comments about your code:

The method:

public static boolean checkOne(String validusername, String inputusername) { boolean usernamecheck = false; if (validusername == inputusername) { usernamecheck = true; } return usernamecheck; } 

can be changed to:

public static boolean checkOne(String validusername, String inputusername) { boolean usernamecheck = validusername.equals(inputusername); return usernamecheck; } 

First thing is that you cannot compare two string using ==. Second thing, you don't need to compare boolean == true. When you say if (boolean) it is implied that it means if (boolean == true). Same goes for your other methods as well.

For example:

public static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) { boolean checkFinal = false; if (usernamecheck == true && passwordcheck == true) { checkFinal = true; } else { checkFinal = false; } return checkFinal; } 

can be written as:

public static boolean checkFinal(boolean usernamecheck, boolean passwordcheck) { return usernamecheck && passwordcheck; } 

Comments

0

You are not saving the boolean results so that they can be passed to the next step. But you could pass them into checkfinal() like this:

public static void main(String[] args) { greeting(); inputusername = questiona(); inputpassword = questionb(); /* these 2 are not used and may not be necessary username(); password(); */ checkfinal(checkOne(validusername, inputusername), checkTwo(validpassword, inputpassword)); } 

Also, note that you probably should set the global variables with the result of the questiona() and questionb() methods as I did. It will work as you have it but it is a bad habit.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.