AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

java string operation

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java
View previous topic :: View next topic  
Author Message
a
Guest





PostPosted: Mon Apr 25, 2011 7:18 am    Post subject: java string operation Reply with quote



Hi

I have 2 string containing login info. I need a reversible opertion to mix
them together.
e.g
C = op(A, B)
op'(C) -> A B

Thanks
Back to top
Luuk
Guest





PostPosted: Mon Apr 25, 2011 10:58 am    Post subject: Re: java string operation Reply with quote



On 25-04-2011 11:18, a wrote:
Quote:
Hi

I have 2 string containing login info. I need a reversible opertion to
mix them together.
e.g
C = op(A, B)
op'(C) -> A B

Thanks


My javaskills are limited, and need refreshing ;)

I did a course 11 years ago, and never use java after that.....


public class TEST {

public static String op( String A, String B) {
int i;
i = A.length();
return i + "|" + A + "|" + B;
}

public static String op(String C, int D) {
int i,j;
String E = "";

i = 0;
j = 0;
while(C.charAt(i) != '|') {
j = j * 10 + C.charAt(i)-48;
i = i + 1;
}

if (D==1) {
i= i + 1;
j= i + j;
} else {
i= i + 2 +j ;
j= C.length();
}

for (int x=i; x<j; x++) {
E = E + C.charAt(x);
}

return E;
}

public static void main (String args[]) {

String A = "someusername";
String B = "someweirdpassword";
String C;
String D;

C = op(A,B);
System.out.println( C );
D = op(C,1);
System.out.println( D );
D = op(C,2);
System.out.println( D );
}
}


--
Luuk
Back to top
Luuk
Guest





PostPosted: Mon Apr 25, 2011 11:06 am    Post subject: Re: java string operation Reply with quote



On 25-04-2011 14:58, Luuk wrote:
Quote:
On 25-04-2011 11:18, a wrote:
Hi

I have 2 string containing login info. I need a reversible opertion to
mix them together.
e.g
C = op(A, B)
op'(C) -> A B

Thanks


My javaskills are limited, and need refreshing ;)

I did a course 11 years ago, and never use java after that.....


public class TEST {

public static String op( String A, String B) {
int i;
i = A.length();
return i + "|" + A + "|" + B;
}

public static String op(String C, int D) {
int i,j;
String E = "";

i = 0;
j = 0;
while(C.charAt(i) != '|') {
j = j * 10 + C.charAt(i)-48;
i = i + 1;
}

if (D==1) {
i= i + 1;
j= i + j;
} else {
i= i + 2 +j ;
j= C.length();
}

for (int x=i; x<j; x++) {
E = E + C.charAt(x);
}

return E;
}

public static void main (String args[]) {

String A = "someusername";
String B = "someweirdpassword";
String C;
String D;

C = op(A,B);
System.out.println( C );
D = op(C,1);
System.out.println( D );
D = op(C,2);
System.out.println( D );
}
}



btw, i hope someone will post some recommendations on this 'code'....

;)

--
Luuk
Back to top
Ian Shef
Guest





PostPosted: Mon Apr 25, 2011 6:11 pm    Post subject: Re: java string operation Reply with quote

"a" <a (AT) mail (DOT) com> wrote in news:W%atp.7637$_P6.4055 (AT) newsfe05 (DOT) iad:

Quote:
Hi

I have 2 string containing login info. I need a reversible opertion to mix
them together.
e.g
C = op(A, B)
op'(C) -> A B

Thanks


Because Java cannot return two results with a single expression, this:
op'(C) -> A B
is completely unclear to me.

My suggestion is below. If you ignore equals, hashCode, toString and main,
it is really quite simple. I hope that I am not solving someone's homework
problem!


