Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help With Caesar Shift

 
Jeff Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need help with the Caesar Shift formula in this program. Any help would be appreciated.

Here is what I have:

//packer



Thanks

[edited to add code tags]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,
What's the problem - does it not compile? Does it not do what is expected?

Please state what is happening vs what you expect.
 
Jeff Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I enter a three letter password such as "zip" and the amount of times for it to shift to the right, the output comes out as: Encrypted password is: 

Here is an example of what the program should do:

Input:
zip (three letter password)
25 (amount of times for it to shift)

Output:
yho (encrypted password)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think in this line:

System.out.println("Encrypted password is: " + (char)i);

you meant for that last "i" to be "character", eh? The variable "i" is the loop index; cast to a char, it'd be a non-printing character.
 
Jeff Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but when I changed the last "i" to "character", the output came out as:

Encrypted password is: z
Encrypted password is: {
Encrypted password is: |
Encrypted password is: }
Encrypted password is: ~
Encrypted password is: 
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: i
Encrypted password is: j
Encrypted password is: k
Encrypted password is: l
Encrypted password is: m
Encrypted password is: n
Encrypted password is: o
Encrypted password is: p
Encrypted password is: q
Encrypted password is: s
Encrypted password is: t
Encrypted password is: v
Encrypted password is: w
Encrypted password is: x
Encrypted password is: y
Encrypted password is: z
Encrypted password is: {
Encrypted password is: |
Encrypted password is: }
Encrypted password is: ~
Encrypted password is: 
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: p
Encrypted password is: q
Encrypted password is: s
Encrypted password is: t
Encrypted password is: v
Encrypted password is: w
Encrypted password is: x
Encrypted password is: y
Encrypted password is: z
Encrypted password is: {
Encrypted password is: |
Encrypted password is: }
Encrypted password is: ~
Encrypted password is: 
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?
Encrypted password is: ?

I'm not sure what else to do?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what is that loop over "n" supposed to do? The outer loop runs once for each character; then the inner loop runs 26 times each time around the outer loop. So you're getting 26 x stroriginal.length() lines printed.

You need to get rid of that inner loop, then work on the expression "(character + n % 26) " so it represents the actual encoded character.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic