Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Small problem

  1. #1
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Small problem

    I'm doing a practice problem where I have to decipher a certain string by moving each letter back two places in the alphabet, I also need to wrap it around if letter is the first or second letter of the alphabet. It works ok except when the letter is in the first or second position, it seems to decipher letter and print which is great though does it an additional time which is peculiar, I've been trying for many hours and still can't figure out small bug.
    Here is my code:
    import java.util.ArrayList;
     
    public class CCipher{
    	public static void main(String[]args){
    		System.out.println(decode("ABC", 1));
    	}
     
    	public static String decode(String cipherText, int shift){
     
          String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z"};
          ArrayList<String>cText = new ArrayList<String>();
          String returnString = "";
          //seperate individual letters and add them to arrayList
          for(int i = 0;i<cipherText.length();i++){
        	  cText.add(cipherText.substring(i,i+1));
          }
          //loop through no. of shifts needed
          for(int i=0;i<shift;i++){
        	  //loop through arrayList
        	  for(String cTEXT: cText){
        	  //loop through alphabet
    		   for(int j=0;j<alphabet.length;j++){
     
    		    		if(cTEXT.equals(alphabet[j])){
     
    		    			if(j<1){
    		    				cTEXT = alphabet[alphabet.length-2];
    		    				returnString += cTEXT;
     
    		    			}else if(j<2){
    		    				cTEXT = alphabet[alphabet.length-1];
    		    				returnString += cTEXT;
     
    		    			}else if(j>=2){
    		    			cTEXT = alphabet[j-2];
    		    			returnString += cTEXT;
    		    			}
    		    		}
     
    		    	}
    		    }
    		}
          return returnString;
     
    	}
    }


    --- Update ---

    Sorry for not giving an example. e.g.If I input "CCC" I get "AAA" output which is great, though if I input "ACC" I get "YVAA" as output, the "Y" is a good thing though I can' understand why from "Y" it is shifted a further two places.


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Small problem

    Hint: unicode.

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: Small problem

    When you have shifted the first char you dont continue with the next char from the original String, i.e you keep looping over the alphabet.

    And this is by far the most complex and unintuitive solution to that problem. I'd say 10 LOC are sufficient to solve the shifting Hint: you are manipulating chars!

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Small problem

    Check this portion of your code:
        	  //loop through alphabet
    		   for(int j=0;j<alphabet.length;j++){
     
    		    		if(cTEXT.equals(alphabet[j])){
     
    		    			if(j<1){
    		    				cTEXT = alphabet[alphabet.length-2];
    		    				returnString += cTEXT;
     
    		    			}else if(j<2){
    		    				cTEXT = alphabet[alphabet.length-1];
    		    				returnString += cTEXT;
     
    		    			}else if(j>=2){
    		    			cTEXT = alphabet[j-2];
    		    			returnString += cTEXT;
    		    			}
    		    		}

    Effectively what you are doing is this:
    cTEXT = A;
    loop to end of alphabet
    A < 1;
    cTEXT = Y;
    returnString += Y;
    ... Loop keeps going this time checking for cTEXT.equals(alphabet[j]) once it gets to Y it matches because you changed it in your previous condition
    Y >= 2;
    cTEXT = alphabet(j - 2); = V
    returnString += V
    // returnString = YV

    And it keeps doing it for the rest of the letters. There are a few ways to remedy this issue one is to make the loop end once one of the conditions is met. When you break out of a loop it only ends the current nested loop you are in but the others still continue unless you break out of those as well when a condition is met.

  5. #5
    Member
    Join Date
    May 2013
    Posts
    165
    Thanks
    58
    Thanked 1 Time in 1 Post

    Default Re: Small problem

    Thank you so much guys, I struggled to understand what you all meant since I know I have overcomplicated this problem.
    I tried 'break' command under each condition as hinted by Ubiquitous and it started working.
    Again Thank you!

Similar Threads

  1. Small Problem with my Code
    By Cyril in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 20th, 2013, 03:02 AM
  2. [SOLVED] Small problem regarding inheritance of classes
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 10th, 2011, 02:11 PM
  3. Small problem, help please?
    By Jonathansafc in forum What's Wrong With My Code?
    Replies: 33
    Last Post: September 3rd, 2011, 06:46 PM
  4. keylistener small problem
    By matecno in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2010, 07:51 PM
  5. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 10:26 AM