-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaBigDecimal.java
More file actions
74 lines (59 loc) · 2.03 KB
/
JavaBigDecimal.java
File metadata and controls
74 lines (59 loc) · 2.03 KB
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
/**
* Challenge: https://www.hackerrank.com/challenges/java-bigdecimal
*
* Made by Charles M. Chong
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class SolutionBigDecimal {
public static void main(String[]args) throws java.lang.Exception{
int n = 0;
BigDecimal bigDecimalInputs = new BigDecimal("200");
DecimalFormat f = new DecimalFormat("");
Scanner in = new Scanner(System.in);
boolean finishLoop = false;
while (finishLoop != true){
try {
//System.out.println("Enter number inputs. Max is 200.");
n = in.nextInt();
ArrayList<BigDecimal> holdInputs = new ArrayList<BigDecimal>();
// 'if' condition to confirm 'n' input is less than or equal to 200
if (n <= 200 && n >= 1){
// Ask for input; Keep taking input until 'n' inputs are entered.
for (int i = 0;i < n; i++){
bigDecimalInputs = in.nextBigDecimal();
// Condition that checks that input is <= 300 digits by changing input to string and counts the length
if ((bigDecimalInputs.toString().length() <= 300) && (bigDecimalInputs.toString().length() >= 0)) {
holdInputs.add(bigDecimalInputs);
}
else {
System.out.println("Number of digits cannot exceed 300 digits");
}
finishLoop = true;
}
} else if (n <= 0 && n >= 201){
System.out.println("Number of inputs is too big or not a positive real number. Starting over...");
}
else {
System.out.println("Not valid input. Max is 200 and must input a positive real number. Starting over...");
}
Collections.sort(holdInputs, new Comparator<BigDecimal>() {
public int compare(BigDecimal o1, BigDecimal o2) {
return o2.compareTo(o1);
}});
for (BigDecimal printOut:holdInputs){
if ((printOut.toString().matches("^0+.+") == true)){
System.out.println(printOut.toPlainString().replace("0.", "."));
}
else {
System.out.println(printOut.toPlainString());
}
}
}
catch (Exception e){
}
}
}
}