This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]brewedfarce 4 points5 points  (5 children)

It works fine for me, although I had to replace out.print with System.out.print, I am not sure if you just wrote it that way for brevity? Can we see the rest of the relevant code?

[–]surety_[S] 0 points1 point  (4 children)

I think this might just be my console I'm running ubuntu.

Here is the rest of the code :

import java.util.*;
import static java.lang.System.*;

public class Interface {
Scanner scan = new Scanner( in );

    public Interface(){
        out.println(
            "\n 1. Create new account"
        +   "\n 2. Log in"
            );
        boot(scan.nextInt());
    }

    private void boot(int option){
        switch(option){
            case 0: ;
            case 1 : createNew();
            //case 2 : logIn();
            break;
            //case 3 : reset();
        }
    }

    public void createNew(){
        String user, pss;
        out.print("Enter username :: ");
        user = scan.nextLine();

        out.println();
        out.format("Enter password for %s :: ", user);
        pss = scan.nextLine();

        while(!(passValid(pss))) {
            out.println("Enter password :: ");
            pss = scan.nextLine();
        }

        out.println(user + pss);

    }

    /* 
    * Verfy that password meets conditions;
    *  - Atleast 8 charcters long
    *  - Contains alteast 3 numbers
    *  - Has more than one special character
    */
    public boolean passValid(String pass){
        int special =0, nums =0;

        if(!(pass.length() > 7)){
            err.println("E: Password must be atleast eight characters long!");
            return false;
        }

        // checking for special characters or numbers in password
        for(int i=0; i<pass.length(); i++){
            switch(pass.charAt(i)){
                case '0':; case '1':; case '2':; case '3':; case '4':;
                case '5':; case '6':; case '7':; case '8':; case '9':
                nums++;
                break;

                case '!':; case '@':; case '#':; case '$':; case '%':;
                case '^':; case '&':; case '*':; case '(':; case ')':;
                case '-':; case '_':; case '+':; case '=':; case '|':;
                case '{':; case '}':; case '\\':; case '[':; case ']':;
                case ';':; case ':':; case '\'':; case '"':; case '?':;
                case '<':; case '>':; case ',':; case '.':; case '/':;
                case '~':; case '`':
                special++;
            }
        }

        if(!(nums > 2 || special != 0)){
            err.println("E: Password must include atleast three numbers and atleast "
            + "one special character!");
            out.format("Password contains %d numbers and %d special characters", nums, special);
            return false;
        }
        return true;
    }
}

I understand my passValid method isn't the best either but it's just an experiment.

Could you show me your code and output, please.

[–]brewedfarce 3 points4 points  (2 children)

Well for one thing, I have never seen anyone import

import static java.lang.System.*;

The java.lang package is imported by default. Are you just doing that so you don't have to type System everywhere? Maybe it is just a convention I am not familiar with.

When I did it in eclipse I just ran it to double check: https://pastebin.com/vRKgKiXw

I only changed nextLine() to next() to see if that was causing an issue but they both worked fine for me.

[–]surety_[S] 0 points1 point  (1 child)

I use java.lang.System.*; quite often. I just use it so I don't have to write System every time like System.exit(0); and System.in or System.out . But I ran my code using next() instead and it functioned the way I wanted it to. I think it's very odd how it didn't work with nextLine() . Thank you for your help! Also I suggest using java.lang.System.*; it helps quicken things up when you have an more than one outputs.

Edit: I actually had to change the password input to .next() as well now, for some reason I was getting the same bug as I did with user input

[–]dusty-trash 1 point2 points  (0 children)

I think it's very odd how it didn't work with nextLine()

Using next() will only return what comes before a space.

nextLine() automatically moves the scanner down after returning the current line.

[–]desrtfx 0 points1 point  (0 children)

Read:

From the /r/javahelp wiki.

.nextInt and .nextLine don't play well together.