Results 1 to 3 of 3
- 02-16-2013, 07:00 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
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
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.*; 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(); } }
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 12:45 PM.
-
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).
-
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:
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.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)
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 07:38 AM.
Similar Threads
-
USerName, Password From Cliet
By raj.mscking@gmail.com in forum New To JavaReplies: 40Last Post: 05-02-2012, 05:35 PM -
Username And Password
By razmyasdfg in forum JDBCReplies: 9Last Post: 05-18-2011, 02:59 PM -
New to GUI. Logging in with Password and username
By slitka in forum New To JavaReplies: 3Last Post: 04-11-2011, 11:39 PM -
username password verification
By bheezee in forum JDBCReplies: 0Last Post: 11-25-2008, 06:55 PM -
JTextFields with username & password.
By Eric in forum AWT / SwingReplies: 2Last Post: 07-01-2007, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks