#idk what i did wrong
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
It's easier if you share code here using:
Please use this format for posting code:
```java
// Example java program
int value = 5;
System.out.println(value);
```
Which results in:
// Example java program
int value = 5;
System.out.println(value);
For syntax highlighting, you have to add the name of the language after the three backticks, like ```java. Please make sure to use exactly this format, so no space between the backticks and the language name, and a newline before the code starts. If done right, the syntax highlighting will even be applied to your text as you type, before sending.
But for non primitives you need to use .equals, not ==.
i had it working befor but now it refuses to run the if statement i took a screen shot of when it worked and compared it to now and it is the exact same
'''java private static final String TheOne21 = null;
private static Scanner sc;
public static void main(final String[] args) {
sc = new Scanner(System.in);
System.out.println("please enter your password: ");
String passWord =sc.nextLine();
if (passWord .equals(TheOne21)) {
System.out.println("You may enter");
} else {
System.out.println("Please try again");
}
would you be able to plug it in and see if it works?
It's 3 ` not "
You have a space after passWord.
Variables should be camelcased, and the thing you compare it to is null (TheOne21) so it will always be false.
Or if it's a constant, uppercased.
I uploaded your attachments as Gist.
TheOne21 if it's on class level should be: THEONE21, and I'd recommend something more descriptive like EXPECTED_PASSWORD.
And passWord.equals(TheOne21) currently translates to passWord.equals(null) which will never be true.
You haven't assigned a value the TheOne21.
so for TheOne21 should i set it to constant, local variable, field or perameter
and sorry im only 2 weeks in i just dont understand why this worked perfectly earlier and now it doesnt
any idea why it worked befor?
im not getting errors
In this case I'd expect a constant, but do assign it a value.
And I'd guess previously you assigned it a value.
And you won't get an error, it will just never be true.
Currently you just basically have if(1 == 2)
You just need to assign whatever password you expect to ThisOne21 and enter it.
private static final String TheOne21 = "MY EXPECTED PASSWORD"
Yes, and in this it's a constant.
if (passWord .equals(TheOne21)) { <= passWord is the name of the variable you assign the user's input to, the right hand side is the constant value.
i make it constant and then the new password = null
It's equivalent to:
if (passWord.equals("MY EXPECTED PASSWORD")) {
Which would be the same as:
if (sc.nextLine().equals("MY EXPECTED PASSWORD")) {
its just sending me in a constant never ending loop
every single time i add something i have to add it all over again
and again
constantly
what do you have so far
I uploaded your attachments as Gist.
umh...
you are assigning the variable to itself.
private static final String TheOne21 = ThisOne21;
You're not assigning it a String value.
Do you mean:
private static final String TheOne21 = "ThisOne21";
?
No problem, we all have to learn.
how would i make this to where theres 5 attempts to enter the correct password?
you could do a for loop
I uploaded your attachments as Gist.
you need brackets after the for
{} these brackets?
yes
so for (int i = 0; i < 5; i++) {}
the code that you want repeated goes inside of the brackets
also this will never run
you declared a variable with the number 5 and the loop only runs if the variable is below 0, which is never
boolean passwordFound = false;
for (int i = 0; i < 5 && !passwordFound; i++) {
System.out.println("Please enter your password - attempt #" + i+1);
String passWord = sc.nextLine();
passwordFound = passWord.equals(TheOne21);
}
if (passwordFound) {
//
} else {
//
}
so thats stating that if the password is false it reduces the number of attempts by 1 i appriciate it i was trying to put the expression after else but that just printed please try again 5 times haha
i really appreciate you guys sorry to take up so much time
No, it's an early exit condition. It will stop once there have been five tries OR the correct password has been found.
perfect thank you
took me a bit to get it to start at 1 and not 11 tho but i worked it out
No problem, good luck!