Results 1 to 15 of 15
  1. #1
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default Char in For Loop

    So basically I wrote code to encode any String using ROT-13. So, for example, if the String is "the", the ROT-13 encoded String will be "gur". Basically, the "t" shifts 13 letters, the "h" shifts 13 letters, and the "e" shifts 13 letters. Here is the code I have for it:
    Java Code:
    public void rot13() {
            for (int i=0; i<givenMessage.length(); i++) {
                char c = givenMessage.charAt(i);
                if 		 (c >= 'a' && c <= 'm') c += 13;
                else 
                	if  (c >= 'n' && c <= 'z') c -= 13;
                else 
                	if  (c >= 'A' && c <= 'M') c += 13;
                else 
                	if  (c >= 'A' && c <= 'Z') c -= 13;
                
            }
    
    	}
    (givenMessage is the message entered by the user in the constructor)

    Now, I need to save the char c in the for loop to a private variable because this method will not print the encoded message. I have another toString() method that will print either the non-encoded String or encoded String. So when I save this char c to a private variable to be able to access in toString(), the char only saves the last letter of the encoded String (which makes sense, but not what I want to do). How can I fix this so that I can either convert the chars into a String or print out all the chars?
    Last edited by spatel14; 06-03-2010 at 04:36 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default

    save the char c in the for loop to a private variable
    Not sure I follow your explanation.
    Change the rot_13() method to accept a String as input arg and return a new String with the the changed value. See the StringBuffer class as a way to build the output String.

  3. #3
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    I sense he wants to place c into a char array and pass it to another method.
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    First:
    Java Code:
                else 
                	if  (c >= 'A' && c <= 'Z') c -= 13;
    Surely you mean:
    Java Code:
    [COLOR="Blue"]            else 
                	if  (c >= 'N' && c <= 'Z') c -= 13;
    [/COLOR]
    It works the way it is because of the previous else if clause, but I'm sure you want to stay consistent.

    They way I would do it (if I would do it at all) is:
    Java Code:
            String rot13(String given) {
                    StringBuilder sb = new StringBuilder(given.length());
                    for (char c : given.toCharArray()) {
                            if (c >= 'a' && c <= 'm')
                                    c += 13;
                            else if (c >= 'n' && c <= 'z')
                                    c -= 13;
                            else if (c >= 'A' && c <= 'M') 
                                    c += 13;
                            else if (c >= 'A' && c <= 'Z') 
                                    c -= 13;
                            sb.append(c);
                    }
                    return sb.toString();
            }
    Any messing around with instance variables, either for the given String or for the result String, is silly. Then again, ROT13-encoding a String is silly anyway.

    -Gary-

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by spatel14 View Post
    How can I fix this so that I can either convert the chars into a String or print out all the chars?

    this code

    Java Code:
    public class TestClass {
    
    	public static StringBuilder rot13(String s) {
    		StringBuilder myString = new StringBuilder();
    
    		String givenMessage = s;
    		for (int i = 0; i < givenMessage.length(); i++) {
    			char c = givenMessage.charAt(i);
    			if (c >= 'a' && c <= 'm')
    				c += 13;
    			else if (c >= 'n' && c <= 'z')
    				c -= 13;
    			else if (c >= 'A' && c <= 'M')
    				c += 13;
    			else if (c >= 'N' && c <= 'Z')
    				c -= 13;
    
    			myString.append(c);
    
    		}
    
    		return myString;
    
    	}
    
    	public static void main(String[] args) {
    		System.out.println(rot13("This is only the test string."));
    	}
    
    }

    produces the output: Guvf vf bayl gur grfg fgevat.

  6. #6
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    char c13 = (c < 'A' || c > 'Z') ? c : ((c & 63) <= 'M' && (C & 63) >= 'A') ? c + 13 : (((c & 63) <= 'Z' && (C & 63) >= 'N') ? c - 13 : c);
    Last edited by iluxa; 06-03-2010 at 11:16 PM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default

    if (c >= 'a' && c <= 'm')
    c += 13;
    else if (c >= 'n' && c <= 'z')
    c -= 13;
    else if (c >= 'A' && c <= 'M')
    c += 13;
    else if (c >= 'N' && c <= 'Z')
    What is the significance of the literals 'm', 'M' etc in this code? What if you want the rotate amt to be other than 13?

    Hardcoding undocumented literals in code is not the best way. Especially if their values depend of part of the algorithm. How about something like:
    char upperLimit = 'a' + rotamt-1;

  8. #8
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Okay, so using some of a bit of all the code above, I got it to work, here is the code I'm using now:

    Java Code:
    public void rot13() {
    		StringBuilder builder = new StringBuilder(givenMessage.length());
    		for (char letter : givenMessage.toCharArray()) {
    			if (letter >= 'a' && letter <= 'm'){
    					letter = (char)(letter+13);
    			}
    			else 
    				if (letter >= 'n' && letter <= 'z'){
    					letter = (char)(letter-13);
    				}
    			else 
    				if (letter >= 'A' && letter <= 'M'){
    					letter = (char)(letter+13);
    				}
    			else 
    				if (letter >= 'N' && letter <= 'Z'){
    					letter = (char)(letter-13);
    				}
    			builder.append(letter);
    		}
    		encodedMessage = builder.toString();
    
    	}
    Now, what happens is if I run the method once, it runs fine, but when I run the method back to back, it should just display the same message I put in. For example:

    givenMessage = "abcde";

    therefore:

    1.rot13() -> "nopqr"
    2.rot13() -> should be "abcde" back again, but it returns "nopqr".

    What's wrong here?

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default

    Please show the code for your test with its output.

    Comment on the code shown: It should use parameters to pass String to encode and return the encoded String vs using global Strings.

  10. #10
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please show the code for your test with its output.

    Comment on the code shown: It should use parameters to pass String to encode and return the encoded String vs using global Strings.
    Well, I'm working on an assignment and the method parameters state the that the method does not take anything in, and only changes the String, it doesn't actually return the String, so it's void. Here's the test code:

    Java Code:
    public static void main(String[] args){
    		
    		SecretMessage b = new SecretMessage("abcde");
    		b.rot13();
    		b.rot13();
    		
    		System.out.println(b.toString());

  11. #11
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    you aren't modifying "givenMessage", so both times, you're working off of the same input string.

    It would be better for your function to return take a string and return a string, like this:

    String rot13(String givenString) {
    ... do what you do
    return result;
    }

    this way, id would be much clearer to your what's going on when you call that function

  12. #12
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    you aren't modifying "givenMessage", so both times, you're working off of the same input string.

    It would be better for your function to return take a string and return a string, like this:

    String rot13(String givenString) {
    ... do what you do
    return result;
    }

    this way, id would be much clearer to your what's going on when you call that function
    Well..the specs for the assignment say that the method should be void and the constructor takes in the message beforehand. I see why getting the String in from the method would be easier, but I'm not sure I can do that..

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,416
    Rep Power
    25

    Default

    In your test program you MUST show the both the input to rot13() and the output for both calls.
    You only show the final output. When you show both for each call you will see what is happening.

    Your assignment definition appears to allow only 1 input String to the rot13() method. It does not provide any way to change it.

  14. #14
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    thats ok, then just make sure to modify "givenString" to "encodedMessage" before you call the function the second time

  15. #15
    spatel14 is offline Member
    Join Date
    Apr 2010
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    thats ok, then just make sure to modify "givenString" to "encodedMessage" before you call the function the second time
    Wow good call! Works perfectly now. Gonna test it out to make sure, but looks to be working. Thanks

Similar Threads

  1. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  2. JNA char **
    By H_P in forum Advanced Java
    Replies: 0
    Last Post: 01-14-2010, 11:13 PM
  3. drawing char by char with Graphics
    By diggitydoggz in forum New To Java
    Replies: 5
    Last Post: 12-27-2008, 12:49 PM
  4. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 02:09 AM
  5. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •