Ad

Given an array of strings and an array of ints, associate them from longest to shortest according to the length of each string. The output format must be a string with the syntax:

name1:number1,name2:number2,name3:number3... nameN:numberN

Input example:
String[]names = {"Ana", "Miguel", "Josemanuel"} int[]numbers = {1034, 21, 537}

Output example:
"JoseManuel:1034,Miguel:537,Ana:21"

Code
Diff
  • import java.util.regex.*;
    import java.util.Arrays;
    import java.util.Comparator;
    
    public class NamesAndNumbers{
      
      public static String run(int[]numbers, String[] names){
        
    		if(numbers == null || names == null) {
    			return "array is null";
    		} else if(numbers.length == 0 || names.length == 0) {
    			return "length is zero";
    		}
        
        if (numbers.length != names.length) {
    			return "The size of the arrays are not the same";
    		}
        
        Arrays.sort(names, Comparator.comparingInt(String::length));
    		Arrays.sort(numbers);
        
        StringBuilder cadenaValue = new StringBuilder();
        
        for(int i = 0; i<names.length; i++) {
    
    			cadenaValue.append(names[i]).append(":").append(numbers[i]).append(",");
    
    		}
        
        cadenaValue.deleteCharAt(cadenaValue.length() - 1);
    
    		String outputValue = cadenaValue.toString();
        
        return outputValue;
        
        
      }
      
    }//end class.
    • import java.util.regex.*;
    • import java.util.Arrays;
    • import java.util.Comparator;
    • public class NamesAndNumbers{
    • public static String run(int[]numbers, String[] names){
    • if(numbers == null || names == null) {
    • return "array is null";
    • } else if(numbers.length == 0 || names.length == 0) {
    • return "length is zero";
    • }
    • if (numbers.length != names.length) {
    • return "The size of the arrays are not the same";
    • }
    • Arrays.sort(names, Comparator.comparingInt(String::length));
    • Arrays.sort(numbers);
    • StringBuilder cadenaValue = new StringBuilder();
    • for(int i = 0; i<names.length; i++) {
    • cadenaValue.append(names[i]).append(":").append(numbers[i]).append(",");
    • }
    • cadenaValue.deleteCharAt(cadenaValue.length() - 1);
    • String outputValue = cadenaValue.toString();
    • return outputValue;
    • }
    • }//end class.

Given an array of strings and an array of ints, associate them from longest to shortest according to the length of each string. The output format must be a string with the syntax:

name1:number1,name2:number2,name3:number3... nameN:numberN

Input example:
String[]names = {"Ana", "Miguel", "Josemanuel"}
int[]numbers = {1034, 21, 537}

Output example:
"JoseManuel:1034,Miguel:537,Ana:21"

Code
Diff
  • public class NamesAndNumbers{
      
      public static String run(int[]numbers, String[] names){
        
        if (numbers.length != names.length) {
    			return "The size of the arrays are not the same";
    		}
        
        return "";
      }
      
    }//end class.
    • public class NamesAndNumbers{
    • public static String run(int[]numbers, String[] names){
    • if (numbers.length != names.length) {
    • return "The size of the arrays are not the same";
    • }
    • return "";
    • }
    • }//end class.