aspose file tools*
The moose likes Beginning Java and the fly likes Having trouble with a simple character mapping program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having trouble with a simple character mapping program." Watch "Having trouble with a simple character mapping program." New topic
Author

Having trouble with a simple character mapping program.

Shawn Weller
Greenhorn

Joined: Apr 07, 2005
Posts: 3
Hi,
Ive done a program that has a default string containing 40 characters, and ask the user to re-enter those same characters in a different order. Then get a string from the user to be mapped/unmapped between the two sequences.

example:
abcdefghijklmnopqrstuvwxyz0123456789.; ! //DEFAULT SEQUENCE
mnbvcxzasdfghjklpoiuytrewq0987654321! ;. //NEW SEQUENCE (from user)

(using the example above) If I am entering something like 'hello' , is comes out as acggk, and goes back to 'hello' fine.
But if I enter the mapped part first, say 'acggk' it is coming out as 'mbzzf' and not 'hello'. Now I know this is because it is mapping from top/down and then from down/top, (when it needs to be down/top then top/down for starting with the already mapped sentence)

I know that I could write out another method to do all of setString()'s content in reverse and then just prompt the user whether or not he is mapping to jumbled characters or back from, but it seems like alot of typing when im sure there must be a simple answer. (plus I do not want to have to re-prompt the user to ask such an option)

Any help would be great, for I have that feeling im trying to do something simple in a difficult way . (I hate that feeling...)

the part of my code from a helper class for mapping the characters:

//defaultChars contains the default 40 char string
//newChars contains the user entered 40 char sequence
//line is the inputed sentence to be jumbled/unjumbled
//count is just checking if non-usable characters are entered
//enResult and deResult are instance variables declared at top of prog


PS. I know this isnt "encryption" but it is uni work and he wrote the question out using those words.
If ya need me to post the whole program I will but im thinking it wouldnt be good for another student to just copy it all.

-Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

Let me see if I'm keeping up ... You have code now that uses two alphabets: plain & scrambled. It finds characters in plain and replaces them with characters in scrambled. You'd like one that finds characters in scrambled and replaces them with plain, but you don't want to retype the whole thing. That's an excellent instinct!

So, how can we use the same code to do both jobs? We need some way to tell it which alphabet should match the input and which one should be used for replacing.

See if this bit of code makes sense ... we try hard in this forum not to give complete answers so I'm just kind of sketching something out to give you ideas ...

That doesn't exactly fit your design. I'm returning a single string where you were setting a couple variables, so it won't quite drop right into your program. But see if it gives you some ideas. Let us know what you come up with!

Oh, BTW: Look at the javadoc for String.indexOf() and see if you can use it to cut your code down a bit.
[ April 07, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Shawn Weller
Greenhorn

Joined: Apr 07, 2005
Posts: 3
Originally posted by Stan James:
[QB]Hi, welcome to the ranch!


Thanks



Ok I just need to clarify what you mean by this. When you call the method 'substitute' and send it those 3 variables in the parameter line, arnt they meant to be in the same order when you declared the method?

If I am calling substitute method twice and each time im passing it parameters in a different order, wont this cause an error?

Also I tried using indexOf() but I think I am making the same mistakes, my brain is just retarded I think.

//trying to do the scrambled way:


Am I way off base in what I am trying to do?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I was going for a generic substitute that would find characters in the first alphabet arg[1] and replace them with characters in the second alphabet arg[2]. The find and replace code inside substitute could be almost exactly what you have now. A call with plain,scrambled will scramble something, a call with scrambled,plain will unscramble.

What was the effect when you tried indexOf? Compiler error? You might have to switch from charAt to substring to get the character to search for.

BTW: You can do an amazing trick with the substitute routine. Try it with pencil & paper and see what this does:

String s = substitute( "GHIJABDE", "ABCDEFGHIJ", "04/16/2005" )
Shawn Weller
Greenhorn

Joined: Apr 07, 2005
Posts: 3
Thanks Stan, I finally got it working properly (with less typing now too).
I couldnt have completed it without your help.

Originally posted by Stan James:

What was the effect when you tried indexOf? Compiler error? You might have to switch from charAt to substring to get the character to search for.


Yeah it did get angry at me for using charAt, but with substring it works fine.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hey, glad you got something going!

Did you try that last trick with substitute? The REXX language has this function built in (as translate()) and the docs include examples of using it to rearrange the characters in a string. I used to use it all the time to convert MM/DD/YYYY to YYYYMMDD for sorting.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having trouble with a simple character mapping program.