Is it possible to enter the number in a char so that permutation of a number can be acheived which will be in the form of string????? here is the coding i did...it worked really well when i initialized the char as(1,2,3).but its not working when i want the input from user.. .like if user enters 3...then string shud bcum 123 and permutation shud be obtained...plz help me i need to submit coding tomorrow itself...... import java.util.*; public class major1
{ int n=0; char c[]; public major1() {
System.out.println("Enter the number of processors=");
Scanner input=new Scanner(System.in);
n=input.nextInt();
for(int j=1;j<=n;j++)
{
c[n]=j;
}
String s=new String(c);
showPattern("", s);
}
public void showPattern(String st, String s)
{
if (s.length() <= 1)
System.out.println("\n"+st + s);
else
{
for (int i = 0; i < s.length(); i++)
{
try
{
String newString = s.substring(0, i)
+s.substring(i + 1);
showPattern(st + s.charAt(i), newString);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
public static void main(String args[])
{
major1 m1=new major1();
}
}
import java.util.*;
public class PermutationExample extends ArrayList<String> {
public static void main(String[] args) {
int[] set = {1, 2, 3};
PermutationExample retVal = permutations(set, 3);
Collections.sort(retVal);
for (int k = 0; k < retVal.size(); k++) {
System.out.printf(" %s\n", retVal.get(k));
}
}
private static PermutationExample permutations(int[] set, int choices) {
PermutationExample perm =new PermutationExample();
String resetStr, setStr;
resetStr = setStr = "";
for (int j = 0; j < choices; j++) {
resetStr += String.format("%d", set[0]);
setStr = resetStr;
}
java.util.Random rand = new java.util.Random();
int max = 1;
int k = 1;
int nextDown = set.length;
while (k <= choices) {
max *= nextDown;
nextDown--;
k++;
}
while (perm.size()< max) {
setStr = "";
java.util.ArrayList<Integer> ele =new java.util.ArrayList<Integer>();
while (ele.size() < choices) {
int anInt = set[rand.nextInt(set.length)];
if (!ele.contains(anInt)) {
ele.add(anInt);
}
}
setStr =String.format("%d%d%d",ele.get(0), ele.get(1), ele.get(2));
if (!perm.contains(setStr)) {
perm.add(setStr);
}
}
return perm;
}
private boolean contains(String str) {
return ((ArrayList) this).contains(str);
}
}
import java.util.*;
public class PermutationExample {
public static void main(String args[]) throws Exception {
Scanner input = new Scanner(System.in);
System.out.print("Enter String: ");
String chars = input.next();
showPattern("", chars);
}
public static void showPattern(String st, String chars) {
if (chars.length() <= 1)
System.out.println(st + chars);
else
for (int i = 0; i < chars.length(); i++) {
try {
String newString = chars.substring(0, i)
+ chars.substring(i + 1);
showPattern(st + chars.charAt(i), newString);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}