permaculture playing cards*
The moose likes Beginning Java and the fly likes Replicating a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replicating a string?" Watch "Replicating a string?" New topic
Author

Replicating a string?

Iain Emsley
Ranch Hand

Joined: Oct 11, 2007
Posts: 60
Is it possible to replicate a string by itself (or any other integer)? In Perl you can use the x operator to multiply the left handstring by the right hadn one, eg $a = b x c;.

I'm still working on some code for which I'm trying to do this but I'm really unsure as to whether I've set it up correctly:

I've tried to take the string from the split (word) and turn it into hexadecimal (the output is like this:
68656C706C696E65406A6973636D61696C2E61632E756B-8BE9FEFE999EE7-aoMTs) and then replicate the hexadecimal string by the number of characters in its own length before joining it with the other string from the previous code block in a hash. (So B becomes 42 then replicated by the number of characters (2) so 4242 before going into the hash). I need to do this to pre-authorise a cookie in another piece of software.

The one thing that does concern me at the moment is whether the String replicate will read the garfield value which is another class.

I'd be very grateful about getting this piece of code on its way to doing everything so I can get it functioning fully.

Thanks,

Iain
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Java does not automatically replicate strings using the * operator. Probably you should use a StringBuilder (or StringBuffer) and write a simple loop to append a string to the StringBuilder n times.

[Iain]: The one thing that does concern me at the moment is whether the String replicate will read the garfield value which is another class.

Well, the variable garfield in replicate() is a method parameter - it's up to whatever code that calls it (not shown) to put that value in. I have no idea who uses this or how, or what garfield means, so there's not much I can do to help with that.

However if we look at the garfield in the earlier code, we see that it's a local variable. It's only meaningful inside the loop it's defined in. Since the loop ends immediately after the definition, no other code can possibly access the variable - it's useless. Also, it gets a new value every time the loop repeats, overwriting the old value. So the loop doesn't seem to accomplish anything - it does some calculations, but no one can retrieve them.

Perhaps you want to define some sort of variable outside the loop. However it's a mystery to me how this variable would be related to the many different values you're calculating inside the loop. Or maybe you should be calling this replicate() method from inside the loop, where the local variable garfield still has meaning. I don't know; I can't even guess what you're trying to do here. The variable names offer no clue at all as to what they mean, so no help there.


"I'm not back." - Bill Harding, Twister
Iain Emsley
Ranch Hand

Joined: Oct 11, 2007
Posts: 60
I think that I have taken care of the local variables and returned them out from the methods and this is the full code block that it is insdie:

Have I managed to resolve the difficulties that I'd put into my code earlier and have I also used StringBuffer() correctly? Ultimately I need to get the values of the email and password in the WALOGIN code transformed into hex (with the password replicated by its own length - and this is the thing that I'm concerned that I've still not achieved) and passed to a string which will link to a socket to go to another programme.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

Oh, if that's all you're trying to do, you can achieve the same thing with

Only the + operator can interact with strings this way - * and other operators do not mix with strings. So StringBuilder is good in general, especially if you're appending repeatedly inside a loop, but + works for short, simple expressions.

I see a couple return statements from inside loops that make me very dubious. Why write a loop at all if you're going to return from the loop (terminating the method you're in) at the end of the first iteration? I think maybe you think return means something very different than it does. I really can't tell what the intent of the other code is though, so I don't know how to suggest fixes.

I think maybe you're trying to do too much at once here, and putitng this all inside single method is making it harder for you to test individual parts of the code to see if they do what you want them to. E.g. you could write a sinple method to get the password from... um.. whatever you get the password from. (It looks like you're getting data from "word" and then assigning data back into "word", after mutilating it badly.) Write the method to take one String as input, and return one String as output, and put in some more print statements initially so you can see what it's doing, and call that method directly from main() so you can see the method in action without distractions from the other parts of your program. Focus on one aspect at a time and get it working. Ideally it would be nice to learn to use JUnit to run tests on individual methods, but for the moment, calling the method(s) directly from main() is probably easier.

Also, the indentation in your program is jumping around kind of randomly; it's hard to follow. I suggest putting those end braces at the same level as the if or loop statement that they correspond to - so intead of

you would have

[ November 02, 2007: Message edited by: Jim Yingst ]
Iain Emsley
Ranch Hand

Joined: Oct 11, 2007
Posts: 60
Many thanks for the comments and pointers, Jim. I'll certainly work on simplifying the code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replicating a string?
 
Similar Threads
Sun Cirtification
Splitting a string in 2 and turning it into hexadecimcal
System.out.println(this)
MD5
Parsing encoded data?