This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with char/string representation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with char/string representation" Watch "Problem with char/string representation" New topic
Author

Problem with char/string representation

Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
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

Joined: Sep 29, 2008
Posts: 12617

You're just getting the current character--you're not building a string containing the built-up ciphertext.
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
But how than this works in first class ? And how to build string ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 30, 2010
Posts: 66
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 30, 2010
Posts: 66
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

Joined: Sep 29, 2008
Posts: 12617

You said you knew how to concatenate strings--so concatenate away!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Why are you using == to check Strings for equality?
Would the StringBuilder class help?
Nicol Green
Ranch Hand

Joined: Jan 30, 2010
Posts: 66
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 30, 2010
Posts: 66
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 30, 2010
Posts: 66
Now I see what you mean, it is beginning Java mistake..

Thanks!

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with char/string representation
 
Similar Threads
OOP Modeling - Separate GUI from implementaion-How to refer to class where have more than one method
Class relations mess - OOP
Hashing Problem
Crypto Challenge Instructions
Adding ScrollBar to JTextArea