• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String = char -- didnolikethis!

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic