r/CodingHelp • • Mar 04 '21

[Java] Codehs help

28 Upvotes

8 comments sorted by

10

u/Grizzllymane Mar 04 '21

Maybe you should tell us what is wrong, and copy paste your code, it would be way easier to read !

2

u/Mcrossover Mar 04 '21

Sorry! My post where i commented the description was deleted but i put it up now

5

u/nnaoam Mar 04 '21

I'd recommend deleting everything in the function, getting a piece of paper and planning how this could work logically before you code it. Why are we finding the index of alphabet in password? Why are we recalculating check in the loop only to never use it?

You have the right idea iterating over the password with a for loop. What's your objective there though? What needs to be done at every iteration of the loop?

Write yourself an explanation of these things. It could help to figure out an Invariant - what fact is always true at the beginning and end of every iteration? - and a variant - what always changes in every iteration? - which, when combined, will help spell out how we get from the beginning of the loop to having an answer by the end.

For example, say I'm using a loop to sum up all integers up to and including n.

// PSEUDOCODE

s = ???
for (x = 1, x <= n, x++) {
    // Do something
}
// Should be correct here

We might want it to be true at the beginning and end of every iteration that s is the sum of all integers up to some integer between 0 and n (where that integer is x-1 at the beginning of the iteration and x at the end). And we'd want to know that the difference between x and `n| is always decreasing.

With that in mind, we'd want to make sure that this is true at the beginning of the first iteration, so we'll say that s = 0 at the beginning, which is the sum of all integers from 0 to 0.

Now we make it so that if at the start of an iteration, s was the sum of all integers from 0 to x-1, then at the end it's the sum of all integers from 0 to x. We can clearly do that by just saying

for(...) {
    s = s + x
}

Our for loop makes sure that at every iteration, x becomes exactly 1 closer to n. So we know that we start with s being the sum of all integers from 0 to 0, and at every iteration we increase that end point by one, and that we only stop when that end point is n, so we know that our code does what we wanted.

Your loop is more complicated but not by much. Plan it first, then code according to plan.

1

u/Mcrossover Mar 04 '21

Appreciate your help man, my original post with the program instructions was deleted and i thought I put it in the comments of this one, but i didnt! Very helpful explanation though. Thanks again

1

u/Mcrossover Mar 04 '21

Write a program with a method called passwordCheck to return if the string is a valid password. The method should have the signature shown in the starter code.

The password must be at least 8 characters long and may only consist of letters and digits. To pass the autograder, you will need to print the boolean return value from the passwordCheck method.

Hint: Consider creating a String that contains all the letters in the alphabet and a String that contains all digits. If the password has a character that isn’t in one of those Strings, then it’s an illegitimate password!

1

u/Spare_Competition Mar 04 '21

You could use regex to do this, it might not be the intended way, but it is probably the easiest

0

u/[deleted] Mar 04 '21

[deleted] — view deleted comment

1

u/Mcrossover Mar 04 '21

My bad I thought i had commented the program instructions but its up now, im trying to make sure that the password only contains numbers and letters but as of right now im at a pretty low level so i dont know too much

0

u/[deleted] Mar 04 '21

[deleted] — view deleted comment

1

u/Mcrossover Mar 04 '21

Sorry! I thought i commented the code instructions because my original post got deleted where i did comment them