This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incorrect output when code syntax seems correct

 
Thomas Crew
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I've been working on a java program that decrypts and encrypts Rot-13 messages. The additional requirements for this program are that the program must only encrypt alphabetical letters (leave any other symbols, numbers, characters unchanged) and the methods within this program must be the ones I've used in the following code (along with the same parameters and signatures).
The problem i keep having is that when i run the program, the output is numerical for any character instead of producing the desired Rot-13 decryption. For example: when i input "zzz", the program outputs "109109109". I understand that its some how giving me the decimal number of its character but i don't know how i can fix (this is after 2 days of messing around with the code). I have a feeling the solution is something extremely simple (hopefully). However i'm unable to figure this out on my own at this point. If someone could read over this code and provide me with some kind of help, i would be really grateful :) .

-Thomas



 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rotateCharacter() returns an int, so you are concatenating a series of ints to the "complete" String.
 
Thomas Crew
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:rotateCharacter() returns an int, so you are concatenating a series of ints to the "complete" String.


Thanks Greg.

If that's the problem, how would I translate the integers returned by the rotateCharacter() method in decrypt(), so its concatenating the encrypted/decrypted letters along with any unchanged numbers/symbols?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method should return a char, not an int. And it should take a char as its parameter too. After all, its job is to turn on char into another char.
 
Thomas Crew
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:The method should return a char, not an int. And it should take a char as its parameter too. After all, its job is to turn on char into another char.


What would I specifically change in the code. When I change the int parameter to a char the program's output still doesn't work and now I'm noticing that the output is encrypting every character as "109" and adding them to one string. Example: "zz,," = "109109109109"


what i tried doing:


 
Thomas Crew
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind with the problem of repeating the first index position (i fixed that). Just still having trouble in figuring out what I specifically should change in the code (in changing the methods parameter from int to char)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Crew wrote:Nevermind with the problem of repeating the first index position (i fixed that). Just still having trouble in figuring out what I specifically should change in the code (in changing the methods parameter from int to char)


You deleted your code, so I don't know what to tell you other than have the method accept and return a char. You can do math on a char just like on an int. For instance 'D' - 2 gives 'B' (although you'll probably have to cast the result to char).

Take your best shot, and if you get stuck, post your code and ask a specific question about what you're having trouble with.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic