Java programming question

11masoodt

Senior Member
Joined
Jan 12, 2008
Messages
794
Reaction score
0
Location
Joburg
i'm fairly new to programming (only started this year) and i need some help on something that i'm making. the task is to write a Java program to ask the user for an ID number (RSA) and to validate the ID number and tell the user if the number they entered was valid or not. The rules for valid ID numbers are:

* Calculate total A by adding the figures in the odd positions i.e. the first, third, fifth, seventh, ninth and eleventh digits.
* Calculate total B by taking the even figures of the number as a whole number, and then multiplying that number by 2, and then add the individual figures together.
* Calculate total C by adding total A to total B. The control-figure can now be determined by subtracting the ones column from figure C from 10. Where the total C is a multiple of 10, the control figure will be 0.

i wrote te following code:

public class IDNumberVerification
{

public static void main (String [] args)
{

String idNumber;

idNumber = JOptionPane.showInputDialog("Enter an ID number");

int totA = 0;

totA = totA + idNumber.charAt(0) - (int)'0';
totA = totA + idNumber.charAt(2) - (int)'0';
totA = totA + idNumber.charAt(4) - (int)'0';
totA = totA + idNumber.charAt(6) - (int)'0';
totA = totA + idNumber.charAt(8) - (int)'0';
totA = totA + idNumber.charAt(10) - (int)'0';


int totB = 0;


totB = idNumber.charAt(1) - (int)'0';
totB = idNumber.charAt(3) - (int)'0';
totB = idNumber.charAt(5) - (int)'0';
totB = idNumber.charAt(7) - (int)'0';
totB = idNumber.charAt(9) - (int)'0';
totB = idNumber.charAt(11) - (int)'0';





}

}

now for totB i want to display those numbers as a whole number as in instruction number 2. example ID I'm using: 9301285276088
 
Are you speaking about adding the individual figures of the total together? I haven't looked at Java for over 10 years, so I won't attempt to write anything here, but look at the modulus/remainder (%) and div (/) operators.

28 * 2 = 56
56 / 10 = 5
56 % 10 = 6
total: 5 + 6 = 11

Something like that?

EDIT: Or are div and / different things? Can't remember. :o

Maybe it's 56 div 10?
Otherwise:
56 % 10 = 6
56 - 6 = 50
50 / 10 = 5
5 + 6 = 11
 
Last edited:
I think he means make it 1 large number like 3+1+8+2+6+8 = 318268 ? If that is the case then add it as a string and then convert that answer to an integer and what does " - (int)'0' " do? surely that subtracts zero which does nothing? Another thing is that your totB isn't changing you're just assigning a new value to it every time so at the end you'll get totB = idNumber.charAt(11) - (int)'0'; as your output so it should be

String stotB = "";

for(int k=1;k<idNumber.length();k=k+2){
stotB = stotB + idNumber.charAt(k);

}
which will give your stotB = "318268" then just convert that to an integer so

totB = Integer.parseInt(stotB);

Then go from there...If that was what your question was?
 
I think he means make it 1 large number like 3+1+8+2+6+8 = 318268 ?

Yes, I see now. The strings will have to be concatenated as you suggested. The mod can be used when adding those individual digits up, as in the next part.
 
Top
Sign up to the MyBroadband newsletter
X