Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By awinston

Thread: Working with strings

  1. #1
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Question Working with strings

    I am making a decoder for a cipher called the Caesar Shift Cipher. I am making a first build of it and it will not be efficent at all so please ignore that. Here is my code so far:

    import java.util.Scanner;
    public class decoder {
    Java Code:
    	public static void main(String[] args) {
    		Scanner number = new Scanner(System.in);
    		System.out.println("Please enter your shift number");
    		int shift = number.nextInt();
    		int counter = 1;
    		
    		if (shift == 1)
    			while (counter  > 0) {
    				System.out.println("Enter a letter");
    				String letter = number.next();
    				if (letter == "a")
    					System.out.println("z");
    				else if (letter == "b")
    					System.out.println("a");
    I thought this would work but whenever I run it it does not print out the new letter. I have no previous work with stings and I would really appriciate any information. Thanks.
    Last edited by gman1199; 07-28-2012 at 02:26 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Working with strings

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Working with strings

    Quote Originally Posted by gman1199 View Post
    I am making a decoder for a cipher called the Caesar Shift Cipher. I am making a first build of it and it will not be efficent at all so please ignore that. Here is my code so far:

    Java Code:
           if (shift == 1)
    			while (counter  > 0) {
    				System.out.println("Enter a letter");
    				String letter = number.next();
    				if (letter == "a")
    					System.out.println("z");
    				else if (letter == "b")
    					System.out.println("a");
    I thought this would work but whenever I run it it does not print out the new letter. I have no previous work with stings and I would really appriciate any information.
    1. You don't have a closing bracket for your class.

    2. You don't have a closing bracket for your while-loop.

    3. A letter will only print out if the user enters a or b.

    4. A letter will only print out if the user enters 1 for the shift.

    5. Inside your while-loop counter is never decremented, so the while-loop (if it had a closing bracket) would run forever.

    6. You compare strings using the == operator when you should use the equals() method instead.

    7. No precautions against invalid user input are taken.

    8. You don't have a closing bracket for your main method.

    Also, I don't understand the connection between the shift number and the actual cipher.


    Since you haven't worked with Strings before, I recommend that you read this: Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
    Last edited by awinston; 07-28-2012 at 12:54 AM. Reason: #8
    kammce likes this.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #4
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Working with strings

    awinston -

    Thank you. The reason for a lot of the errors you pointed out is the program is not finished and I only posted a small section because it is, as I say in the post, very lengthy. The shift number is basically the amount of letters that are moved from the back of the alphabet to the front. For example, a cipher where the shift number is 4 would look like this: a=w, b=x, c=y, d=z, e=a, ... z=v. The cipher was used by Caesar to communicate with his generals. It's very easily solved because there are only 25 possible combinations.

  5. #5
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Working with strings

    That's interesting!

    Why are you choosing to go about this in an inefficient manner (as you pointed out)?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #6
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Working with strings

    Sorry for not getting back sooner, out of town for a few weeks. The answer to your question is I have no idea how else to do it. Also, I was working on my code today, trying to make it analise a whole string, and I came up with this (using that web page you gave me) :
    Java Code:
    import java.util.Scanner;
    public class decoder {
    
    	public static void main(String[] args) {
    		Scanner number = new Scanner(System.in);
    		System.out.println("Please enter your shift number");
    		int shift = number.nextInt();
    		int counter = 0;
    		int i = 0;
    		System.out.println("Enter a string");
    		String cipher = number.next();
    		int len = cipher.length();
    		if (shift == 1)
    			while (counter < len) {
    				Character letter = cipher.charAt(i);
    				if (letter.equals("a"))
    					System.out.println("z");
    				else if (letter.equals("b"))
    					System.out.println("a");
    				
    				counter++;
    				i++;	
    			}
    	}
    }
    I thought this would work, but when I ran it, nothing was printed out. I originally got this error -

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(Unknown Source)
    at decoder.main(decoder.java:15)

    - but this was fixed when I changed to while loop to while (counter < len). Any thoughts you have on this would be appriciated, I have no idea what is wrong.

    Thanks,
    gman1199

  7. #7
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Working with strings

    Can you post the code that was giving you the error?

    Also, it looks like you can use either counter or i without using both of them. They are always the same value, so you only need one.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  8. #8
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Working with strings

    Java Code:
    import java.util.Scanner;
    public class decoder {
     
        public static void main(String[] args) {
            Scanner number = new Scanner(System.in);
            System.out.println("Please enter your shift number");
            int shift = number.nextInt();
            int counter = 0;
            int i = 0;
            System.out.println("Enter a string");
            String cipher = number.next();
            int len = cipher.length();
            if (shift == 1)
                while (counter < len) {
                    Character letter = cipher.charAt(i);
                    if (letter.equals("a"))
                        System.out.println("z");
                    else if (letter.equals("b"))
                        System.out.println("a");
                     
                    counter++;
                    i++;    
                }
        }
    }
    This is what the error was coming from

  9. #9
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Working with strings

    I can compile and run that code with no errors. Didn't you say that you were receiving the error before you changed the condition of the while-loop to (counter < len)? What was the condition before?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  10. #10
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Working with strings

    Also, you should be aware that using the equals() method to compare a Character with a String will return false.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  11. #11
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Working with strings

    Sorry, I forgot to change it from while (counter < len) to while (counter <= len). It was (counter <= len) that was causing the error. Also, is there a way to fix that equals() thing?

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Working with strings

    is there a way to fix that equals() thing?
    Yeah, just don't use a character.

    In java, a literal 'a' is the character a where as "a" is a string. As long as you are not using a literal character, you're fine. The alternative is to use only characters in which case the == will work. You're current solution seems fine though!

  13. #13
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Working with strings

    So if I replace "a" with a (or is it 'a'?) it should work?

  14. #14
    gman1199 is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Working with strings

    Thank you so much! I replaced everything with the '' and it works!

  15. #15
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Working with strings

    Quote Originally Posted by gman1199 View Post
    Sorry, I forgot to change it from while (counter < len) to while (counter <= len). It was (counter <= len) that was causing the error.
    Yes, (counter <= len) will allow counter to equal length. Since the last character of any String has an index of length-1, this will cause the StringIndexOutOfBoundsException to be thrown.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Working with strings

    Quote Originally Posted by gman1199 View Post
    it works!
    Congrats, but here's a tip on making your code more efficient.
    Java Code:
    System.out.println((char) 'b' - 1);
    The only issue is how to wrap 'a' back around to 'z'.

  17. #17
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Working with strings

    So if I replace "a" with a (or is it 'a'?) it should work?
    It's the ' vs the " that determines wether a literal is a char or a String in java.
    So:
    Java Code:
    char someCharacter = 'a';
    String someString = "a";

Similar Threads

  1. OR || not working in while loop when comparing strings
    By janey4115 in forum New To Java
    Replies: 4
    Last Post: 11-16-2011, 12:42 PM
  2. if statement with strings not working... again
    By hardcorebadger in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 07:02 AM
  3. Replies: 2
    Last Post: 01-04-2011, 05:30 AM
  4. working with unicode strings
    By drift in forum New To Java
    Replies: 4
    Last Post: 10-08-2010, 08:58 AM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 AM

Tags for this Thread

Posting Permissions

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