Why do i need to take 48? char to int.
As you can see I am having to take 48 from each value to get my input - for example if the user puts in 0 it outputs 48, 1 - 49, 2 - 50 etc. It is a temp fix, but not a good one. :confused: Can anyone tell me why? To do with char to int conversion? I am converting (char)numbers to numbers so why 48? I would understand if it was letters to numbers (hex).
Thanks!
Code Java:
//Input
String ISBN =
JOptionPane.showInputDialog("Enter an ISBN-10 number without check digit");
//Values
char cx1 = ISBN.charAt(0);
char cx2 = ISBN.charAt(2);
char cx3 = ISBN.charAt(3);
char cx4 = ISBN.charAt(4);
char cx5 = ISBN.charAt(6);
char cx6 = ISBN.charAt(7);
char cx7 = ISBN.charAt(8);
char cx8 = ISBN.charAt(9);
char cx9 = ISBN.charAt(10);
//char to int
int x1 = cx1;
int x2 = cx2;
int x3 = cx3;
int x4 = cx4;
int x5 = cx5;
int x6 = cx6;
int x7 = cx7;
int x8 = cx8;
int x9 = cx9;
//Check digit
int c = ((x1-48)+2*(x2-48)+3*(x3-48)+4*(x4-48)+5*(x5-48)+6*(x6-48)+7*(x7-48)+8*(x8-48)+9*(x9-48))%11;
JOptionPane.showMessageDialog(null, (x1-48) +"-" +(x2-48) +"" +(x3-48) +"" +(x4-48) +"-" +(x5-48) +"" +(x6-48) +"" +(x7-48) +"" +(x8-48) +"" +(x9-48) +"-" +c);
}
}
Re: Why do i need to take 48? char to int.
Characters abide by ascii format, and digits begin at index 48 (see Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion). If you wish to just grab the integer value, you could use Integer.parseInt or Character.digit
Re: Why do i need to take 48? char to int.
Quote:
Originally Posted by
Scotty
As you can see I am having to take 48 from each value to get my input - for example if the user puts in 0 it outputs 48, 1 - 49, 2 - 50 etc. It is a temp fix, but not a good one. :confused: Can anyone tell me why? To do with char to int conversion? I am converting (char)numbers to numbers so why 48? I would understand if it was letters to numbers (hex).
Thanks!
Code :
//Input
String ISBN =
JOptionPane.showInputDialog("Enter an ISBN-10 number without check digit");
//Values
char cx1 = ISBN.charAt(0);
char cx2 = ISBN.charAt(2);
char cx3 = ISBN.charAt(3);
char cx4 = ISBN.charAt(4);
char cx5 = ISBN.charAt(6);
char cx6 = ISBN.charAt(7);
char cx7 = ISBN.charAt(8);
char cx8 = ISBN.charAt(9);
char cx9 = ISBN.charAt(10);
//char to int
int x1 = cx1;
int x2 = cx2;
int x3 = cx3;
int x4 = cx4;
int x5 = cx5;
int x6 = cx6;
int x7 = cx7;
int x8 = cx8;
int x9 = cx9;
//Check digit
int c = ((x1-48)+2*(x2-48)+3*(x3-48)+4*(x4-48)+5*(x5-48)+6*(x6-48)+7*(x7-48)+8*(x8-48)+9*(x9-48))%11;
JOptionPane.showMessageDialog(null, (x1-48) +"-" +(x2-48) +"" +(x3-48) +"" +(x4-48) +"-" +(x5-48) +"" +(x6-48) +"" +(x7-48) +"" +(x8-48) +"" +(x9-48) +"-" +c);
}
}
int x1 = (int) cx1;
int x2 = (int) cx2;
int x3 = (int) cx3;
int x4 = (int)cx4;
int x5 = (int)cx5;
int x6 = (int)cx6;
int x7 = (int)cx7;
int x8 = (int)cx8;
int x9 = (int) cx9;
Also, you forgot charAt(1).
Re: Why do i need to take 48? char to int.
Quote:
Originally Posted by
javapenguin
int x1 = (int) cx1;
int x2 = (int) cx2;
int x3 = (int) cx3;
int x4 = (int)cx4;
int x5 = (int)cx5;
int x6 = (int)cx6;
int x7 = (int)cx7;
int x8 = (int)cx8;
int x9 = (int) cx9;
Also, you forgot charAt(1).
This will not fix the original posters problem. Casting a char to an int casts the Ascii table value of said character, for the char '0', this is 48, for char '1', this is 49 (see my post above).
Re: Why do i need to take 48? char to int.
And for code readability, you should subtract '0' not 48. That makes it clear exactly why a subtraction is needed.
A better way might be to construct a BigInteger using the String value and work with that.
db
Re: Why do i need to take 48? char to int.
Quote:
Originally Posted by
javapenguin
int x1 = (int) cx1;
int x2 = (int) cx2;
int x3 = (int) cx3;
int x4 = (int)cx4;
int x5 = (int)cx5;
int x6 = (int)cx6;
int x7 = (int)cx7;
int x8 = (int)cx8;
int x9 = (int) cx9;
Also, you forgot charAt(1).
No, I dont need the dash xD
Re: Why do i need to take 48? char to int.
Quote:
Originally Posted by
copeg
This will not fix the original posters problem. Casting a char to an int casts the Ascii table value of said character, for the char '0', this is 48, for char '1', this is 49 (see my post above).
javapenguin frequently posts incorrect solutions to problems. Most of the time he is guessing. It has yet to help a single person.
Re: Why do i need to take 48? char to int.
I know it is already solved, but for future reference:
When adding mathamatics in Strings, it is not necessary to add empty strings between the calculations. In the this code:
Code java:
JOptionPane.showMessageDialog(null, (x1-48) +"-" +(x2-48) +"" +(x3-48) +"" +(x4-48) +"-" +(x5-48) +"" +(x6-48) +"" +(x7-48) +"" +(x8-48) +"" +(x9-48) +"-" +c);
you have added "" between your calculations. When building the String, JAVA will perform all the calculations in the parentheses and then convert it to a String value (effectively). Without those parentheses, JAVA will simply take the numbers as String values.
For example:
This:
Code java:
System.out.println("Value: "+5+9);
Will print out Value: 59.
While this:
Code java:
System.out.println("Value: "+(5+9));
Will print out Value: 14.
So, instead of the code you have above, you can simply say:
Code java:
JOptionPane.showMessageDialog(null, (x1-48) +"-" +(x2-48) +(x3-48) +(x4-48) +"-" +(x5-48) +(x6-48) +(x7-48) +(x8-48) +(x9-48) +"-" +c);