简体   繁体   中英

How to get the user input data that i recently typed in?

I just started to learn Java a few days ago and i wanted to create a program that will create an account,stored it and then if we enter the exact username and password then it will say "login successful".Please don't mind my way of naming the variables and classes.My question is in my "crttacc" class,where i have set up 2 get method to get the un and pa variables.However,in my main method after running the create method in crttacc,the input data seem to gone away and not stored in the un,pa variables as when i try to print them out to check it,it would return "null".I'm a newbie so i'm not quite sure that my question was clear enough and i hope that u guys can help me.You have my gratitude. Here's my codes:(please don't laugh :D)

import java.util.Scanner;
class crttacc {
    String pa;
    String un;

    void create() {
        Scanner unin = new Scanner(System.in);
        Scanner passin = new Scanner(System.in);
        System.out.println("enter the user name: ");
        String l1 = unin.nextLine();
        System.out.println("enter the password: ");
        String l2 = passin.nextLine();
        l1=un;
        l2=pa;

    }
    String getpass(){
        return un;
    }
    String getuser(){
        return pa;
    }
}
class loginn {
    ;

    void logingin() {
    crttacc acc1 = new crttacc();
    String pass = acc1.getpass();
    String user = acc1.getuser();
    System.out.println(pass);
    System.out.println(user);
    Scanner passwordinput = new Scanner(System.in);
    Scanner usernameinput = new Scanner(System.in);
    System.out.println("username:");
    String line1 = usernameinput.nextLine();
    System.out.println("Password:");
    String line2 = passwordinput.nextLine();
    String d1=line1;
    String d2=line2;
    if(d1.equals(pass) && d2.equals(user)) {
        System.out.println("login successfull");
    }
    else {
        System.out.println("try again");
}
}   
}
public class login3 {
    public static void main(String[] args) {
        Scanner com = new Scanner(System.in);
        System.out.println("enter your command: create account or login");
        String com1 = com.nextLine();
        String l1=com1;
        if(com1.equals("create account")) {
            crttacc acc1 = new crttacc();
            acc1.create();

        }
        Scanner com2 = new Scanner(System.in);
        System.out.println("would you like to login now? y/n");
        String cm2 = com2.nextLine();
        String l2=cm2;
        if(l2.equals("y")) {
            loginn log1 = new loginn();
            log1.logingin();

        }
        else{

        }
}
}

it's :

un=l1;
pa=l2;   

in the Create() method

The problem seems to be, that you store the input data you get through the scanner in l1 and l2. Then you give your l1 and l2 variables the value of un and pa (which is null at this point). At the end of your method the local variables l1 and l2 are thrown away and nothing changed for your un and pa.

You propably wanted something like this:

void create() {
    Scanner scanner = new Scanner(System.in);
    System.out.println("enter the user name: ");
    String l1 = scanner.nextLine();
    System.out.println("enter the password: ");
    String l2 = scanner.nextLine();
    this.un = l1;
    this.pa = l2;
}

With this you stored the values of l1 and l2 in your fields un and pa and can access them through your getters.

Hint: you don't need a new Scanner for every input. You can reuse it :)

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