This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Cant get only number input  RSS feed

 
Ranch Hand
Posts: 109
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to have a car registration number inputted correctly like so

07ts2322

the numbers after ts can run into as many as they like,but cannot accept a letter after the ts

everything is working well up to ts..after that letters and numbers can be put in and this is not what i want...only numbers should go in.

also those ifs look ugly and it would be nice if i could tidy that up a bit.

heres my code:

 
Marshal
Posts: 62881
203
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider a regular expression; if you have two digits, two letters, and any number (> 0) of digits, that should be easy enough to achieve. There is a nice section in the Java™ Tutorials.
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: don't use a short as a loop index; use an int. In fact, don't use a short full stop.
 
wayne brandon
Ranch Hand
Posts: 109
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell

will check out that tutorial...would that be the preferred way of dealing with this problem?

regarding the short

im busy doing a bootcamp and the instructor insists on using these...but i know better

thanks again
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

wayne brandon wrote:. . . regarding the short

im busy doing a bootcamp and the instructor insists on using these. . .

Shorts are good for wearing in Summer, playing football or swimming with. Don't let your bootcamp instructor think I am telling you to boot him somewhere
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wayne and Campbell, I'm new in CodeRanch, I would like to add my two cents to the problem
but of course Wayne do as your Instructor at Bootcamp tells you, this is just my opinion to the problem and a possible answer.
The best answer anyway is always using Regular Expressions as Campbell mentioned it before.



Marcelo Aravena
 
Campbell Ritchie
Marshal
Posts: 62881
203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for the suggestion, but I can see things I would change about it.
Don't close your Scanner reading System.in; it isn't necessary and can cause problems if you want to read again.
I don't like break; and there is a way you can use your boolean to avoid break; Otherwise you could dispense with that variable.
All local variables must be initialised before they can be used; they don't have defulat values like fields. I can see a potential bug with the iterations counter.
We don't know that there are always two digits before the letters, so the bit about index 2 or 3 might not work correctly.
Only use \n if somebody has said they want the LF character. Otherwise use printf and %n. That will give you the correct line end for your OS.
 
Marcelo Antonio Aravena Lobos
Greenhorn
Posts: 2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell, you are right with Scanner,  \n could be completely avoided as you mention, I just left it as the original code but you are right in your comments
I used break in order to don't iterate over long words let's say a String with hundred of characters, break allow me to just go away from the loop at once, and iterators tells me always how many iterations has been made.
Regarding the characters at zero and one index, if not digits they will go in the else statement and then with a break it won't count it.
There are many ways to approach this code problem as you mention.

Thank you again for your comments and advices Campbell.

It's good to be here at JavaRanch, thanks again guys.

Marcelo Aravena
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!