Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Encryption Error
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ProgrammingFun




PostPosted: Sat Nov 13, 2010 12:14 pm   Post subject: Encryption Error

Hi,

I was trying to solve the following question:
Quote:

6. Encrypt.java: Code a program which encrypts a line of code. The encryption specifications are as follows:
? first and last character of each string are exchanged.
? Middle characters of each string are shifted to the character two after it in the ASCII table (works for non-letters as well.)
? spaces are left alone
? careful with strings less than 3 characters


Example:
Input: Enter a line to be encrypted: Happy Days!
Output: The encryption is: ycrrH !c{uD


with the following code:
Java:

import java.util.*;

   public class Encrypt2
   {
      public static void main (String[] args)
      {
         Scanner in = new Scanner (System.in);
         int length;
         String encrypt = "";
     
         System.out.print ("Enter string: ");
         String str = in.nextLine();
         length = str.length();
       
         char[] breakup = new char[length];
         char change;
       
         for (int c = 0; c < length; c++)
         {
         
            if ( (int) breakup[c] == 32)
            {
               change = breakup[0];
               breakup[0] = breakup[c-1];
               breakup[c-1] = breakup[0];
               
               for (int d = c+1; d <length-1; d++)
               {
                  if ((int) breakup[d] == 32)
                  {
                     change = breakup[c+1];
                     breakup[c+1] = breakup[d-1];
                     breakup[d-1] = change;
                  }
                 
                  else
                  {
                     breakup[d] = (char) (((int)breakup[d]) + 1);
                  }
               }
            }
           
            else if (c >= 1 && c < length)
            {
               change = breakup[0];
               breakup[0] = breakup[length-1];
               breakup[length-1] = change;
               
               breakup[c] = (char) (((int)breakup[c]) + 1);
            }
                
         }
         
         for (int c = 0; c < length; c++)
            encrypt = encrypt + (breakup[c]);
       
         System.out.println ("Encrypted String: " + encrypt);
           
      }
   }


However, I am not getting a proper output.
Can someone plz help me in determining whether this is a logic or syntax error and possibly, how to fix it?
Thanks for the time...

EDIT: Sorry for not commenting the code...I hope its not that cluttered...
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Sat Nov 13, 2010 4:15 pm   Post subject: RE:Encryption Error

You never assigned the characters of the string to breakup, which is why you get those rectangles as output (or at least thats what its like for me). You can assign the characters of the inputted string to breakup with a for loop and using the charAt method of the inputted string.

Also theres a problem with this line:
Java:

else if (c >= 1 && c < length)
ProgrammingFun




PostPosted: Sat Nov 13, 2010 4:25 pm   Post subject: Re: RE:Encryption Error

TerranceN @ Sat Nov 13, 2010 4:15 pm wrote:
You never assigned the characters of the string to breakup, which is why you get those rectangles as output (or at least thats what its like for me). You can assign the characters of the inputted string to breakup with a for loop and using the charAt method of the inputted string.

Also theres a problem with this line:
Java:

else if (c >= 1 && c < length)

Thanks for pointing that out (does java read and execute an else if statement even if the if statements was executed?)
I now get output but it is still incorrect:
Java:

   import java.util.*;

   public class Encrypt2
   {
      public static void main (String[] args)
      {
         Scanner in = new Scanner (System.in);
         int length;
         String encrypt = "";
     
         System.out.print ("Enter string: ");
         String str = in.nextLine();
         length = str.length();
       
         char[] breakup = new char[length];
         char change;
       
         for (int c = 0; c < length; c++)
            breakup[c] = str.charAt(c);
       
         for (int c = 0; c < length; c++)
         {
         
            if ( (int) breakup[c] == 32)
            {
               change = breakup[0];
               breakup[0] = breakup[c-1];
               breakup[c-1] = breakup[0];
               
               for (int d = c+1; d <length-1; d++)
               {
                  if ((int) breakup[d] == 32)
                  {
                     change = breakup[c+1];
                     breakup[c+1] = breakup[d-1];
                     breakup[d-1] = change;
                  }
                 
                  else
                  {
                     breakup[d] = (char) (((int)breakup[d]) + 1);
                  }
               }
            }
           
            else if (breakup[c] != 32 && c >= 1 && c < length)
            {
               change = breakup[0];
               breakup[0] = breakup[length-1];
               breakup[length-1] = change;
               
               breakup[c] = (char) (((int)breakup[c]) + 1);
            }
                
         }
         
         for (int c = 0; c < length; c++)
            encrypt = encrypt + (breakup[c]);
       
         System.out.println ("Encrypted String: " + encrypt);
           
      }
   }
TerranceN




PostPosted: Sat Nov 13, 2010 4:49 pm   Post subject: RE:Encryption Error

Only one block of an if statement gets executed, so if the first conditional is true it wont check or run any of the others (else if's and else).

Like I stated before, the error is in the line I mentioned. By saying if c >= 1 you avoid the first character, so what do you have to do to c < length to avoid the last character?

Edit: Whoops, I never tried a string with spaces, which it turns out there is still something wrong having to do with spaces.
Edit2: Does the program need to switch the first and last letter of each word, or the entire inputted string?
ProgrammingFun




PostPosted: Sat Nov 13, 2010 5:29 pm   Post subject: RE:Encryption Error

OK, I fixed the else if error...
I believe that the first and last letter for every word has to be switched....
Thanks...
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: