8 Replies - 464 Views - Last Post: 10 February 2012 - 11:56 AM Rate Topic: -----

Topic Sponsor:

#1 bwills6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 12

Caesar Encryption ?

Posted 06 February 2012 - 04:44 PM

Hey I am working on a basic encryption and I can get it to encrypt but I can't get it to encrypt a entire string. Any hints. I know its something little I am forgetting. I just don't remember how to take the next token of the input string. Thanks

public static  String userinput = "";
    
    public static void main(String[] args) {
        
        
          System.out.println("Please enter a string");
           Scanner scan = new Scanner(System.in);                        
           userinput = scan.next();
         
            int shift = 1;
            char conversion;
           
           
           for (int i = 0; i<userinput.length();i++)
           {
               conversion = userinput.charAt(i);
               conversion = (char)(conversion + shift);
               System.out.print(conversion);
               
           }

This post has been edited by modi123_1: 06 February 2012 - 06:07 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Caesar Encryption ?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 6,037
  • Joined: 20-September 08

Re: Caesar Encryption ?

Posted 07 February 2012 - 04:08 AM

Essentially nothing wrong with that code. What's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Caesar Encryption ?

Posted 07 February 2012 - 04:42 AM

Quote

but I can't get it to encrypt a entire string. Any hints .... I just don't remember how to take the next token of the input string

Do you mean to if the string has more than one word here? Then your problem should be here:
userinput = scan.next();

You need nextLine() instead of next()
Was This Post Helpful? 2
  • +
  • -

#4 bwills6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 12

Re: Caesar Encryption ?

Posted 07 February 2012 - 01:27 PM

Thanks I got it working now. nextLine() is what I was looking for
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Caesar Encryption ?

Posted 07 February 2012 - 08:02 PM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

#6 bwills6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 12

Re: Caesar Encryption ?

Posted 10 February 2012 - 11:22 AM

So I cleaned up the code a bit...but now I can't get it to convert the text. I believe its getting stuck in the second for loop. But I don't know what to do or how to fix it. Any help would be awesome.
public class CeaserApp {
    

    public static String userinput = "";
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a string to encrypt");
        userinput = scan.nextLine();
        encryption();
        
         }
        
        
        public static void encryption()
        {
            
                String[] temp;
                String parser = " ";
                temp = userinput.split(parser);
                char conversion = 0;
                int key = 1;
          

        for(int i = 0;i<temp.length;i++)
               temp[i] = userinput; 
          
        for(int t = 0; t<userinput.length();t++)
            conversion = userinput.charAt(t); 
            conversion = (char)(conversion + key);
            System.out.print(conversion);         
                           
        }

This post has been edited by smohd: 10 February 2012 - 11:29 AM
Reason for edit:: Code tags added. Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Caesar Encryption ?

Posted 10 February 2012 - 11:36 AM

Wait a minute...
What are you doing here?
  for(int i = 0;i<temp.length;i++)
               temp[i] = userinput;


Again looking at your second loop, the only line associated with a loop is the first line, So the last output will be the cipher of the last character only. Use {} to put a block of code with for loop.

Also there is a tutorial on cipher which you may find useful...
Was This Post Helpful? 0
  • +
  • -

#8 bwills6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 12

Re: Caesar Encryption ?

Posted 10 February 2012 - 11:47 AM

I was using the first one to get all of words from the slit() function. I am not sure if I'm doing that right. But what I'm really trying to do is get that parsed string to a char so I then can do the shit to them by one. But I am having trouble with that.
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Caesar Encryption ?

Posted 10 February 2012 - 11:56 AM

No, this line puts every word into array:
temp = userinput.split(parser);

So the first loop has no use there.
Look at the first code you posted in the OP, that was a little good, take it to your method now. If you want to skip the spaces not to be shifted, then you have to loop through the array above and in every word do the shifting, you will need a nested loop for that. also try to look at the tutorial I gave above.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1