What’s the error in my code--------------
import java.util.*;
public class Main {
public static boolean comp(StringBuilder x, StringBuilder y){
StringBuilder xy = x.append(y);
StringBuilder yx = y.append(x);
String str1 = xy.toString();
String str2 = yx.toString();
int xy1 = Integer.parseInt(str1);
int yx1 = Integer.parseInt(str2);
return xy1 > yx1;
}
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t–>0){
int n = cin.nextInt(), arr[] = new int[n];
for(int i = 0;i<arr.length;i++){
arr[i] = cin.nextInt();
}
for(int i = 0;i<arr.length;i++){
for(int j = 0;j<arr.length-1;i++){
StringBuilder x = new StringBuilder(arr[j]);
StringBuilder y = new StringBuilder(arr[j+1]);
if(comp(x,y)){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int s : arr){
System.out.print(s);
}
}
}
}
-----------------form biggest number problem two----------
import java.util.*;
public class Main {
public static boolean comp(StringBuilder x, StringBuilder y){
StringBuilder temp=new StringBuilder(x);
StringBuilder xy = x.append(y);
StringBuilder yx = y.append(temp);//check we need to store the value of x in temp as x.append() changes the value of x
String str1 = xy.toString();
String str2 = yx.toString();
int xy1 = Integer.parseInt(str1);
int yx1 = Integer.parseInt(str2);
return xy1 < yx1;
}
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
while(t-- > 0){
int n = cin.nextInt(), arr[] = new int[n];
for(int i = 0;i<arr.length;i++){
arr[i] = cin.nextInt();
}
for(int i = 0;i<arr.length;i++){
for(int j = 0;j<arr.length-1;j++){//here the increment statement should be j++
StringBuilder x = new StringBuilder(arr[j]+"");//here we need to convert the numbers as string
StringBuilder y = new StringBuilder(arr[j+1]+"");
if(comp(x,y)){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int s : arr){
System.out.print(s);
}
System.out.println();//new line has to be printed
}
}
}
only one test case pass