File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't analyse the result

 
mony monk
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic