?
Solved

Casting char to int

Posted on 2003-11-27
8
Medium Priority
?
1,873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

777 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