• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the method String.toCharArray()
 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So can anyone try to help me during the swapping part.....
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3918
10
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a hint: what are the values an int can take?
 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D*mn! I've counted it wrongly Thank you, Rob, for catching my mistake.
 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to do??
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the subclasses of Number which I counted wrongly yesterday. One of them will supply what you want.
 
Chan Lee
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ALL
Thanks a ton! I got the solution....
 
Manish R Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic