Jump to content

Java help


Recommended Posts

Hey there Ahoyworld community.

I recently started a final project for my computer class and I chose programming in Java.

 

I am currently making a Login/Logout application and I just need some help. Here's the code:

 

package login_out_code;
 
import java.util.*;
import java.io.*;
 
public class Login_out_code {
 
    public static void main(String[] args) {
        
        System.out.println("Hello! Please login to access our system");
 
        System.out.println("Please type the username: ");
        
        String ass;
         try {java.io.BufferedReader butt = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
 
         ass = butt.readLine();
         }
         catch (Exception e){
             System.out.println("Invalid Username.");
            int breakp = 43;
            }
         
         
            System.out.println("Now, please type the password: ");
 
            String bacon;
 
            try{java.io.BufferedReader pig = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
 
            bacon = pig.readLine();
            
            }
            catch(Exception e){
                System.out.println("Invalid Password, please try again.");
            int breakp = 43;
            }
            
            System.out.println("Welcome to the the system!");
         
         
    }
}

 

Logging in works... just without a set username/password. I've worked on this for a couple of hours. I realise the solution may be an easy fix. But i just started learning Java 2½ weeks ago. So I'm new to it.

 

I want to login with a username and password but right now I can put any username/password and be able to get in. Help please!

Worked on this for hours!

 

Thanks!

- R. Berezon

Link to comment
Share on other sites

Here's an example on how to accept a specific hardcoded user and pass:

package login_out_code;

import java.util.*;
import java.io.*;

public class Login_out_code {

    public static void main(String[] args) {

        String username = "username";
        String password = "passw0rd";

        System.out.println("Hello! Please login to access our system");

        System.out.println("Please type the username: ");

        String ass;
        try {
            java.io.BufferedReader butt = new java.io.BufferedReader(new java.io.InputStreamReader(System.in ));
            ass = butt.readLine();
            if (!ass.equals(username)) {
                System.out.println("Invalid Username.");
                System.exit(0);
            }
        } catch (Exception e) {
            System.out.println("Invalid Username.");
            int breakp = 43;
        }


        System.out.println("Now, please type the password: ");

        String bacon;

        try {
            java.io.BufferedReader pig = new java.io.BufferedReader(new java.io.InputStreamReader(System.in ));
            bacon = pig.readLine();
            if (!bacon.equals(password)) {
                System.out.println("Invalid Password.");
                System.exit(0);
            }
        } catch (Exception e) {
            System.out.println("Invalid Password, please try again.");
            int breakp = 43;
        }

        System.out.println("Welcome to the the system!");

    }
}

 

 

It's just an example since if you have the username and password hardcoded, anyone with access to the files can read them, you would want to use a database to store the passwords encrypted.

 

I have added two "if" conditionals for the ass and bacon vars which are the user and pwd you enter on the console, if they don't match the hardcoded values (the username and password string vars on the top) the program just exit, if they match the code execution continues as normal. To do the "please try again" thing you would need a while loop, let me know if you want me to show you how to do that.

Link to comment
Share on other sites

I have added two "if" conditionals for the ass and bacon vars...

 

Proof that you shouldn't skip to the end of a post.

 

Regarding other, more on-topic points: fantastic post, wok.

Link to comment
Share on other sites

Thank you wok! that was exactly what I needed, thanks! now to continue my project. :) +1 to wok.

I can probably be able to get a while loop working. It was just trying to set the hardcoded variables for user and password.

Again, thank you very much!

 

 

 

I told you, there's help to get in the community. Thanks wok for proving my point. My own java knowledge is quite limited at the moment. But I know a lot of other stuff :-)

 

 

You are right! Thanks for bringing up that idea. Can't believe that I didn't think about that earlier.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Forum Statistics

    11.1k
    Total Topics
    66.4k
    Total Posts
×
×
  • Create New...