-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArraysDS.java
76 lines (57 loc) · 1.64 KB
/
ArraysDS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
*
*/
package org.gradle;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author ankitkumar
*
*/
public class ArraysDS {
private final static String EMPTY_STRING=" ";
/**
* @param args
*/
public static void main(String[] args) {
ArraysDS.Arguments input=new ArraysDS.Arguments();
ArraysDS mySolution=new ArraysDS();
mySolution.readInput(input);
mySolution.doReverse(input);
}
private void doReverse(Arguments input){
for(int index=input.length-1;index>=0;index--){
System.out.print(input.inputArray[index]+EMPTY_STRING);
}
}
private void readInput(Arguments input) {
Scanner inputScanner=new Scanner(System.in);
try{
input.length=Integer.parseInt(inputScanner.nextLine());
if(!(1<=input.length && input.length<=1000)){
throw new IllegalArgumentException("Invalid length");
}
String[] inputArrayString=inputScanner.nextLine().split(EMPTY_STRING);
if(input.length!=inputArrayString.length){
throw new IllegalArgumentException("Invalid input array");
}
input.inputArray=new int[input.length];
for(int index=0;index<input.length;index++){
int element=Integer.parseInt(inputArrayString[index]);
if(!(1<=element && element<=10000)){
throw new IllegalArgumentException("Invalid input array element");
}else{
input.inputArray[index]=element;
}
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid input array");
}finally{
inputScanner.close();
}
}
public static class Arguments {
private int length;
private int[] inputArray;
}
}