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

Threaded View

  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.


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