Solved

Casting char to int

Posted on 2003-11-27
8
1,867 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 50 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
 
LVL 35

Expert Comment

by:TimYates
ID: 9831825
true, but it takes all the imlication out of it :-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception after setting jdbc session management 2 52
JDeveloper 12c for 32 bit 4 71
thymeleaf natural templating vs JSP 2 68
Java Timer (static) 9 36
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now