• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

extracting String characters using a for loop.

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why when using an editor do I get the error:
Error: line 4 - cannot find symbol - method charAt(int)
I am trying to extract the integer characters from the String to then Parse them into Integers.
I am just using println() to check character extraction.
 
Marshal
Posts: 72905
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Gerard wrote:. . . Error: line 4 - cannot find symbol - method charAt(int) . . .

Your object hasn't got a charAt() method. You can't simply call that method. You have to precede it with a reference to the object you are calling it on: in this case num. Since I have seen the name num applied to numbers, I don't think it is a good name for a String.

You won't get the numeric values printed like that; you will have to convert the chars to ints, which you can do with a cast or the rarely used unary + operator.
 
Campbell Ritchie
Marshal
Posts: 72905
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't try parsing chars into integers; they already are integers.
 
Rancher
Posts: 4306
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Parse them into Integers.


I assume the OP is trying to convert '1' to 1.  
One way is to subtract '0' from the character value.
 
Campbell Ritchie
Marshal
Posts: 72905
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also a method in the Character class to do the same.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic