• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find length of characters from user input  RSS feed

 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try that out and see if it works
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah It doesn't work
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junaid Mahmud wrote:Yeah It doesn't work

It should work. Please post your modified code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are the relevant lines of code:
............
Can you explain why you think this would work the way you intend for it to work?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:These are the relevant lines of code:...
And:

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still having troubles as I cant get it to work. This is so exhausting lol.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I honestly have no clue what I am doing lol. I did try your method, but for some reason it wasn't working.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you are trying to do here but this will give you a compile error because it's not a boolean expression.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I rearranged some parts, but still cant get it to work. Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh This is so frustrating
 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a little better? Am I almost there?



 
Junaid Mahmud
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I give up! I have been trying to do this one thing for the past 6 hours and cant figure it out.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are not doing anything with the parameter named "string".
 
Campbell Ritchie
Marshal
Posts: 56587
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!