Advertisement
Guest User

Java - Messaging

a guest
Feb 23rd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.text.DecimalFormat;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11.     public static void main (String [] args){
  12.         Scanner scanner = new Scanner(System.in);
  13.        
  14.         List<Integer> numbers = Arrays.stream(scanner.nextLine()
  15.         .split(" ")).map(Integer::parseInt)
  16.         .collect(Collectors.toList());// List of numbers
  17.        
  18.         String text = scanner.nextLine();// String
  19.        
  20.         List<String> list = Arrays.stream(text.split(""))
  21.         .collect(Collectors.toList());// Turns the string into List
  22.        
  23.         List<String> result = new ArrayList<>();// Empty list for the output
  24.        
  25.         for (int i=0; i<numbers.size(); i++){// For loop, repeats for each number in the number list
  26.             int element = digitsSum(numbers.get(i));// The char position in the string; comes from the method below
  27.            
  28.             if (element>=list.size()){// If the char position is beyond the list size, start counting from 0
  29.                 element = element - list.size();
  30.             }
  31.             result.add(list.get(element));// Takes the character and puts it in the result list
  32.             list.remove(element);// Removes the character from the original list
  33.         }
  34.        
  35.         for (String letter : result){// Prints the final result as string
  36.             System.out.print(letter);
  37.         }
  38.     }
  39.    
  40.     public static int digitsSum (int number){// Method to get the sum of digits from a number
  41.         String temp = "" + number;
  42.         int result = 0;
  43.        
  44.         for (int i=0; i<temp.length(); i++){
  45.             result+=(int)temp.charAt(i) - 48;
  46.         }
  47.         return result;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement