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

String = char -- didnolikethis!

 
Jim Hooper
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys, maybe i'm trying to simplify things too much but why does this code produce an error?

public static void main(String[] args)
{
Stringnumword = args[0];

Stringstemp;

stemp = numword.charAt(1);
...............

Again any help guys much appreciated.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is a strongly-typed language. The charAt() method returns a char, which you are trying to assign to a String reference variable. You can do one of several things, including changing your variable type to that of char or using the String.valueOf(char c) method to convert the char from charAt() to a string.

Hope this helps....
 
Steven Bell
Ranch Hand
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure sure if I answered this with the previous post, but here it goes.

the charAt() method returns a variable of type char which you are trying to assign to a String reference. wrong type.

Also you should not be making so many posts, these questions have been related to the same problem and you should have just asked them in the same thread, much less confusing. Also if you keep creating extra posts for essentially the same/similar topic some people may become annoyed and be less lickely to help you.
 
Jim Hooper
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AH! Many thanks Steve sorted it out a treat... I did try changing the type from String to Char but that seemed to cause an error as well but the String.valueOf() is top draw.
 
Jim Hooper
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies Steven,

Firstly, you did answer my question but then I assigned it to a String, which I now know you can't do.

The reason I reposted rather than replied was because my query had moved on and felty it was a different issue, as proved. I will note my posting habits in future though and apologise.

Forgive an over eager mind?
 
Dean Jones
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
char, not Char. Watch the case on the c.

This works just fine.



[ January 05, 2005: Message edited by: Dean Joness ]
[ January 05, 2005: Message edited by: Dean Joness ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic