Simple computer login problem
so the problem with my code is it will not run unless i manually make a text file, i want it to check if the text files exist and if they do not (and they wont) then to create them, but i always come up with an error.
this is my source code, feel free to use it for yourself,(post fixed versions of my code please)
Quote:
package computer.login.v2;
import java.io.*;
import java.util.*;
public class ComputerLoginV2 {
public static void main(String[] args) throws FileNotFoundException, IOException {
File user = new File("user.txt");
File pass = new File("pass.txt");
Scanner getsome = new Scanner(System.in);
Scanner checksomeu = new Scanner(user);
Scanner checksomep = new Scanner(pass);
int trys;
String loginstatus, username, password, firsttimeuser, firsttimepass1, firsttimepass2, usertry, passtry, reset;
loginstatus = "blocked";
trys = 0;
while (loginstatus.equals("blocked")) { //this is suppposed to create the new file
if (!user.getAbsoluteFile().exists()) {
user.createNewFile();
FileOutputStream userstream = new FileOutputStream(user);
}
if (!user.getAbsoluteFile().exists()){
pass.createNewFile();
FileOutputStream passstream = new FileOutputStream(pass);
}
loginstatus = "false";
} //end of problem
while (loginstatus.equals("false")) { //here on works
checksomeu.close();
checksomep.close();
checksomeu = new Scanner(user);
checksomep = new Scanner(pass);
if (!checksomeu.hasNextLine() || !checksomep.hasNextLine()) { //no login already set up
System.out.println("First Time Users"
+ "\nPlease enter a user name(write this down as you will need it later):");
firsttimeuser = getsome.nextLine();
System.out.println("And a password:");
firsttimepass1 = getsome.nextLine();
System.out.println("Enter the password again");
firsttimepass2 = getsome.nextLine();
if (firsttimepass1.equals(firsttimepass2)) {
username = firsttimeuser;
password = firsttimepass1;
PrintStream console = System.out; //console defined normal output
FileOutputStream userstream = new FileOutputStream(user); //sends all file output to txt
PrintStream userprintstream = new PrintStream(userstream); //carlton print stream defined as a text file
System.setOut(userprintstream); //redefines stestem.our as to where is goes
System.out.println(username); //now text goes to the file
System.setOut(console); //switches back to the console
System.out.println("Your username has been set");//now its normal
FileOutputStream passstream = new FileOutputStream(pass); //sends all file output to txt
PrintStream passprintstream = new PrintStream(passstream); //carlton print stream defined as a text file
System.setOut(passprintstream); //redefines stestem.our as to where is goes
System.out.println(password);
System.setOut(console); //switches back to the console
System.out.println("Your password has been set");
}
} else {
System.out.println("Please enter your username");
usertry = getsome.nextLine();
System.out.println("And your password");
passtry = getsome.nextLine();
if (usertry.equals(checksomeu.nextLine()) && passtry.equals(checksomep.nextLine())) {
loginstatus = "true";
} else {
checksomeu.close();
checksomep.close();
checksomeu = new Scanner(user);
checksomep = new Scanner(pass);
System.out.println("Nice try And ");
trys = trys + 1;
if (!usertry.equals(checksomeu.nextLine()) && !passtry.equals(checksomep.nextLine())) {
System.out.println("Wow you got them both wrong");
} else {
System.out.println("This isn't the government bro, Get it completely right");
}
}
}
if (trys > 5) {
System.out.println("System Lock Out");
loginstatus = "lockout";
}
}
if (loginstatus.equals("true")) {
System.out.println("Congratulations you Logged in"
+ "\nDo you want to reset the username and password?(y/n)");
reset = getsome.nextLine();
if (reset.equals("y")) {
System.out.println("Good because that username and password was crap!");
FileOutputStream userstream = new FileOutputStream(user);
FileOutputStream passstream = new FileOutputStream(pass); //sends all file output to txt
}
if (!reset.equals("y") && !reset.equals("n")) {
System.out.println("I'm just going to assume you meant no");
}
if (reset.equals("n")) {
System.out.println("Your settings should still be here the next time you turn me on ;)");
}
}
if (loginstatus.equals("lockout")) {
System.out.println("Go hack someone elses basic java program!");
}
}
}
the error is run:
Exception in thread "main" java.io.FileNotFoundException: user.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
at java.util.Scanner.<init>(Scanner.java:656)
at computer.login.v2.ComputerLoginV2.main(ComputerLog inV2.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Re: Simple computer login problem
Please wrap your code in the code tags, and please post any and all errors
Re: Simple computer login problem
Re: Simple computer login problem
Quote:
i want it to check if the text files exist
See the API for the File class, it contains a methods exists() to check if a File exists as well as to create a new File.
Re: Simple computer login problem
unfortunately i have tried .exist() but it still came up with the same error,
please note i program in netbeans on a public computer that does not have access to command prompt
Re: Simple computer login problem
Quote:
please note i program in netbeans on a public computer that does not have access to command prompt
That is like trying to build a house without a hammer.
Or rebuilding an engine without a wrench.
Or driving a patrol car with no donut.
How do you do your job without the tools?
Back to the question.
Quote:
.exist() but it still came up with the same error
Is .exist() throwing an error or saying the file does not exist? If the code has been changed from what is posted, post the new code. How can someone see what you changed? If there is an error message, paste it here as well. Use code tags on the code as described in the announcements page
Re: Simple computer login problem
I tried the .exists(); first but i come up with the same error i posted above. i may have been useing it wrong. good you guys post an example of how to use it correctly? (either with this program or just a really simple program)
Re: Simple computer login problem
This is the new code with the .exists() and it comes up with this error:
Exception in thread "main" java.io.FileNotFoundException: user.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
at java.util.Scanner.<init>(Scanner.java:656)
at computer.login.v2.ComputerLoginV2.main(ComputerLog inV2.java:14)
Java Result: 1
Code :
package computer.login.v2;
import java.io.*;
import java.util.*;
public class ComputerLoginV2 {
public static void main(String[] args) throws FileNotFoundException, IOException {
File user = new File("user.txt");
File pass = new File("pass.txt");
Scanner getsome = new Scanner(System.in);
Scanner checksomeu = new Scanner(user);
Scanner checksomep = new Scanner(pass);
int trys;
String loginstatus, username, password, firsttimeuser, firsttimepass1, firsttimepass2, usertry, passtry, reset;
loginstatus = "blocked";
trys = 0;
while (loginstatus.equals("blocked")) { //this is suppposed to create the new file
if (user.exists()==false) {
user.createNewFile();
FileOutputStream userstream = new FileOutputStream(user);
}
if (pass.exists()==false){
pass.createNewFile();
FileOutputStream passstream = new FileOutputStream(pass);
}
loginstatus = "false";
} //end of problem
while (loginstatus.equals("false")) { //here on works
checksomeu.close();
checksomep.close();
checksomeu = new Scanner(user);
checksomep = new Scanner(pass);
if (!checksomeu.hasNextLine() || !checksomep.hasNextLine()) { //no login already set up
System.out.println("First Time Users"
+ "\nPlease enter a user name(write this down as you will need it later):");
firsttimeuser = getsome.nextLine();
System.out.println("And a password:");
firsttimepass1 = getsome.nextLine();
System.out.println("Enter the password again");
firsttimepass2 = getsome.nextLine();
if (firsttimepass1.equals(firsttimepass2)) {
username = firsttimeuser;
password = firsttimepass1;
PrintStream console = System.out; //console defined normal output
FileOutputStream userstream = new FileOutputStream(user); //sends all file output to txt
PrintStream userprintstream = new PrintStream(userstream); //carlton print stream defined as a text file
System.setOut(userprintstream); //redefines stestem.our as to where is goes
System.out.println(username); //now text goes to the file
System.setOut(console); //switches back to the console
System.out.println("Your username has been set");//now its normal
FileOutputStream passstream = new FileOutputStream(pass); //sends all file output to txt
PrintStream passprintstream = new PrintStream(passstream); //carlton print stream defined as a text file
System.setOut(passprintstream); //redefines stestem.our as to where is goes
System.out.println(password);
System.setOut(console); //switches back to the console
System.out.println("Your password has been set");
}
} else {
System.out.println("Please enter your username");
usertry = getsome.nextLine();
System.out.println("And your password");
passtry = getsome.nextLine();
if (usertry.equals(checksomeu.nextLine()) && passtry.equals(checksomep.nextLine())) {
loginstatus = "true";
} else {
checksomeu.close();
checksomep.close();
checksomeu = new Scanner(user);
checksomep = new Scanner(pass);
System.out.println("Nice try And ");
trys = trys + 1;
if (!usertry.equals(checksomeu.nextLine()) && !passtry.equals(checksomep.nextLine())) {
System.out.println("Wow you got them both wrong");
} else {
System.out.println("This isn't the government bro, Get it completely right");
}
}
}
if (trys > 5) {
System.out.println("System Lock Out");
loginstatus = "lockout";
}
}
if (loginstatus.equals("true")) {
System.out.println("Congratulations you Logged in"
+ "\nDo you want to reset the username and password?(y/n)");
reset = getsome.nextLine();
if (reset.equals("y")) {
System.out.println("Good because that username and password was crap!");
FileOutputStream userstream = new FileOutputStream(user);
FileOutputStream passstream = new FileOutputStream(pass); //sends all file output to txt
}
if (!reset.equals("y") && !reset.equals("n")) {
System.out.println("I'm just going to assume you meant no");
}
if (reset.equals("n")) {
System.out.println("Your settings should still be here the next time you turn me on ;)");
}
}
if (loginstatus.equals("lockout")) {
System.out.println("Go hack someone elses basic java program!");
}
}
}
Re: Simple computer login problem
Please see the announcements page for the use of code tags. I just don't see the point in trying to read a page of left justified textual code. Use the "Go Advanced" and "Preview Post" buttons to preview the post and make sure the code is well formatted.
Re: Simple computer login problem
Quote:
Originally Posted by
jps
Please see the
announcements page for the use of code tags. I just don't see the point in trying to read a page of left justified textual code. Use the "Go Advanced" and "Preview Post" buttons to preview the post and make sure the code is well formatted.
can you help now
Re: Simple computer login problem
Quote:
Originally Posted by
centralnathan
can you help now
I have removed the profanity from your post, and do not appreciate your poor attitude. For that reason this thread is closed.
If you still need help, change your attitude and ask a new question. Any further personal attacks will result in further action.