Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with char/string representation

 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
But how than this works in first class ? And how to build string ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said you knew how to concatenate strings--so concatenate away!
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Paul Clapham
Sheriff
Pie
Posts: 20960
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Pie
Posts: 20960
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Now I see what you mean, it is beginning Java mistake..

Thanks!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic