aspose file tools*
The moose likes Beginning Java and the fly likes can't analyse the result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't analyse the result

mony monk
Greenhorn

Joined: May 02, 2007
Posts: 3
Hi
I just started preparing for java. I wrote a simple program nad could not work the result. can anyone explain
class Example{
public static void main(String args[]) {
int grain = 0;
int grain1 = 1;
char count_1 = 'a';
System.out.println(grain + grain1 + count_1);
}
}
The result I got is 98
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi,

welcome to the ranch.

a char (character) is also an integer value (not to confused with an Integer). Take a look at the ascii table: http://www.asciitable.com/

Im sure you'll figure out, why the result is 98. If not, tell me what
you found out when you looked at the ascii table and I'll give you another hint.

/Svend Rost
mony monk
Greenhorn

Joined: May 02, 2007
Posts: 3
Thank you.
So it all got added.
If i want them to display as it is It seems I need to seperate them in some form
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Yes, they are "seen" as integers - and since the ascii value of a is 97 the result is 98.

Prasanna: If i want them to display as it is It seems I need to seperate them in some form
True. This can be done in several ways. The + operator in the following statement: a+b can either be an arithmetic operator or one concatenating strings (putting them together). Which type it is depends on the types on its left and right hand.

In your example + sees grain, grain1 and count_1 as integers. I'll show you how to use + to concatenate strings.



Im sure you can figure it out if you experiment abit.. else, reply back and I'll give you another hint

Happy coding.

/Svend Rost
[ May 02, 2007: Message edited by: Svend Rost ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't analyse the result