• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading in a 16-digit credit card number

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys i have a question to do

Read in a 16-digit credit card number. Output it in the format DDDD-DDDD-DDDD-DDDD where D indicates a digit

now we did this bit of code in class



now i did understand most of it,but not all

the modulas operation didnt make any sense to me what so ever...and we ran out of time before I could really make heads or tails of it.

i would appreciate any input regarding this and i will play with the code and see if i can finish it,but the modulus explained would help a lot

thanks guys
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to one of our Java® fora.

The % operator is called the remainder operator, but many people say modulo. Please look at that link and also the Java™ Tutorials. Remainder is a basic arithmetical concept;when I was seven years old I had to learn that 15 divided by 4 is 3 remainder 3.
Why are you messing around with shorts. I can see no point in the cast.
Why are you using DecimalFormat rather than System.out.printf?
Unless you are specifically being taught about the % operator, I would go back to the drawing board. You are not reading a number anywhere. But you can do that later.  I woiuldn't regard a credit card number as a number, but as text. There are ways to insert hyphens into text, as you will find from this class.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes 1 know what mod is and that its represented by %
i just dont know why she was using 10000

this was her code...i was reading about printf too but we havent used it
we used System.out.format yesterday

i just have to learn this way
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out.format and System.out.printf are more‑or‑less indistinguishable the same.
Can't you work out why you would use % 10000 to split a number into fours?
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Embarrassingly enough yes Campbell my maths is very weak (I'm also working on that now) and although I know what % does I just can't see how it would split up a group of numbers
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it:-
System.out.println(1234567890 % 10000);
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
7890 it gave me the last four digits

but why is that? please feel free to tell me to get lost mr campbell as this isnt a maths tutoring web site lol

thanks for your help so far
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
N % 10000 will give you the remainder of N divided by 10000. That remainder will be in the range of 0 through 9999, the equivalent of the last four digits.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jon,

Mod is a fun thing to do in coding when it comes to numbers, there are a few unique features which I'll discuss after the example below

(Hope my ascii art is good )

Notice that 34 is the last 2 digits from 1234

So, any number % 10 will give you the last digit, and %100 will get last 2 digits and so on ...



 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another useful example of % is when you divide by 2:



Notice a pattern here ?  If a number is divisible by 2, it will return 0 and if not, it will always return 1. This can be used determine if a number is odd or even.

Another cool feature is that the mod will always return a number from 0 to N-1 where N is the divisor. Eg. if your divisor is 3, the mod will be always either 0 or 1 or 2.
This can be used to distribute elements in a Round robin manner. Here's a simple program to demonstrate that:



output:

 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:(...)
Another cool feature is that the mod will always return a number from 0 to N-1 where N is the divisor.


Try  -1 % 2.
 
salvin francis
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Try  -1 % 2.


The negative realm is something I always avoid

Yes, negative numbers break my statement.
 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:yes 1 know what mod is and that its represented by %
i just dont know why she was using 10000 ....



a) yes 1 know what mod is and that its represented by %
b) i just dont know why she was using 10000

FYI a) and b) are contradictory statements; guys here gaves you some pointers if yet you can't connect the definition of modulus to possible use cases take a look also here

 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:1234567890 % 10000
...
7890 it gave me the last four digits

but why is that?


jon, it is really easy to understand, you just need some primitive (might even dumb) example. Ignore what these mathematicians mumble around (just kidding), nobody understands them apart themselves

Imagine you have 10 apples. So what is the remainder 10 % 3...

So you simply do:
10 - 3 = 7
7 - 3 = 4
4 - 3 = 1
1 - 3 = alert, insufficient fonds. You have only 1 left.

So that 1 is the remainder. How it is implemented internally - not our business (probably differently than I described), but that certainly should help to understand what the remainder is.

Is any clearer now how you got 7890? (1234567890 % 10000)
1234567890 - 10000 = ...
... - 10000 = ...
7890 - 10000 = alert, insuf... Déjà vu
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And modulus really help you to extract digits.

Think how dividing and using modulus operator you can extract individual digits. Brute force way to understand is to get calculator and play a bit (with some pretty predictable numbers to use for division, modulus) and see what you get.
 
reply
    Bookmark Topic Watch Topic
  • New Topic