aspose file tools*
The moose likes Beginning Java and the fly likes String = char -- didnolikethis! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String = char -- didnolikethis!" Watch "String = char -- didnolikethis!" New topic
Author

String = char -- didnolikethis!

Jim Hooper
Greenhorn

Joined: Dec 13, 2004
Posts: 13
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

Joined: May 22, 2003
Posts: 657

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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: Dec 13, 2004
Posts: 13
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

Joined: Dec 13, 2004
Posts: 13
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

Joined: Jan 04, 2005
Posts: 8
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String = char -- didnolikethis!
 
Similar Threads
static modifier
Local/Remote implemetation Advice PLEASE !
How many objects are created?
help me with the installation process
help