Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Simple computer login problem

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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)

    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)
    Last edited by centralnathan; October 18th, 2012 at 12:45 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple computer login problem

    Please wrap your code in the code tags, and please post any and all errors

  3. The Following User Says Thank You to copeg For This Useful Post:

    centralnathan (October 18th, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple computer login problem

    there you go

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple computer login problem

    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.

  6. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple computer login problem

    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.
    .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

  8. #7
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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)

  9. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    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!");
            }
        }
    }
    Last edited by jps; November 1st, 2012 at 03:55 PM. Reason: **** moderator <-Removed profanity

  10. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  11. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple computer login problem

    Quote Originally Posted by jps View Post
    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

  12. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Simple computer login problem

    Quote Originally Posted by centralnathan View Post
    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.

Similar Threads

  1. [SOLVED] Having issues with simple logIn --- insert new user to th db
    By justyStepi in forum JDBC & Databases
    Replies: 5
    Last Post: August 27th, 2012, 07:06 PM
  2. Problem with LogIn - Multiple users
    By justyStepi in forum AWT / Java Swing
    Replies: 5
    Last Post: April 28th, 2012, 12:18 PM
  3. i need coding for simple login
    By nag in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 29th, 2011, 02:30 PM
  4. Buttons and a Login problem
    By raja211991 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 23rd, 2011, 02:41 PM
  5. simple login web service
    By mr_aliagha in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: January 5th, 2010, 03:49 PM

Tags for this Thread