Hey! I am trying to get the length of characters that the user prints. If the user prints a length less than 5 or greater than 5 It should say "Enter a valid 5 digit number" If the length is 5 I want it to go on the next question. Thanks!
Junaid Mahmud wrote:Hey! I am trying to get the length of characters that the user prints. If the user prints a length less than 5 or greater than 5 It should say "Enter a valid 5 digit number" If the length is 5 I want it to go on the next question. Thanks!
Since you already have the value in your program as an int, isn't it just easier to make sure that the number is greater than 9999 and less than 100000?
Junaid Mahmud wrote:I am still having troubles as I cant get it to work. This is so exhausting lol.
At line 7... Your cardnumber variable is now a string variable instead of an int. Interesting choice. Why did you just abandon fixing via my last set of recommendations? Anyway, this variable is set to null.
At line 13... You now have another string variable named aString. Not sure of the purpose, but it has the same value as cardnumber, which is also null, and...
At line 15... You declare a new length variable. This variable will be set to the length of the aString instance. And since aString reference is set to null, this should throw a NullPointerException.
Not sure what you are trying to do, but you can't take a string length when there isn't a string yet.
It seems like you are assuming that what you wrote is what you intended and that you are not looking at the statements being executed in top-down order. Perhaps making a printout for yourself and covering it up with a blank sheet of paper that you then move down one line at a time so that you're not jumping ahead. Determine what each line is actually doing, in order. Pretend that you are the computer.
I have changed my code to when I first uploaded this post. I have no reason why this code will not work. It says "Enter a valid number" every time even if the length is 5.
Aside from the fact that you are calling the length() method but you haven't finished that method yet and it always returns zero. You are not looking at the ORDER that you are doing things. You enter the card number at line 6, but you calculate its length at line 1. How could that ever work if you take them in order.
I changed the return to 5 because I that is length of the card number that is required. This made the code print out every number and ignores the code that I made for if length less than 5 "Enter a 5 digit code"
Junaid Mahmud wrote:I changed the return to 5 because I that is length of the card number that is required. This made the code print out every number and ignores the code that I made for if length less than 5 "Enter a 5 digit code"
The length() method is *not* supposed to return what is required -- it is supposed to return what it actually is. So, if you only return 5, then the length is always considered as 5, regardless of what it actually is.
Junaid Mahmud wrote:. . . Am I almost there? . . .
No
That main method is far too long. You probably have ten methods' worth of code there.
The technique for printing book/DVD in lines 103‑121 looks very non‑object‑oriented to me. Why don't you have book and DVD objects? Why not create an enum which can be used in a switch statement?
Junaid Mahmud wrote:I give up! I have been trying to do this one thing for the past 6 hours and cant figure it out.
Perhaps it would be a good idea to tell us what you are trying to do with each change -- meaning what are your thoughts and why do you think it would work? I have to say, and trying not to be insulting, your changes seem almost random. And because of that, it is very difficult to figure out where you are getting confused, and hence, very difficult to give you a hint in the right direction.
Don't know if you've baled on this or not. Here's something that I try when my project seems to get hung up on one issue. I make a copy of the project's code and then remove all code not pertaining to the issue. This makes it easier to see and debug the problem. Here's your code after it has been trimmed down.
Post by:autobot
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
a bit of art, as a gift, the permaculture playing cards