Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays; // need this for sorting
- import java.util.Scanner; // need this for reading user input
- public class ReverseInputNumbers
- {
- public static void main( String[] args ) {
- // read/process/write imperative/functional model -
- // not really as OOP as it ought to be, but better than
- // simply doing monolithic linear, imperative programming
- // in the main() method
- // read - collect user input
- int[] integers = getUserInput();
- // process - determine desired characteristics
- int[] reversed = reverse( integers );
- int min = getMin( integers );
- int max = getMax( integers );
- // write - report results
- report( integers, reversed, min, max);
- }
- // read - - - - - -
- public static int[] getUserInput() {
- Scanner scan = new Scanner( System.in );
- System.out.println(
- "Enter a list of positive integers separated by blanks" +
- "\n(like 1 2 3 4)"
- );
- // by treating initial input as String, there is no artificial
- // attempt to restrict number of integers that can be collected
- String[] sIntegers = scan.nextLine().split( " " );
- // number of substrings in sIntegers will be number of actual
- // integers initially expected - need to try to convert from
- // String to int
- int last = sIntegers.length;
- int[] integers = new int[ last ];
- int badValueCnt = 0;
- for (int i = 0; i < last; i++) {
- try {
- int intI = Integer.parseInt( sIntegers[ i ] );
- // negative int would parse, but it's unwanted
- if (0 > intI) badValueCnt++;
- else integers[ i - badValueCnt ] = intI;
- }
- // if bad input, increment counter
- catch (NumberFormatException e) {
- badValueCnt++;
- }
- }
- // if any bad values were encountered, there will be empty (zero-filled
- // by default) positions at end of array...drop those before returning
- if (0 < badValueCnt) integers = dropBadValues( integers, badValueCnt);
- return integers;
- }
- public static int[] dropBadValues( int[] integers, int badValueCnt ) {
- int last = integers.length - badValueCnt;
- int[] keep = new int[ last ];
- for (int i = 0; i < last; i++) {
- keep[ i ] = integers[ i ];
- }
- return keep;
- }
- // process - - - - - -
- public static int[] reverse( int[] integers ) {
- int last = integers.length;
- int[] reversed = new int[ last ];
- for (int i = 0; i < last; i++) {
- reversed[ i ] = integers[ last - i - 1];
- }
- return reversed;
- }
- public static int getMin( int[] integers ) {
- // use a copy to avoid side effects
- int[] copy = Arrays.copyOf(integers, integers.length);
- Arrays.sort( copy );
- return copy[ 0 ];
- }
- public static int getMax( int[] integers ) {
- // use a copy to avoid side effects
- int[] copy = Arrays.copyOf(integers, integers.length);
- Arrays.sort( copy );
- return copy[ copy.length - 1 ];
- }
- // write - - - - - -
- public static void report( int[] integers, int[] reversed, int min, int max) {
- System.out.println(
- "integers input were:\n" + getIntString( integers ) +
- "\n\nreversed integers are:\n" + getIntString( reversed ) +
- "\n\nminimum is " + min + " and maximum is " + max
- );
- }
- public static String getIntString( int[] integers ) {
- // format int[] into comma-delimited String of numerals
- // visual separator for individual elements when printed - can be
- // anything - will occur between each pair of items
- String sep = ", ";
- int last = integers.length;
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < last; i++) {
- sb.append( integers[ i ] + sep);
- }
- // chop extraneous, trailing sep before returning
- return sb.toString( ).substring(0, sb.length() - sep.length());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement