#idk what i did wrong

1 messages · Page 1 of 1 (latest)

wide orchid
#

i do not understand why this is skipping my if statement i have rewrote it and i cant get it to work

elfin nimbusBOT
#

<@&987246399047479336> please have a look, thanks.

bronze verge
#

It's easier if you share code here using:

elfin nimbusBOT
#

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.

bronze verge
#

But for non primitives you need to use .equals, not ==.

wide orchid
#

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?

bronze verge
#

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.

wide orchid
#

whats not camelcased? and what do i compair it to?

elfin nimbusBOT
bronze verge
#

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.

wide orchid
#

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

bronze verge
#

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)

wide orchid
#

i cant figure out how to make it true

#

i cant asign it ThisOnw21

#

ThisOne21

bronze verge
#

You just need to assign whatever password you expect to ThisOne21 and enter it.

#

private static final String TheOne21 = "MY EXPECTED PASSWORD"

wide orchid
#

ohhhhhhh

#

but whatever i put there is wants me to make a constant or field'

bronze verge
#

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.

wide orchid
#

i make it constant and then the new password = null

bronze verge
#

It's equivalent to:
if (passWord.equals("MY EXPECTED PASSWORD")) {

#

Which would be the same as:
if (sc.nextLine().equals("MY EXPECTED PASSWORD")) {

wide orchid
#

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

low slate
#

what do you have so far

wide orchid
elfin nimbusBOT
bronze verge
#

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";
?

wide orchid
#

im an idiot

#

im so sorry to waste your time god i feel dumb

bronze verge
#

No problem, we all have to learn.

wide orchid
#

how would i make this to where theres 5 attempts to enter the correct password?

low slate
#

you could do a for loop

wide orchid
#

when this worked befor i added

elfin nimbusBOT
low slate
#

you need brackets after the for

wide orchid
#

{} these brackets?

low slate
#

yes

#

so for (int i = 0; i < 5; i++) {}

#

the code that you want repeated goes inside of the brackets

low slate
#

you declared a variable with the number 5 and the loop only runs if the variable is below 0, which is never

bronze verge
#
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 {
  //
}
wide orchid
#

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

bronze verge
wide orchid
#

perfect thank you

#

took me a bit to get it to start at 1 and not 11 tho but i worked it out

bronze verge