• 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
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

A very simple question I can't solve (about converting char to int)

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys, i have a question the im struggling  to solve by myself and it goes like that:

The program will read from the user 3 notes from type char which are digits.
The program will create variable from type int that will consist the digits that will become a number.
The program will print the number on the screen.

E.g- the 3  digits are : 4,5,6 and the number is 456.

I have tried to write the following code:


import java.util.Scanner;
public class Exercise_2
{
   public static void main(String[]args)
   {
       Scanner scan= new Scanner(System.in);
       char ch1,ch2,ch3;
       System.out.println("Enter 3 digits:");
       ch1=scan.next().charAt(0);
       ch2=scan.next().charAt(0);
       ch3=scan.next().charAt(0);

      int result;
      result=(100*ch1)+(10*ch2)+(1*ch3);
      System.out.println(result);
   }
}


But the program dont give the result i want to get and i dont know what is the problem.

please help me
 
Marshal
Posts: 79827
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the code button on your posts; the code will look so much better.

That question isn't as simple as you think, but there is a solution. So you write 4 5 6 and it adds up to 5784 or something like that, does it? When you pass a char like 'a', does the PC store 'a' or "a" in its memory? Of course it doesn't. It stores 97 (or 0x0061). The same applies to passing a char representing a digit. It doesn't store 4 anywhere. If you look for an ASCII table, for example this one, you will find that what is stored is totally different. There are several ways to sort it out, if you insist on using chars.
  • 1: Using the values in the ASCII table, do some arithmetic to convert '4' to 4, etc.
  • 2: Find the wrapper class corresponding to chars, and go through its methods to see if you can't find something to find a numeric value.
  • There are doubtless other ways to do this.
     
    rian bron
    Ranch Hand
    Posts: 117
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Please use the code button on your posts; the code will look so much better.

    That question isn't as simple as you think, but there is a solution. So you write 4 5 6 and it adds up to 5784 or something like that, does it? When you pass a char like 'a', does the PC store 'a' or "a" in its memory? Of course it doesn't. It stores 97 (or 0x0061). The same applies to passing a char representing a digit. It doesn't store 4 anywhere. If you look for an ASCII table, for example this one, you will find that what is stored is totally different. There are several ways to sort it out, if you insist on using chars.

  • 1: Using the values in the ASCII table, do some arithmetic to convert '4' to 4, etc.
  • 2: Find the wrapper class corresponding to chars, and go through its methods to see if you can't find something to find a numeric value.
  • There are doubtless other ways to do this.



    Thank you for refreshing my mind about the memory saving about char, i didnt really know this before and now i am controlling it 100%.

    I found a solution for my problem and here is is:



    Thank you !
     
    Campbell Ritchie
    Marshal
    Posts: 79827
    388
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So, you are subtracting '0'? I think that is one of the better ways to do it
     
    Saloon Keeper
    Posts: 5546
    213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Or, without doing complex maths, have a look at the method: Integer.parseInt(String).
     
    Ranch Hand
    Posts: 468
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Does the assignment state it has to be a char converted to int?
     
    I've never won anything before. Not even a tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic