fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String text = "Nothing right in my brain. Nothing left in my brain";
  13.  
  14. int max = 4;
  15. int min = 2;
  16.  
  17. Random rand = new Random();
  18. int randomNum = rand.nextInt((max - min) + 1) + min;
  19. // After split string into words:
  20.  
  21. String[] words = text.split(" ");
  22. // Then, get 4 different numbers from 1 to words.length
  23.  
  24. ArrayList<Integer> randomPositions = new ArrayList<Integer>(randomNum);
  25. max = words.length;
  26. min = 1;
  27. for (int count = 0; count < randomNum; count ++) {
  28. int random = rand.nextInt((max - min) + 1) + min;
  29. if (randomPositions.contains(random)) count --;
  30. else randomPositions.add(random);
  31. }
  32. // Finally put \n in positions when rebuilding the array:
  33.  
  34. StringBuilder result = new StringBuilder();
  35. for (int count = 0; count < max; count ++) {
  36. result.append(words[count]);
  37. if (randomPositions.contains(count))
  38. result.append("\n");
  39. else
  40. result.append(" ");
  41. }
  42.  
  43.  
  44. System.out.println(result);
  45. }
  46. }
Success #stdin #stdout 0.1s 321280KB
stdin
Standard input is empty
stdout
Nothing right in my
brain. Nothing
left in my brain