Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Why do i need to take 48? char to int.

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question 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. 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!

     //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);
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. The Following User Says Thank You to copeg For This Useful Post:

    Scotty (October 29th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why do i need to take 48? char to int.

    Quote Originally Posted by Scotty View Post
    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. 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!

     //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).

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why do i need to take 48? char to int.

    Quote Originally Posted by javapenguin View Post
    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).

  6. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default 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.
    int x1 = cx1 - '0';

    A better way might be to construct a BigInteger using the String value and work with that.

    db
    Last edited by Darryl.Burke; October 28th, 2010 at 03:14 AM. Reason: code line added

  7. #6
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Why do i need to take 48? char to int.

    Quote Originally Posted by javapenguin View Post
    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

  8. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Why do i need to take 48? char to int.

    Quote Originally Posted by copeg View Post
    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.

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    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:
    System.out.println("Value: "+5+9);
    Will print out Value: 59.
    While this:
    System.out.println("Value: "+(5+9));
    Will print out Value: 14.

    So, instead of the code you have above, you can simply say:
    JOptionPane.showMessageDialog(null, (x1-48) +"-" +(x2-48) +(x3-48) +(x4-48) +"-" +(x5-48) +(x6-48) +(x7-48) +(x8-48) +(x9-48) +"-" +c);
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM
  2. coverting to Char?
    By JavaNoob82 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 16th, 2010, 07:52 AM
  3. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 11:13 PM
  4. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM
  5. char (toUppercase?)
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 12th, 2009, 10:01 AM