• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

My loop isnt working second time round

 
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

a program to take a car registration number in
format is
12dd2345

numbers after the letters can go up to 8

the problem is that when 06fg22343 is put in the first time it works

addsadsad enterd, returns invalid
06fg22343  can be put in and it works

but as soon as numbers are entered
1dd454355345

or
22332131321

seems like numbers just stop a legitimate reg number being put in after the loop runs again

please give me clues, i really want to think about this

i tried printing the carRegNumber in various places to see if it was repeating something from the previous scanner or similar issue

but im not too versed with loops like this

thanks for reading

 
Saloon Keeper
Posts: 8018
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marshal
Posts: 3458
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to Carey's comment, I would also suggest that you extract the validator code in to a separate method (maybe a separate class) which you can test and debug independently of the main application.

 
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also a lot of if's, a switch might be cleaner
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I just need to move int loopCount = 0; into the do loop,can't believe I missed that.

I understand what you saying about a new class etc but this is only a small assignment program but I will keep it in mind

Hey Daniel yes I will do that and try to use a switch

Thanks all
 
Sheriff
Posts: 16131
268
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne brandon wrote:I understand what you saying about a new class etc but this is only a small assignment program but I will keep it in mind


The best time to learn how to write clean code is when you have small problems. When you don't already have the habit of breaking apart a problem like this into smaller, more manageable tasks, taking on a larger problem is going to be even more difficult.

Frankly, what you have now can already be considered a big unreadable mess.
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
junilu i realise that unfortunately my tutor said we can only use 3 methods in this program, so i have to follow instruction
but i will be doing more examples like this in my own projects and will apply to what is being said.

 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne brandon wrote:junilu i realise that unfortunately my tutor said we can only use 3 methods in this program,


*le sigh* When will instructors stop telling their students things like that.
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know Knute

on top of it we have to use bytes for days of the week
shorts (Campbell knows all about them)
floats for moneys
chr ...etc etc

thank goodness i have you keyboard cowboys to guide me!!!
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you following that course?
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel its a bootcamp type thing teaching java and python at a local community college in the republic of ireland
 
Daniel Demesmaecker
Rancher
Posts: 1170
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No offence, but I think you're better of on learning on your own...
At least you're aware of the bad practices they learn you...
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guys can you please explain these lines to me

i dont really understand them in the loop

 
lowercase baba
Posts: 12974
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne brandon wrote:hey guys can you please explain these lines to me

i dont really understand them in the loop


Formatting is very important to make it easy for others to read your code.  I think it should be formatted like this:



What it is doing is looping through every character in the CarRegNumber String, on index of i.  If you are looking at the 3rd or 4th character (with are index positions 2 and 3), you test to see if the character is a letter.  at every other position, you are testing to see if it is a digit.   Based on your original post, that's the format we are looking for...

DDCCDDDDD

where D is a digit 0-9, and C is a character of the alphabet, A-Z.
 
Rancher
Posts: 531
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all it has to say is invalid, then you can make a regex and it will be way simpler  something like: "\\d{2}[A-Za-z]{2}\\d{6,8}"
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but how would i implement that Al? i tried reading the docs,it was like french to me.
 
Marshal
Posts: 72472
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne brandon wrote:. . .  i tried reading the docs,it was like french to me.

Which documentation did you find so difficult? Didn't I give you a link to the Java™ Tutorials a few weeks ago? I thought that was easy to read.
 
Al Hobbs
Rancher
Posts: 531
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regex is very intimidating at first.  When i first saw it it seemed pretty random to me.   But using regex is so nice once you kinda understand it.  It might be a little bit too hard for now,  but i think the earlier you are exposed to a harder thing the better.
 
Al Hobbs
Rancher
Posts: 531
6
IntelliJ IDE Spring Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regex is basically telling it what to look for in each part of the string.   If i do "hello"  then it will match only if it finds each of those letters in order like how its written.   The \\d means any kind of number.  The {2} modifies the \\d to say that there should be 2 of them.  The [] means anything inside of it is ok.  

Then the {6,8} means 6 to 8 of the thing before it.
 
Carey Brown
Saloon Keeper
Posts: 8018
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To disect "\\d{2}[A-Za-z]{2}\\d{6,8}":

Note that the double backslash is a requirement for Java String literals, the actual regular expression only requires a single backslash.
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Campbell there was a lot to it, didnt know what was pertinent to my situation, sorry.

thanks guys

will definitely look at it as soon as i have this assignment handed in.

 
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic