Advertisement
richarduie

ReverseInputNumbers.java

Apr 2nd, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. import java.util.Arrays; // need this for sorting
  2. import java.util.Scanner; // need this for reading user input
  3.  
  4. public class ReverseInputNumbers
  5. {
  6. public static void main( String[] args ) {
  7. // read/process/write imperative/functional model -
  8. // not really as OOP as it ought to be, but better than
  9. // simply doing monolithic linear, imperative programming
  10. // in the main() method
  11.  
  12. // read - collect user input
  13. int[] integers = getUserInput();
  14.  
  15. // process - determine desired characteristics
  16. int[] reversed = reverse( integers );
  17. int min = getMin( integers );
  18. int max = getMax( integers );
  19.  
  20. // write - report results
  21. report( integers, reversed, min, max);
  22. }
  23.  
  24. // read - - - - - -
  25. public static int[] getUserInput() {
  26. Scanner scan = new Scanner( System.in );
  27. System.out.println(
  28. "Enter a list of positive integers separated by blanks" +
  29. "\n(like 1 2 3 4)"
  30. );
  31. // by treating initial input as String, there is no artificial
  32. // attempt to restrict number of integers that can be collected
  33. String[] sIntegers = scan.nextLine().split( " " );
  34. // number of substrings in sIntegers will be number of actual
  35. // integers initially expected - need to try to convert from
  36. // String to int
  37. int last = sIntegers.length;
  38. int[] integers = new int[ last ];
  39. int badValueCnt = 0;
  40. for (int i = 0; i < last; i++) {
  41. try {
  42. int intI = Integer.parseInt( sIntegers[ i ] );
  43. // negative int would parse, but it's unwanted
  44. if (0 > intI) badValueCnt++;
  45. else integers[ i - badValueCnt ] = intI;
  46. }
  47. // if bad input, increment counter
  48. catch (NumberFormatException e) {
  49. badValueCnt++;
  50. }
  51. }
  52. // if any bad values were encountered, there will be empty (zero-filled
  53. // by default) positions at end of array...drop those before returning
  54. if (0 < badValueCnt) integers = dropBadValues( integers, badValueCnt);
  55. return integers;
  56. }
  57. public static int[] dropBadValues( int[] integers, int badValueCnt ) {
  58. int last = integers.length - badValueCnt;
  59. int[] keep = new int[ last ];
  60. for (int i = 0; i < last; i++) {
  61. keep[ i ] = integers[ i ];
  62. }
  63. return keep;
  64. }
  65.  
  66. // process - - - - - -
  67. public static int[] reverse( int[] integers ) {
  68. int last = integers.length;
  69. int[] reversed = new int[ last ];
  70. for (int i = 0; i < last; i++) {
  71. reversed[ i ] = integers[ last - i - 1];
  72. }
  73. return reversed;
  74. }
  75. public static int getMin( int[] integers ) {
  76. // use a copy to avoid side effects
  77. int[] copy = Arrays.copyOf(integers, integers.length);
  78. Arrays.sort( copy );
  79. return copy[ 0 ];
  80.  
  81. }
  82. public static int getMax( int[] integers ) {
  83. // use a copy to avoid side effects
  84. int[] copy = Arrays.copyOf(integers, integers.length);
  85. Arrays.sort( copy );
  86. return copy[ copy.length - 1 ];
  87. }
  88.  
  89. // write - - - - - -
  90. public static void report( int[] integers, int[] reversed, int min, int max) {
  91. System.out.println(
  92. "integers input were:\n" + getIntString( integers ) +
  93. "\n\nreversed integers are:\n" + getIntString( reversed ) +
  94. "\n\nminimum is " + min + " and maximum is " + max
  95. );
  96. }
  97. public static String getIntString( int[] integers ) {
  98. // format int[] into comma-delimited String of numerals
  99. // visual separator for individual elements when printed - can be
  100. // anything - will occur between each pair of items
  101. String sep = ", ";
  102. int last = integers.length;
  103. StringBuffer sb = new StringBuffer();
  104. for (int i = 0; i < last; i++) {
  105. sb.append( integers[ i ] + sep);
  106. }
  107. // chop extraneous, trailing sep before returning
  108. return sb.toString( ).substring(0, sb.length() - sep.length());
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement