aspose file tools*
The moose likes Java in General and the fly likes StringBuffer's setCharAt -  near help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "StringBuffer Watch "StringBuffer New topic
Author

StringBuffer's setCharAt - near help

Vidya Ramachandran
Greenhorn

Joined: Nov 04, 2005
Posts: 24
I am having a problem with the following code that prints out permutations of a given string...

class Permutations
{

public static void main(String[] args)
{
StringBuffer str = new StringBuffer("abc ");
Permute(str);
}

private static void Permute(StringBuffer str)
{
int length, i;
length = str.length();
int[] used = new int[length];
StringBuffer out = new StringBuffer(length);
for(i=0;i<length;i++)
{
used[i]=0;
}
DoPermute(str,out,used,length,0);
}
private static void DoPermute(StringBuffer str, StringBuffer out, int[] used,int length,int recursLev)
{
int i;
if(recursLev == length)
{
System.out.println(out);
return;
}
for(i=0;i<length;i++)
{
if(used[i]==1)
{
continue;
}
out.setCharAt(recursLev,str.charAt(i)) ;

used[i]=1;
DoPermute(str,out,used,length,recursLev+1);
used[i]=0;

}

}
}


The output is :

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.setCharAt(StringBuffer.java:350)
at Permutations.DoPermute(Permutations.java:36)
at Permutations.Permute(Permutations.java:20)
at Permutations.main(Permutations.java:7)
Press any key to continue . . .

Can someone please help me.. whats the problem here....
amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
Hello,

StringBuffer(int length)
Constructs a string buffer with no characters in it and an initial capacity specified by the length argument. It just set capacity of new StringBuffer and it does not initiate it to '0' index.

so following code will do for specific case :

class Permutations
{
public static void main(String[] args)
{
StringBuffer str = new StringBuffer("abc ");
Permute(str);
}

private static void Permute(StringBuffer str)
{
int length, i;
length = str.length();
// This is your old code StringBuffer out = new StringBuffer(length);
// this is new one
StringBuffer out = new StringBuffer(" ");

int[] used = new int[length];

for(i=0;i<length;i++)
{
used[i]=0;
}
DoPermute(str,out,used,length,0);
}

private static void DoPermute(StringBuffer str, StringBuffer out, int[] used,int length,int recursLev)
{
try
{
int i;
if(recursLev == length)
{
System.out.println(out);
return;
}

for(i=0;i<length;i++)
{
if(used[i]==1)
{
continue;
}

out.setCharAt(recursLev,str.charAt(i)) ;

used[i]=1;
DoPermute(str,out,used,length,recursLev+1);
used[i]=0;

}
}
catch (Exception e) {System.out.println ("Error : " + e);}
}
}




Regards

Amit Pandit
Vidya Ramachandran
Greenhorn

Joined: Nov 04, 2005
Posts: 24
thanks

but even after that i get StringIndexOutOfBoundsException.

cant figure out whats wrong...:-(
amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
No, its giving correct o/p here

Note : StringBuffer out = new StringBuffer(" ");
// the spaces in a constructor should match your length
means -- > " " this should be of length 4

bye
Vidya Ramachandran
Greenhorn

Joined: Nov 04, 2005
Posts: 24
thanks a lot amit.

but isnt such a creation improper. How else can we handle this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer's setCharAt - near help
 
Similar Threads
Passing a Variable value from one Method to another Method
String combinations & permutations
Printing out the combinations of a given string
Tricky task! Help needed...
Permutations