• 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

Problem with char/string representation

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this code for Caesar cipher implementation, which works fine, but when I adapt it for my code it is not working.



Here is my class:


I suppose it is a problem with String conversion, it is showing only the last character,
but why it is working in Console, then ? How do I make it to work in TextArea, as well ?

Could someone please help me with this ? Thank You!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're just getting the current character--you're not building a string containing the built-up ciphertext.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But how than this works in first class ? And how to build string ?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because in the first one you're printing out each character. In the last one you just get the current character.

I think if you're writing Swing apps but don't know how to build a string the learning is happening backwards... Maybe search the web for "java string concatenation".
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for quick response!

I know how to do concatenation. I was just asking for opinion how to do it maybe on another way in this case.
Thanks, anyway.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you're trying to build a string character-by-character. Since Java Strings are immutable, there'd be the overhead of a new string *somewhere* in the process.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I see.

But is there any way to adapt this code written for console to work as I wanted for Swing ? Or it is not possible to work on this way ?
Some way to gather currents to string, or with type cast ?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You said you knew how to concatenate strings--so concatenate away!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using == to check Strings for equality?
Would the StringBuilder class help?
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchie!

Thank You for reply!

I am using == because I am checking which item is selected in Combo box.
In Combo box I have a few types of encryption algorithms and one of them is Caesar.

I just read description about of String Builder class, and will give it try!
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nicol Green wrote:I am using == because I am checking which item is selected in Combo box.



No... in this code

you are comparing two strings. You aren't doing anything with items or combo boxes there.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I was copying code from my project I made mistake,
it should go like this:



Now should be ok, if I understand good what you mean ?

 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. You're still using the == operator to attempt to compare the contents of two strings. Don't do that. Use the equals() method of either of the two.
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I see what you mean, it is beginning Java mistake..

Thanks!

 
rubbery bacon. rubbery tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic