-5

I have a problem because I do not know how to save the variable. The variable newname and the newpassword deafult are zero. But in case 1 they are changed to the given values, but after case 1 the variables return to the basic values 0. and i cant log in (in case 2) becasue login and password always are 0. How i can globally set this variable on case 1?

    String newname = null;
    String newpassword = null;


    System.out.println("Hello!");
    System.out.println();
    System.out.println("     ****************************************");
    System.out.println("     *                 MENU                 *");
    System.out.println("     ****************************************");
    System.out.println("     1. Create new account");
    System.out.println("     2. Log in");
    System.out.println("     3. Help");
    System.out.println("     0. End");
    Scanner opcje = new Scanner(System.in);
    int choose = opcje.nextInt();

    switch (choose) {

        case 1:

            System.out.println("You choose create new acount\n Enter the login");
            Scanner nlogin = new Scanner(System.in);
            newname = nlogin.nextLine();

            System.out.println("Please enter the password ");
            Scanner npassword = new Scanner(System.in);
            newpassword = npassword.nextLine();
            System.out.println("the account has been created\n");

        case 2:

            Scanner login = new Scanner(System.in);
            System.out.println("Login:");
            String pass1 = login.nextLine();
            System.out.println("Password:");
            Scanner password = new Scanner(System.in);
            String pass2 = password.nextLine();
            if (pass1 == newname & pass2 == newpassword){
                System.out.println("you are logged in");
            }else{
                System.out.println("incorrect passoword or login");
            }
            break;

        case 3:
            System.out.println("Help is off");
            break;

        case 0:
            System.out.println("ending");
            break;

        default:
            System.out.println("Select the option by pressing 1,2,3 or 0");
            break;
    }
}

}

  • I do not want compare strings, I want globally to set strings in case 1 – Marcin Kaczmarzyk Dec 25 '17 at 15:25
  • I see no break beetween case 1 and 2. It could je thé cause of the problem – Greg Artisi Dec 25 '17 at 15:26
  • 1
    put break; in every case – Lalit Verma Dec 25 '17 at 15:27
  • 3
    This looks like a scoping issue. Is this code inside a class? main? method? since your variables are declared inside it they'll be 'alive' for as long as it runs but, not once it's done. @Marcin - This is a good start: [tutorial](https://www.geeksforgeeks.org/variable-scope-in-java/). – Eggcellentos Dec 25 '17 at 15:27
  • There are multiple errors here. 1. you are missing a break after the first case, 2. you potentially have a scope issue for your variables if they are simply local variables in the method and you call the method repeatedly then their contents are simply forgotten in between calls. 3. you compare Strings using `==`. – luk2302 Dec 25 '17 at 15:29
  • 1
    @GregArtisi I think he might be missing the `break;` intentionally, because after sign up user have to login. This is just my personal opinion ! – zsubzwary Dec 25 '17 at 15:29
  • @marcin kaczmarzyk you are right, i Saw it after – Greg Artisi Dec 25 '17 at 15:31
  • @MarcinKaczmarzyk Use `do-while` to make your `menu`. You can have a `switch` inside the `do-while`. Take everything out of each `case:` and put it in `functions` so the `case calls a function` instead of having a `block of code` after every case (it's messy this way). And yes, you are `missing a break` at the end of `case 1:`. Don't miss the `break` intentionally. Rather `make functions` and call the `function login();` from `case 1:`. – Ivan86 Dec 25 '17 at 15:41
  • ok i use do while and break between case 1 and 2. And still variable is forgotten – Marcin Kaczmarzyk Dec 25 '17 at 15:45
  • Please read [mcve] and enhance your question accordingly. – GhostCat Dec 25 '17 at 16:46
  • But beyond that this community is not meant as compiler replacement. You dropping many lines of code with zillions of issues is not appropriated. – GhostCat Dec 25 '17 at 16:47

3 Answers3

0

There are so many issues here.

  1. You are missing a break; after the first case
  2. You potentially have a scope issue for your variables if they are simply local variables in the method and you call the method repeatedly then their contents are simply forgotten in between calls.
  3. You are comparing Strings using == , Read More here => Java String Equals

Correct Code:

public class MainClass {
    String newname = null;
    String newpassword = null;
    //continue your code here
    //call menu() when required
    menu();

}


public void menu()
{
    System.out.println("Hello!");
    System.out.println();
    System.out.println("     ****************************************");
    System.out.println("     *                 MENU                 *");
    System.out.println("     ****************************************");
    System.out.println("     1. Create new account");
    System.out.println("     2. Log in");
    System.out.println("     3. Help");
    System.out.println("     0. End");
    Scanner opcje = new Scanner(System.in);
    int choose = opcje.nextInt();

    switch (choose) 
    {

        case 1:

            System.out.println("You choose create new acount\n Enter the login");
            Scanner nlogin = new Scanner(System.in);
            newname = nlogin.nextLine();

            System.out.println("Please enter the password ");
            Scanner npassword = new Scanner(System.in);
            newpassword = npassword.nextLine();
            System.out.println("the account has been created\n");
            break;

        case 2:

            Scanner login = new Scanner(System.in);
            System.out.println("Login:");
            String pass1 = login.nextLine();
            System.out.println("Password:");
            Scanner password = new Scanner(System.in);
            String pass2 = password.nextLine();
            if (pass1.equals(newname) & pass2.equals(newpassword)){
                System.out.println("you are logged in");
            }else{
                System.out.println("incorrect password or login");
            }
            break;

        case 3:
            System.out.println("Help is off");
            break;

        case 0:
            System.out.println("ending");
            break;

        default:
            System.out.println("Select the option by pressing 1,2,3 or 0");
            break;
    }

}
zsubzwary
  • 1,196
  • 1
  • 14
  • 22
0

One of the problems is with this line:

   if (pass1 == newname & pass2 == newpassword){
      System.out.println("you are logged in");
     }else{
     System.out.println("incorrect passoword or login");
      }

If you will debug this code, You will notice that this if statement, doesn't compare the values of the String. For more details you may visit: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

Try this code instead:

        if(pass1.equals(newname) && pass2.equals(newpassword)) 
        {
            System.out.println("you are logged in");
        }else{
            System.out.println("incorrect passoword or login");
        }

second problem:

you need to put your switch statement in a while loop:

1.This why the program won't end (and this way the variable) will be saved.

2.IF the user input is invalid instead of going to default, the program will also ask again for the user to write a number.

yoav
  • 191
  • 1
  • 2
  • 10
0

Try changing the way you compare Strings, also use java logical operators instead of Bitwise &.

static String newname = null;
static String newpassword = null;
public static void main(String[] args) {

    System.out.println("Hello!");
    System.out.println();
    System.out.println("     ****************************************");
    System.out.println("     *                 MENU                 *");
    System.out.println("     ****************************************");
    System.out.println("     1. Create new account");
    System.out.println("     2. Log in");
    System.out.println("     3. Help");
    System.out.println("     0. End");
    Scanner opcje = new Scanner(System.in);
    int choose = opcje.nextInt();

    switch (choose) {

        case 1:

            System.out.println("You choose create new acount\n Enter the login");
            Scanner nlogin = new Scanner(System.in);
            newname = nlogin.nextLine();

            System.out.println("Please enter the password ");
            Scanner npassword = new Scanner(System.in);
            newpassword = npassword.nextLine();
            System.out.println("the account has been created\n");
        case 2:

            Scanner login = new Scanner(System.in);
            System.out.println("Login:");
            String pass1 = login.nextLine();
            System.out.println("Password:");
            Scanner password = new Scanner(System.in);
            String pass2 = password.nextLine();
            //Java uses equals method to compare Strings
            //Java also uses && as the logical operator for "and"
            if (pass1.equals(newname) && pass2.equals(newpassword)) {
                System.out.println("you are logged in");
            } else {
                System.out.println("incorrect password or login");
            }
            break;

        case 3:
            System.out.println("Help is off");
            break;

        case 0:
            System.out.println("ending");
            break;

        default:
            System.out.println("Select the option by pressing 1,2,3 or 0");
            break;
    }
}
BluRe.CN
  • 311
  • 2
  • 11