Forums Register Login

How to find length of characters from user input

+Pie Number of slices to send: Send
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!

+Pie Number of slices to send: Send
 

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?

Henry
+Pie Number of slices to send: Send
Let me try that out and see if it works
+Pie Number of slices to send: Send
Yeah It doesn't work
+Pie Number of slices to send: Send
 

Junaid Mahmud wrote:Yeah It doesn't work


It should work. Please post your modified code.
+Pie Number of slices to send: Send
These are the relevant lines of code:
............
Can you explain why you think this would work the way you intend for it to work?
+Pie Number of slices to send: Send
 

Junilu Lacar wrote:These are the relevant lines of code:...

And:

+Pie Number of slices to send: Send
 

Junaid Mahmud wrote:


I would have written this as:

This allows for some future flexibility. Today it's 5 character length, tomorrow it may be something different.
+Pie Number of slices to send: Send
I am still having troubles as I cant get it to work. This is so exhausting lol.

+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
I honestly have no clue what I am doing lol. I did try your method, but for some reason it wasn't working.
+Pie Number of slices to send: Send
Not sure what you are trying to do here but this will give you a compile error because it's not a boolean expression.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send

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.
+Pie Number of slices to send: Send
Okay, I rearranged some parts, but still cant get it to work. Thanks
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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"



+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
Ugh This is so frustrating
+Pie Number of slices to send: Send
Is this a little better? Am I almost there?



+Pie Number of slices to send: Send
I give up! I have been trying to do this one thing for the past 6 hours and cant figure it out.
+Pie Number of slices to send: Send

You are not doing anything with the parameter named "string".
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1235 times.
Similar Threads
I am having some trouble
Missing Output While Incorporating A Class
Developing UNO game in java
learning how to use classes
problem with while loop
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:47:36.