Solved

Casting char to int

Posted on 2003-11-27
8
1,862 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 50
word0 challenge 3 59
create a gui in perl 3 46
maven project error 5 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

15 Experts available now in Live!

Get 1:1 Help Now