Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer's setCharAt - near help

 
Vidya Ramachandran
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

but even after that i get StringIndexOutOfBoundsException.

cant figure out whats wrong...:-(
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot amit.

but isnt such a creation improper. How else can we handle this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic