|
I am tryint to validate a password that has the following rules: can't be the same as username, at least 8 characters, and must have a uppercase letter and special character. Any help you can offer would be AMAZING!
import java.util.Scanner;
public class Main {
static String userName = "johndoe";
static String password = "ABC_1234";
static boolean validPassword = false;
public static void main(String[] args) {
System.out.println("Please Change Your Password");
System.out.println("""
Please make sure it is 8 characters long,
Contains an uppercase letter,
Contains a special character,
Does not contain your username,
And is not the same as the previous password
""");
checkPassword();
}
public static void checkPassword() {
System.out.println();
System.out.print("What is your new password? ");
Scanner scanner = new Scanner(System.in);
String newPassword = scanner.next();
while (!validPassword) {
if (newPassword.equalsIgnoreCase(userName)) {
System.out.println("Your password can't be the same as your username");
System.out.print("What is your new password? ");
newPassword = scanner.next();
}
if (newPassword.length() < 8) {
System.out.println("You password must be at least 8 characters");
System.out.print("What is your new password? ");
newPassword = scanner.next();
}
for (int i = 0; i < newPassword.length(); i++) {
char ch = newPassword.charAt(i);
if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) {
System.out.println("Please make sure your password has an uppercase and/or special character");
newPassword = scanner.next();
validPassword = true;
} else {
System.out.println("stop");
validPassword = false;
}
}
}
}
}
modified 14-Jan-24 10:51am.
|
|
|
|
|
if (!Character.isUpperCase(ch) || (Character.isDigit(ch) && Character.isLetter(ch) && Character.isWhitespace(ch))) {
This if clause (and the following statements) does not make much sense as you are testing for multiple conditions existing at the same time. You need to test each character in turn and keep a count of the valid types (upper case, digit, special). Then when all characters have been tested check that each count is greater than zero to signify a valid password.
|
|
|
|
|
Ok... I will try that, I thought you could check for multiple conditions in that way as I saw it done that way somewhere.
|
|
|
|
|
No, it could never be done that way; look at the test:
if (!Character.isUpperCase(ch)
||
(Character.isDigit(ch)
&&
Character.isLetter(ch)
&&
Character.isWhitespace(ch))
So when this test fails (as it mostly will) you request a new password, but then set validPassword = true; , without checking anything else.
|
|
|
|
|
You're not handling your "validPassword" flag in all cases (setting / resetting). And the while loop should be in Main ... calling the check routine (which returns a result) and reading the next try if necessary.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Gotcha. So based on what you are describing, this is the reason the loop is running only once and not continuing until the valid result is met? I will try that. Thank you.
|
|
|
|
|
I am in IntelliJ IDEA. I want to view the disassembly of this code to see if it does two comparisons:
if (-0.0 == 0.0) System.out.println("they're equal");
Based on searching, it sounds like IntelliJ can't do that. Anyone know of a Java IDE that can do this?
Thank you.
|
|
|
|
|
At the command prompt in your directory enter the following command:
javap -c your_program_name
where "your_program_name " is the name of the class containing the compiled code.
If you want more information on the Java development utilities you can find it at Home: Java Platform, Standard Edition (Java SE) 8 Release 8[^].
|
|
|
|
|
In addition redirecting that to a file is probably going to be a good idea. Even a small class is going to have a lot of output.
|
|
|
|
|
True, but even so I doubt it will make a lot of sense to the OP.
|
|
|
|
|
Just curious why/how you expect two comparisons?
|
|
|
|
|
For doubles, I believe there is a positive zero and a negative zero.
|
|
|
|
|
There is only ever a single comparison made for each comparison operator, so if your code only has a single '==' operator, the JIT'd code will only generate a single IL comparison.
-0.0 does occur when a negative floating-point value is so small that is cannot be represented. That does NOT mean it's a different 0 from positive 0.0. You can see this is you try to convert an extremely small value to a string and output it. You can get -0.0.
If your code, the compiler will evaluate the comparison and optimize it out, replacing the expression with just 'true'. The resulting code, and what you see in the disassembly, will be very close to:
if (true) System.out.println("they're equal");
or, depending on code around this, maybe even:
System.out.println("they're equal");
If you want to know how numbers are represented in Java, and most other languages, read The IEEE Standard for Floating-Point Arithmetic (IEEE 754)[^]
modified 12-Jan-24 18:49pm.
|
|
|
|
|
Hi everyone! I want to integrate ChatGPT into my Spring Boot app. How can I do this?
|
|
|
|
|
Go to the ChatGPT website and ask them.
|
|
|
|
|
My first thought was "Why?". My second was yours.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Gerry Schmitz wrote: "Why?" Probably because he doesn't understand ... anything.
|
|
|
|
|
Steps
1. Learn Spring Boot and Java if needed.
2. Get a basic app running in Sprint Boot
3. Figure out what exactly the functionality will be with ChatGPT. There is no code involved in this. You will need to figure out what data goes into ChatGPT and what comes out.
4. Write a test app to work with 3. Use that until you figure out how to do everything for 3.
5. Write new code based on 4, and 2 which does whatever it is that you want.
|
|
|
|
|
I am using IntelliJ IDEA 2023.3.1 (Community Edition), and I'm seeing some weirdness.
I made a simple test.
public class SimpleBreakTest {
public static void main(String[] args) {
String a = null;
a.toUpperCase();
}
}
I put a breakpoint on the first line:
String a = null;
Then, I debugged the program, and it stopped at the breakpoint. Then, I pressed the "Stop" button in the debugger.
Instead of stopping execution, it looks like it continued because it printed out this message:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toUpperCase()" because "a" is null
at SimpleBreakTest.main(SimpleBreakTest.java:5)
Any ideas why the stop button in the debugger doesn't seem to work right?
Thanks.
|
|
|
|
|
... because a to upper without an assignment makes no sense anyway.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Well that is true specifically but in general it isn't relevant. The code is a valid statement. If it was a different method it might do something. And there is no way the debugger should be figuring that out.
|
|
|
|
|
Valid, not valid. He's created an "illogical" scenario what with null assignments, etc. Sorry, I don't blame the compiler or run time. (I frankly have no patience for this type of "nonsense"). And it's hearsay ... Did you "test" it?
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
All of that is true to the extent that it isn't worth spending time on anyways (which I stated in my other post.)
Gerry Schmitz wrote: Did you "test" it?
Not relevant. If the OP can replicate it and I can't then nothing is proven. If I can replicate it then it still doesn't change the fact that it is not worth spending time on figuring it out. Not for me at least.
|
|
|
|
|
Are you certain you pressed the stop button?
|
|
|
|
|
It is of course not really worth figuring it out. And presuming you can replicate it.
But if you are just curious and have time...
Try adding a few more statements and see what happens. Statements before and/or after.
The only thing I can think of is that code optimization is in play. Generally I would not expect that but perhaps that is exactly the cause.
If it is an optimization then I would expect the following might fix it (in that Stop will work.)
String a = null;
int i = 3;
a.toUpperCase();
System.out.println("i=" + i);
|
|
|
|