public class MixTogether {

private String a ;
private String b ;

MixTogether(String a, String b) {
this.a = a ;
this.b = b ;
}

public static MixTogether op(String a, String b) {
return new MixTogether(a, b);
}

public String getA() { return a ; }
public String getB() { return b ; }

@Override
public boolean equals(Object obj) {
if ( !(obj instanceof MixTogether)) return false ;
MixTogether m = (MixTogether)obj ;
return m.getA().equals(a) && m.getB().equals(b) ;
}
@Override
public int hashCode() { return toString().hashCode() ; }
@Override
public String toString() { return a + b ; }

public static void main(String [] args) {
String login = "login" ;
String password = "password" ;
MixTogether c = op(login, password) ;
System.out.println("c=" + c) ;
System.out.println("a=" + c.getA()) ;
System.out.println("b=" + c.getB()) ;
}
}
Back to top
Inline
Guest





PostPosted: Mon May 23, 2011 6:43 am    Post subject: Re: java string operation Reply with quote

Ian Shef wrote:
Quote:
"a"<a (AT) mail (DOT) com> wrote in news:W%atp.7637$_P6.4055 (AT) newsfe05 (DOT) iad:

Hi

I have 2 string containing login info. I need a reversible opertion to mix
them together.
e.g
C = op(A, B)
op'(C) -> A B

Thanks


Because Java cannot return two results with a single expression, this:
op'(C) -> A B
is completely unclear to me.

My suggestion is below. If you ignore equals, hashCode, toString and main,
it is really quite simple. I hope that I am not solving someone's homework
problem!


public class MixTogether {

It's possible to return both strings as an array. It is a bad idea to
put a password in a String. Because that String will than happily live
in the String pool, a char[] would be more appropriate.

class MyStrings {

private String[] originalStrings = new String[2];

public void setStrings(String a, String b){
originalStrings[0] = a;
originalStrings[1] = b;
}

public String getMixed(){
return originalStrings[0] + originalStrings[1];
}

public String[] getOriginalStrings(){
return originalStrings;
}


public static void main(String[] args){
MyStrings m = new MyStrings();
m.setStrings("sys", "admin");
for(String s: m.getOriginalStrings()){
System.out.println(s);
}
System.out.println(m.getMixed());
}

}
Back to top
Ian Shef
Guest





PostPosted: Mon May 23, 2011 4:56 pm    Post subject: Re: java string operation Reply with quote

Inline <a@a> wrote in news:4dda1e1a$0$49175$e4fe514c (AT) news (DOT) xs4all.nl:

Quote:
Ian Shef wrote:
"a"<a (AT) mail (DOT) com> wrote in news:W%atp.7637$_P6.4055 (AT) newsfe05 (DOT) iad:

Hi

I have 2 string containing login info. I need a reversible opertion to
mix them together.
e.g
C = op(A, B)
op'(C) -> A B

Thanks


Because Java cannot return two results with a single expression, this:
op'(C) -> A B
is completely unclear to me.

My suggestion is below. If you ignore equals, hashCode, toString and
main, it is really quite simple. I hope that I am not solving
someone's homework problem!


public class MixTogether {

It's possible to return both strings as an array.
I agree, but it is not nearly as clear and expressive as getLogin() and

getPassword() would be. I used getA() and getB() because A and B are the
names that the original poster used.

Quote:
It is a bad idea to
put a password in a String. Because that String will than happily live
in the String pool, a char[] would be more appropriate.
I agree, but the original poster defined the problem in terms of "string"

[sic].

Quote:

class MyStrings {

private String[] originalStrings = new String[2];

public void setStrings(String a, String b){
originalStrings[0] = a;
originalStrings[1] = b;
}

public String getMixed(){
return originalStrings[0] + originalStrings[1];
}

public String[] getOriginalStrings(){
return originalStrings;
}


public static void main(String[] args){
MyStrings m = new MyStrings();
m.setStrings("sys", "admin");
for(String s: m.getOriginalStrings()){
System.out.println(s);
}
System.out.println(m.getMixed());
}

}

This solution still has the String pool issue, and it is unclear at the end
what is login and what is password. Furthermore, returning originalStrings
breaks encapsulation.

I admit that it is a valid response to the original poster's request,
because the request was unclear and open to multiple interpretations, and
the original poster has not posted a clarification that is visible to my
news feed.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.