• Post Reply Bookmark Topic Watch Topic
  • New Topic

Character Limit using Scanner Class  RSS feed

 
Anshul Kulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I am working on a school project an I need to set a character limit for a String entered by the user. I am using Scanner class to take the input.

I got a few solutions from my colleagues which can mostly be summarized as ignoring all the characters after the nth character (assuming n is the word limit). But I don't want to do that as I personally feel that it is not user-friendly.
Please Help.

Regards,
Anshul Kulkarni.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want to do that please describe what it is you want to do.

Scanner does not have the ability to limit the amount of characters on a "as-you-type" basis. You can get the full string and then decide how you want to respond to strings that are too long.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshul Kulkarni wrote:I got a few solutions from my colleagues which can mostly be summarized as ignoring all the characters after the nth character (assuming n is the word limit). But I don't want to do that as I personally feel that it is not user-friendly.

You are right. It isn't user friendly at all to accept the input and without telling the user it doesn't conform with defined standards to truncate something and let go like that.

From what you described it sounds that you want to validate the input, and if input doesn't conform with your defined standards, reject it, and ask user once again to enter the input, but this time with providing more information what is expected in the input, so the user would have clearer idea what he's doing wrong.

Do you have any code to show us you got so far?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use a Scanner and still limit the number of characters you process, then probably the most user-friendly you can get is:
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also display something to give the user a visual guide:


 
Anshul Kulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply.

Have you encountered social networking sites, such as Twitter which have a character limit for your posts? They do not allow you to type even a single character one the limit is reached. Yet, you do not need to confirm to get the error from the system about excessive characters. I wanted to do something similar.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obviously, Twitter doesn't use a Scanner. You said you were using Scanner and we've already told you there is no way for you to do that and get the functionality of limiting the input as the user types. You will need to use a different means of getting user input. Is your UI going to be the command line or is it graphical?
 
Anshul Kulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Anshul Kulkarni wrote:I got a few solutions from my colleagues which can mostly be summarized as ignoring all the characters after the nth character (assuming n is the word limit). But I don't want to do that as I personally feel that it is not user-friendly.

You are right. It isn't user friendly at all to accept the input and without telling the user it doesn't conform with defined standards to truncate something and let go like that.

From what you described it sounds that you want to validate the input, and if input doesn't conform with your defined standards, reject it, and ask user once again to enter the input, but this time with providing more information what is expected in the input, so the user would have clearer idea what he's doing wrong.

Do you have any code to show us you got so far?


Sorry I do not have any code ready at the moment to show you. I am still in the phase where I use pen and paper to make a rough draft of the program.
 
Anshul Kulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Obviously, Twitter doesn't use a Scanner. You said you were using Scanner and we've already told you there is no way for you to do that and get the functionality of limiting the input as the user types. You will need to use a different means of getting user input. Is your UI going to be the command line or is it graphical?


I guess it is the command line?? Sorry for not having knowledge about such a simple thing. I am using BlueJ IDE. So whatever the text-box like thing is at the bottom in the terminal window is where the user would enter the string
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshul Kulkarni wrote:Sorry I do not have any code ready at the moment to show you. I am still in the phase where I use pen and paper to make a rough draft of the program.

Well then in that case, I suggest you just write a program that works first. Don't worry about all that user-friendly stuff until you've got a basic program that works. Something basic that works and can be submitted for a grade is much better than a bunch of great ideas that are still only on paper.
 
Anshul Kulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I'll get the basic code ready tomorrow and then think of implementing the character limit.
Thanks a lot again, your quick help is greatly appreciated by me.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshul Kulkarni wrote:I am using BlueJ IDE.

Then don't worry about adding that kind of gold plating yet. Follow the KISS Principle.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anshul Kulkarni wrote:Have you encountered social networking sites, such as Twitter which have a character limit for your posts?

Yes buddy, these usually are JavaScript magic, so called validations on the fly. So, once interactive validation happens when user typing, then there is still validation behind the scenes upon storing such input.
So you are talking about the fancy stuff seen in a browser (web application in simplified words). What we are talking here, we are talking about the application core brains validating the input, which you always need to do anyway.

Now, what else we see often, is that students fairly often misinterpret requirements by not reading them carefully. Do you have such school assignment text, so we could have a look at that particular part is asking about such input validation?
 
Anshul Kulkarni
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Do you have such school assignment particular text?


No, we do not have anything in our text. Our syllabus is just the mere basics of java.
Staff note (Liutauras Vilda):

Quote tags were fixed.

 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand a bit on what Liutauras said.

Twitter looks something like this:
1) Browser (or other front end GUI) - in this bit we can do fancy validation (whether in Javascript or whatever language the app is written in) etc, such as checking character limits.
this sends data to:
2) The Server - this also does validation on the data it is sent (including character limits).  This is because the request sent to the server may not have come from a Twitter app at all, and so may not have been validated.  If it exceeds the limit then it will send an error back.
(from the Twitter Docs for response and error codes)

354
The text of your direct message is over the max character limit.
Corresponds with HTTP 403. The message size exceeds the number of characters permitted in a Direct Message.


How does this compare to what you are trying to do?
Well, for your app, (1) is going to be covered by the terminal.  This doesn't provide you with the facility to interactively control what the user enters (Scanner is just reading what it is sent).  Indeed, you have about as much control as (2) has over it, ie none.
The only bit you can control is (2).  So you need to decide what to do when the user enters too much.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!