Solved

Casting char to int

Posted on 2003-11-27
8
1,870 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
map related example 6 55
jboss wildfly 10.1 10 228
ejb message driven bean mdb creation steps 2 22
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

856 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