Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. public class Order {
  2. public static String order(String words) {
  3. String[] arrayOfWords = words.split("\\s+");
  4. int amountOfWords = arrayOfWords.length+1;
  5. String[] orderedWords = new String[amountOfWords-1];
  6. int i = 0;
  7.  
  8. if (words == "") return "";
  9.  
  10. while(i < amountOfWords-1){
  11.  
  12. int number = Integer.parseInt(extractNumber(arrayOfWords[i]));
  13. orderedWords[number-1] = arrayOfWords[i] + " ";
  14. i++;
  15. }
  16. String s = new String();
  17. for (String w : orderedWords){
  18. s += w;
  19. }
  20.  
  21. return s.trim();
  22.  
  23. }
  24.  
  25. public static String extractNumber(String str){
  26. if(str == null || str.isEmpty()) return "";
  27. StringBuilder sb = new StringBuilder();
  28. boolean found = false;
  29. for(char c : str.toCharArray()){
  30. if(Character.isDigit(c) && c != '0'){
  31. sb.append(c);
  32. found = true;
  33. } else if(found){
  34. break;
  35. }
  36. }
  37. return sb.toString();
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement