Ad

Ok, it took me a while to understand what is meant to be done here.
For what I've got, you would like to sort string of integers by their weight, which means sum of all its digits.

If I am right - I've found out that there was a mistake in tests. You have placed 2000 after 11's. As they are equal according to weight (2+0+0+0 = 1+1), they are sorted to be next to each other. The one which is placed closer to the beginning of input string will be first - so 2000 is right to be first here.

By the way I've written my own snippet of code which does what you wanted (I guess) in less number of code lines.

Hope you find it usefull.

Code
Diff
  • import java.util.stream.IntStream;
    import java.util.stream.Collectors;
    import java.util.Arrays;
    
    public class WeightSort {
    	
    	public static String orderWeight(String strng) {
        return Arrays.stream(strng.split(" "))
                     .sorted((current, next) -> Integer.compare(countWeight(current), countWeight(next)))
                     .collect(Collectors.joining(" "));
    	}
      
      private static int countWeight(String str){
        return Arrays.stream(str.split(""))
                     .mapToInt(character -> Integer.parseInt(character))
                     .sum();
      }
    }
    • import java.util.*;
    • import java.util.stream.IntStream;
    • import java.util.stream.Collectors;
    • import java.util.Arrays;
    • public class WeightSort {
    • public static String orderWeight(String strng) {
    • if( strng =="2000 10003 1234000 44444444 9999 11 11 22 123"){
    • return "11 11 2000 10003 22 123 1234000 44444444 9999";
    • }
    • if(strng ==""){
    • return "";
    • }
    • System.out.println(strng);
    • String[] str= strng.split(" ");
    • int[][] tab = new int[3][30];
    • for(int i=0;i<str.length; i++){
    • int sum=0;
    • String ss=str[i];
    • for(int j=0; j< ss.length(); j++){
    • sum+= Integer.parseInt(Character.toString(ss.charAt(j)));
    • }
    • tab[0][i]=Integer.parseInt(ss);
    • tab[1][i]=sum;
    • tab[2][i]=i+1;
    • }
    • int len = 0,m=0;
    • while(tab[0][m] != 0){
    • len++;
    • m++;
    • }
    • int[][] tab2= new int[3][len];
    • for(int n=0; n<len;n++){
    • tab2[0][n]=tab[0][n];
    • tab2[1][n]=tab[1][n];
    • tab2[2][n]=tab[2][n];
    • }
    • int temp=0;
    • for (int k = 1; k < tab2[0].length; k++) {
    • for(int j = k ; j > 0 ; j--){
    • if(tab2[1][j] < tab2[1][j-1]){
    • temp = tab2[1][j];
    • tab2[1][j] = tab2[1][j-1];
    • tab2[1][j-1] = temp;
    • temp = tab2[0][j];
    • tab2[0][j] = tab2[0][j-1];
    • tab2[0][j-1] = temp;
    • temp = tab2[2][j];
    • tab2[2][j] = tab2[2][j-1];
    • tab2[2][j-1] = temp;
    • }
    • if (tab2[1][j] == tab2[1][j-1] && tab2[2][j] < tab2[2][j-1]){
    • temp = tab2[0][j];
    • tab2[0][j] = tab2[0][j-1];
    • tab2[0][j-1] = temp;
    • temp = tab2[1][j];
    • tab2[1][j] = tab2[1][j-1];
    • tab2[1][j-1] = temp;
    • temp = tab2[2][j];
    • tab2[2][j] = tab2[2][j-1];
    • tab2[2][j-1] = temp;
    • }
    • }
    • }
    • String ret="";
    • for(int ll=0; ll<len; ll++){
    • System.out.println(tab2[0][ll]+":"+tab2[1][ll]+":"+tab2[2][ll]);
    • ret+= tab2[0][ll]+" ";
    • }
    • return ret.substring(0, ret.length() - 1);
    • return Arrays.stream(strng.split(" "))
    • .sorted((current, next) -> Integer.compare(countWeight(current), countWeight(next)))
    • .collect(Collectors.joining(" "));
    • }
    • private static int countWeight(String str){
    • return Arrays.stream(str.split(""))
    • .mapToInt(character -> Integer.parseInt(character))
    • .sum();
    • }
    • }

Does the same, just looks a bit neater.

Code
Diff
  • import java.util.stream.IntStream;
    public class CountTheDigit {
    
      public static int count(int num, int digit) {
        int result = 0;
        
        do {
          if (num % 10 == digit)
            ++result;
          num /= 10;
        } while (num != 0);
        
        return result;
      }
      
    	public static int nbDig(int n, int d) {
      	return IntStream.range(0,n)
          .map(i -> count(i * i, d))
          .sum();
      }
    }
    • import java.util.stream.IntStream;
    • public class CountTheDigit {
    • public static int count(int num, int digit) {
    • int result = 0;
    • do {
    • if (num % 10 == digit)
    • ++result;
    • num /= 10;
    • } while (num != 0);
    • return result;
    • }
    • public static int nbDig(int n, int d) {
    • int result = 0;
    • for(int i = 0; i < n; ++i)
    • result += count(i * i, d);
    • return result;
    • return IntStream.range(0,n)
    • .map(i -> count(i * i, d))
    • .sum();
    • }
    • }

It requires some optimization, but it works with just any number.

Code
Diff
  • import java.util.stream.IntStream;
    
    public class Primes {
      public static boolean isAPrime(int number) {
        return IntStream.range(2, number)
          .noneMatch(divider -> (number % divider) == 0); 
      }
    }
    • import java.util.stream.IntStream;
    • public class Primes {
    • public static boolean isAPrime(int number) {
    • if (number < 2 || (number % 2 == 0 && number != 2)) return false;
    • for (int i = 3; i*i <= number; i += 2) {
    • if (number % i == 0) return false;
    • }
    • return true;
    • return IntStream.range(2, number)
    • .noneMatch(divider -> (number % divider) == 0);
    • }
    • }