|
When posting your question please:- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-fou
|
|
|
|
|
How do you separate large numbers in Java? I want to use something like commas.
I think this works in C++, but it doesn't work in Java:
int onebillion = 1'000'000'000;
Anyone know?
Thanks.
|
|
|
|
|
From Java 7 onwards, you can use the underscore in numeric literals. For instance, 1 million would be represented as 1_000_000. Documentation for this feature can be found here[^].
|
|
|
|
|
|
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.
|
|
|
|