Java Coding

Posts 111 of 11 · Page 1 of 1
Java Coding
Hello everybody!

I'm currently taking an online Java class and I was getting stuck at one part of the class. I was wondering if anyone could offer me some help with the class. I asked my teacher about it and they will get back to me asap, but I would like to see some variety in this. I am trying to make a program that (in Eclipse's console) prompts the user to enter a password that is only characters including a-z, A-Z, and 0-9. It needs to be at least 8 characters long and I don't know exactly how to code it. I am learning about Strings currently and I have looked up multiple things. I am very interested in Java programming (and programming in general. I currently do not know any other coding languages, but I think my next two will be python and php (since after minor research they both seem easy, just the logic is different.) My brain thinks very logically so if anyone has any suggestions I do not need an explanation unless I ask about it. I get general idea's really easy. Any help is really appreciated.

Thanks for your help.
I really wish you would've posted some code as to see where you're having an issue. Either way I've coded this for you and tried explaining my logic. If you need me to explain anything, feel free to ask.
Code:
import java.util.Scanner; // needed to get user input

public class MPGHExample {

   public static void main(String args[]) {
      Scanner input = new Scanner(System.in); // instantiate Scanner
      String userPassword; 
      int passwordLength;
      
      // Prompt user
      System.out.println("Enter a password. Can containt a-z, A-Z, 0-9");
      userPassword = input.nextLine(); // store password
      passwordLength = userPassword.length();
      
      // Check to see if password is atleast 8 digits long 
      if(passwordLength >= 8){
         // checks to see if password contains only these characters
         if(userPassword.matches("[a-zA-Z0-9]+")){
            System.out.println("That is a valid password.");
         } else {
            System.out.println("You entered illegal characters.");
         }
      } else {
         System.out.println("Password must be 8 characters long.");
      }
      
   }
}
Thank you so much. It is interesting to see how you coded this differently than my teacher had. If I get a chance tomorrow, I will copy the code and paste it here so you may see. This website is blocked at school so that doesn't help.
All right cool! No problem and thank you.
Use if statement and boolean.
Boolean check = false;
Add restrictions, if it is not 8 character long, it would prompt as an error.
i.e.
if(char.long <= 9)
check = true;
Quote Originally Posted by Source Unknown View Post
Use if statement and boolean.
Boolean check = false;
Add restrictions, if it is not 8 character long, it would prompt as an error.
i.e.
if(char.long <= 9)
check = true;

Are you trolling?
Quote Originally Posted by Cosmo_ View Post
Are you trolling?
No I am not.


Code:
   public static void main(String[] args)
    {
        //variables
        boolean length=false;
        boolean up=false;//Upper letter
        boolean dwn=false;//Lower letter
        boolean num=false;//number
        char ch;
        //enter Scanner
        Scanner r = new Scanner(System.in);
        //enter prompt;
        System.out.println("Please enter a password with atleast 8 character\nthat consist of one upper case, one lower case and one digit.");
        String password=r.next();//body of for loop
        //enter the restrictions
        for(int i=0;i<password.length();i++)
        {
            //string
            ch=password.charAt(i);
            if(password.length()>7)
            {
                length=true;
                if (Character.isUpperCase(ch))
                {
                    up=true;
                }
                else if(Character.isLowerCase(ch))
                {
                    dwn=true;
                }
                else
                {
                    num=true;
                }
            }
        }
        if(length&&up&&dwn&&num)
        {
            System.out.printf("You successfully entered your password!");
        }
        else
        {
            System.out.printf("The password you entered is invalid");
        }
    }
There are different ways of doing this so. :3 This code has the same concept as his. Yours were precise though.
Quote Originally Posted by Source Unknown View Post
No I am not.
I do humbly apologize good sir. In your original post you had char.long and that made me curious. So I began searching through the Java api and then searched on google to no avail. Then you had <= 9 and I just assumed I got le trolled!

You took an interesting approach and it makes for a stronger password. You require the user to enter in at least one of each. OP said it can contain only a-z A-Z 0-9 not that it required each. I entered in "As1!$@%#%$" and the output was "You successfully entered your password!" Which again makes for a stronger password and overall a better program but breaks OP's rules.
Quote Originally Posted by Cosmo_ View Post
I do humbly apologize good sir. In your original post you had char.long and that made me curious. So I began searching through the Java api and then searched on google to no avail. Then you had <= 9 and I just assumed I got le trolled!

You took an interesting approach and it makes for a stronger password. You require the user to enter in at least one of each. OP said it can contain only a-z A-Z 0-9 not that it required each. I entered in "As1!$@%#%$" and the output was "You successfully entered your password!" Which again makes for a stronger password and overall a better program but breaks OP's rules.
No worries it is not a big deal, I was just literally lazy to explain it so I just gave keywords and such.

Yes, it breaks OP's rules, but it literally has the same concept when asking for restrictions.
I apologize that it took me so long to get this up. I suppose if I didn't get on my GameCube then go outside with my friends I would have responded sooner, but the point is I have it before a weeks time (I tend to put things off like my HW that I should be doing now :P)

This is my source code and I honestly do think it is interesting to see how you have coded it differently Cosmo_. If you wouldn't mind telling me how you learned Java I would be very interested to hear about that. I'm thinking about self teaching myself either Python, C, php, or maybe even perl. Anyone have any suggestions on my next language to learn, or tips on learning one of the previous 4 listed? Thanks if you have any input. Everything is greatly appreciated.

Code:
import java.util.*;
public class Password
(
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        boolean valid = true;
        System.out.println("Please enter a pasword: ");
        String password = input.next();
        int i = 0;
        if (password.length() < 8){
            System.out.println("Password must be at least 8 characters.");
            valid = false;
        }
        while (i < password.length() && valid == true){
            if ((password.charAt(i)>='a' && password.charAt(i)<='z') || (password.charAt(i)>='A' && password.charAt(i)<='Z') || (password.charAt(i)>= '0' && password.charAt(i)<='9')){
                valid = true;
                else (
                    valid = false;
                    System.out.println("Password must contain letters or numbers only. (a-z, A-Z, 0-9");
                    }
                }
                i++
                }
                if (valid == true){
                    System.out.println("Password Accepted!");
                   }
           }
}

*Edit* I'm annoyed with how it doesn't include the spaces I added in with the } etc. I'm new to posting here so I don't know how to make it appear with the spaces.
I signed up for a Java class at my local community college and a week before the class I felt as though everyone in my class would be programming masters so I began reading books/watching videos. It was an intro class and next week are finals and we only reached up to learning about arrays . I fell in love with programming and try to learn as much as I can about it.
Those are really in demand languages to know right now! For game hacking I suggest C. To put your code in the blocks u have to put [.code.] and ending [./code.] without periods.

Edit: Your code is good. :]
Posts 111 of 11 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?