aspose file tools*
The moose likes Beginning Java and the fly likes Swapping of two Strings in the form of character arrays..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Swapping of two Strings in the form of character arrays....." Watch "Swapping of two Strings in the form of character arrays....." New topic
Author

Swapping of two Strings in the form of character arrays.....

Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
hi friends,

1) Suppose, there are two strings. Say.. x and y.
2) The length of x is l1 and length of y is l2.

3) if(l1>=l2)
{
I want to convert the content of string x into an array of character ch1.
I want to convert the content of string y into an array of character ch2.
}

4) else
{
I want to convert the content of string x into an array of character ch2.
I want to convert the content of string y into an array of character ch1.
}

can anyone provide the code for me?? please
Kuldeep Tewari
Ranch Hand

Joined: May 22, 2006
Posts: 35
use the method String.toCharArray()
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
@Tewari

I used it but I'm getting error as:

Swap.java:32: cannot find symbol
symbol : variable ch1
location: class Swap
ch1[len1]='a';
^

Swap.java:33: cannot find symbol
symbol : variable ch2
location: class Swap
ch2[len2]='b';
^

public class Swap
{
public static void main(String args[])
{
int len1,len2;
String x = "123453";
String y = "56456";

len1 = x.length();
len2 = y.length();

if(len1>=len2)
{
char ch1[] = x.toCharArray();
char ch2[] = y.toCharArray();
System.out.println("ghi");
}

else
{
char ch2[] = x.toCharArray();
char ch1[] = y.toCharArray();
System.out.println("hi");
}

for(int i=0;i<len1;i++)
{
ch1[len1]='a'; //32 line
ch2[len2]='b'; //33 line
}
}
}
>
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
If I write like this,

public class Swap
{
public static void main(String args[])
{
int len1,len2;
String x = "123";
String y = "56456";

char ch1[] = new char[];
char c21[] = new char[];

len1 = x.length();
len2 = y.length();

if(len1>=len2)
{
ch1[] = x.toCharArray();
ch2[] = y.toCharArray();
}

else
{
ch2[] = x.toCharArray();
ch1[] = y.toCharArray();
}
System.out.println("hi");
}
}

/* Errors:
Swap.java:11: array dimension missing
char ch1[] = new char[];
^
Swap.java:12: array dimension missing
char c21[] = new char[];
^
Swap.java:19: not a statement
ch1[] = x.toCharArray();
^
Swap.java:19: ';' expected
ch1[] = x.toCharArray();
^
Swap.java:20: not a statement
ch2[] = y.toCharArray();
^
Swap.java:20: ';' expected
ch2[] = y.toCharArray();
^
Swap.java:25: not a statement
ch2[] = x.toCharArray();
^
Swap.java:25: ';' expected
ch2[] = x.toCharArray();
^
Swap.java:26: not a statement
ch1[] = y.toCharArray();
^
Swap.java:26: ';' expected
ch1[] = y.toCharArray();
^
10 errors
*/
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
So can anyone try to help me during the swapping part.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Swap.java:32: cannot find symbol
symbol : variable ch1
location: class Swap
ch1[len1]='a';
^

Swap.java:33: cannot find symbol
symbol : variable ch2
location: class Swap
ch2[len2]='b';
^


You declared your arrays in the "if" block. It is out of scope in the "for" block.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Swap.java:11: array dimension missing
char ch1[] = new char[];
^
Swap.java:12: array dimension missing
char c21[] = new char[];
^


When you instantiate an array, you need to specify the size.

As for the other errors, it is generally good practice to fix the first few errors, and then recompile... So, don't worry about the other eight, until you recompile after fixing the first two.

Henry
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
Chan Lee wrote:@Henry

public class Swap
{
public static void main(String args[])
{
int len1,len2;
String x = "123453";
String y = "56456";

len1 = x.length();
len2 = y.length();

if(len1>=len2)
{
char ch1[] = x.toCharArray();
char ch2[] = y.toCharArray();
System.out.println("ghi");
}

else
{
char ch2[] = x.toCharArray();
char ch1[] = y.toCharArray();
System.out.println("hi");
}

for(int i=0;i<len1;i++)
{
ch1[len1]='a'; //32 line
ch2[len2]='b'; //33 line
len1++; len2++;
}
}
}
>



I'm unable to correct the code and getting errors. So, please...can you rewrite the code and provide the actual code for this program?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

You haven't followed what Henry has said in previous post ?

Just move both the declarations "char ch1[] " and "char ch2[]" out of both "if" and "else" blocks to somewhere you defined the "int len1,len2; " maybe. Then compile the code and see.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
Thank you,
yep...I got!!!

But i've one more doubt

I need to convert a large Decimal String "1234567898765432123456789" to an Integer equivalent and then want to convert it back to String.

public class Con
{
public static void main(String args[])
{
String s1 = new String("1234567898765432123456789");
String s2 = new String();

s2 = Integer.toString(Integer.parseInt(s1));
System.out.println(s2);
}
}

Giving an exception... how do i overcome?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

Giving an exception... how do i overcome?


Well, what is the exception? And doesn't the exception tell you what the problem is?

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Here's a hint: what are the values an int can take?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
Henry Wong wrote:
Giving an exception... how do i overcome?


Well, what is the exception? And doesn't the exception tell you what the problem is?

Henry


The exception is.....
Exception in thread "main" java.lang.NumberFormatException: For input string: "1
234567898765432123456789"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Con.main(Con.java:14)

So, how do i convert such a large string to integer??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
There are 14 forms of integer in Java; 5 are primitives, and the other 9 are objects of subclasses of the Number class. If you look in Number for subclasses, you will find something which can take "123456789123456789123456789" and turn it into an integer number.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Campbell Ritchie wrote:There are 14 forms of integer in Java; 5 are primitives, and the other 9 are objects of subclasses of the Number class.

I see the 5 primitive forms (char is also one), but how did you get at 9 classes? Number has 10 known subclasses, of which Float, Double and BigDecimal are not integer. That leaves 7.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
D*mn! I've counted it wrongly Thank you, Rob, for catching my mistake.
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
how to do??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Have a look at the subclasses of Number which I counted wrongly yesterday. One of them will supply what you want.
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
@ALL
Thanks a ton! I got the solution....
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
yopu can learn swapping from my example
class StringTransfer
{ String s1="this is my string";
String s2;
char buff1[];char buff2[];
StringTransfer()
{
buff1=s1.toCharArray();
buff2=new char[buff1.length];

for(int x=0,y=buff1.length-1;x<buff1.length;x++,y--)
{
// buff2[x]=buff1[x]; for printing just the simply string as it is
buff2[y]=buff1[x]; //For reverseing the string
}

System.out.println("Reverse"+String.valueOf(buff2)); //extract value back from the character array!

}
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
Thank you! Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swapping of two Strings in the form of character arrays.....