?
Solved

Casting char to int

Posted on 2003-11-27
8
Medium Priority
?
1,877 Views
Last Modified: 2012-05-04
Hi,

I have this piece of code, but cannot compile it, as I receive the following error:

'unexpected type' on this line: num = int(innum);

Why is the casting not correct?

Code follows:

class test {

public static void main(String[] args) throws Exception
{
char innum;
int num = 0;

try
{
System.out.println("Enter a number:");
innum = (char)System.in.read();
System.in.read();
System.in.read();

num = int(innum);
num = num + 2;
System.out.println(num);

}
catch(Exception e)
{ System.out.println("Problem reading string input, program will exit");
System.exit(0);
}

}
}

Regards
0
Comment
Question by:barnarp
8 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 9831549
num = (int)innum ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9831551
> num = int(innum);

is the pascal way of casting :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9831658
You don't need to cast:

char c = 'a';
int a = c;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:TimYates
ID: 9831825
true, but it takes all the imlication out of it :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9831827
(and the spelling apparently) ;-)

imlication = implication

*sigh*
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9832503
Why bother converting the System.in.read() line to a char in the first place, if you are simply converting it back to an int later?

Unless of course you actually want the int value of what the read character is (i.e. 9 instead of 57 (or whatever the number is, I can't remember...) for '9', in which case, you'd probably want to do:

num = Integer.valueOf(String.valueOf(innum));
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9832834
Better replace:
innum = (char)System.in.read();
System.in.read();
System.in.read();
num = int(innum);

with
num = System.in.read() -'0';
System.in.read();
System.in.read();

This way in num you'll have the number that is entered (if it is between 0 and 9 but I suppose so as you cast to char)
0
 

Author Comment

by:barnarp
ID: 9835485
Thank you very much for all your replies.

All very valid, but I think TimYates was the simplest and also answered first.

I guess that is what you get when a Delphi programmer tries to code java.

I actually wanted integer code instead of ascii, so the following code is actually better and does exactly what I want.

import java.io.*;

class test {

   public static void main(String[] args) throws IOException,Exception
   {
  int num;
 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
 String digitstring;
 
 try
 {
    System.out.println("Enter a number:");

    digitstring = stdin.readLine();
    num = Integer.parseInt(digitstring);
   
    num = num + 2;
   
    System.out.println ("You calc: \"" + num + "\"");

 
 }
   catch(Exception e)
   {   System.out.println("Problem reading string input, program will exit");  
       System.exit(0);
   }

}
}

Regards


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question