Results 1 to 3 of 3
  1. #1
    clingcling is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default verifying Username and Password from a textfile..

    Hi guys can you help me, Im new to java and Im trying to make a simple program that will verify the username and the password from a textfile.. These are two different java files, im trying to link it to each other but having problems using another class/program's variable. Im quite new to java so im not familiar with some of the codes. Thanks for the responses..

    This is working properly


    Java Code:
    import java.io.*;
    
    import java.util.*;
    
    public class Userpass {
    
    private Scanner x;
    
    public void  openFile(){
    
    try{
    
    x=new Scanner(new File("Accounts.text"));
    
    }
    
    catch (Exception e)
    
    {System.out.println("File not found");
    
    }
    
    }
    
    public void readFile(){
    
    while (x.hasNext()){
    
    String a =x.next();
    
    String b =x.next();
    
    System.out.println(a);
    
    System.out.println(b);
    
    
    }
    
    }
    
    public void closeFile(){
    
    x.close();
    
    }
    
    }
    This is the login... the problems here are the strings a and b keep printing null.... im sure i missed something..


    Java Code:
    import java.io.*;
    
    public class Login {
    
    public static void main(String args[])throws Exception {
    
    BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    
    String a=new userpass().a;
    
    String b=new userpass().b;
    
    String uname;
    
    String pword;
    
    userpass r=new userpass();
    
    r.openFile();
    
    r.readFile();
    
    System.out.println("Enter Username: ");
    
    uname=input.readLine();
    
    System.out.println("Enter Password: ");
    
    pword=input.readLine();
    
    if(uname.equals(a)&&(pword.equals(b)))
    
    {System.out.println("Correct");
    
    r.readFile();}
    
    else
    
    {System.out.println("Fail");
    
    System.out.println(a); //both of them prints null
    
    System.out.println(b); //both of them prints null
    
    }
    
    }
    
    }
    Last edited by clingcling; 02-16-2013 at 01:45 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: verifying Username and Password from a textfile..

    It's very hard to help if we can't read the code. Please get rid of the line numbers and post well formatted code using code tags (read the link in my signature below).

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: verifying Username and Password from a textfile..

    Next where do you set the a and b variables with valid Strings? You are setting them with Strings from another class that has never initialized them. Also you are creating three completely distinct userpass objects when you should only be creating a single instance.

    To clarify, you create userpass objects as noted below:

    Java Code:
    String a=new userpass().ab;  // *** here *** (1)
    String b=new userpass().ac;  // *** here *** (2)
    String uname;
    String pword;
    userpass r=new userpass();  // *** here *** (3)
    r.openFile();  // **** (4)
    r.readFile();  // **** (5)
    So a unique userpass object is created on lines 1, 2, and 3, and by unique it is important to note that changing the property of one object has no effect on the others. You assign your null Strings to a and b on lines (1) and (2) before any userpass object has had a chance to initialize the ab or ac Strings. You then initialize strings in a completely different userpass object held by the r variable on line (5). This has no effect on the previously created userpass objects, and even if it did, would have no effect on the a and b variables since they have been assigned nulls, and nothing will change that unless they are later assigned something else.

    Suggestions:
    • Again, please try to post decently formatted readable code without line numbers. It's hard to read someone else's program so let's not make it harder.
    • Again, please edit your post and add [code] [/code] tags around your well formatted code.
    • All class names should begin with an upper case letter, and all variable and method names a lower case letter.
    • Use variable names that make sense and make the code self-commenting. Variables named a, b, ab, ... are difficult to see at a glance what they may be doing or what their purpose may be.
    • Create one userpass object (the class should be called UserPass), have it read in the necessary file to initialize all variables
    • Then use that single object to initialize any Strings you need. Extract their values with getter methods and not by directly accessing variables which should be private.
    Last edited by Fubarable; 02-16-2013 at 08:38 AM.

Similar Threads

  1. USerName, Password From Cliet
    By raj.mscking@gmail.com in forum New To Java
    Replies: 40
    Last Post: 05-02-2012, 06:35 PM
  2. Username And Password
    By razmyasdfg in forum JDBC
    Replies: 9
    Last Post: 05-18-2011, 03:59 PM
  3. New to GUI. Logging in with Password and username
    By slitka in forum New To Java
    Replies: 3
    Last Post: 04-12-2011, 12:39 AM
  4. username password verification
    By bheezee in forum JDBC
    Replies: 0
    Last Post: 11-25-2008, 07:55 PM
  5. JTextFields with username & password.
    By Eric in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 12:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •