I'm trying to access each element from a string object and concatenate it with another string:
Current output: from object -> hrsize
4,77GB
1,91GB
2,86GB
After converting KB's to GB's I need to give a name to all outputs. To be like this:
TotalMemory: 4,77GB
MemoryAvailable: 1,91GB
MemoryFree: 2,86GB
With this output, I will be able to convert this to a Hashmap. (k , v)
I tried to iterate over the list to assign a name to each value but without success.
I will leave the full code:
class Example {
static void main(String[] args) {
int A = 5000000 //TotalMemory -> Kb
int B = 2000000 // MemoryAvailable -> Kb
int C = 3000000 // MemoryFree -> Kb
int[] array = [ A, B, C ] // Convert all variables into an Array
for(int i in array) {
def size = i // Converting each element from the object to be converted into MB GB or TB
// Memory can be converted in KB MB GB AND TERABYTE depending on its size
String hrSize = ""
try {
int k = size
double m = size / 1024 // bytes
double g = size / 1048576 // bytes
double t = size / 1073741824 // bytes
DecimalFormat dec = new DecimalFormat("0.00")
if (k > 1) {
//If the size is more than 1kb but less than 1024 bytes, the output will be KiloBytes
hrSize = dec.format(k).concat("KB")
}
if (m > 1) {
//If the size is more than 1kb but less than 1048576 bytes, the output will be Megabytes
hrSize = dec.format(m).concat("MB")
}
if (g > 1) {
//If the size is more than 1kb and more than 1048576 but less than 1073741824 bytes, the output will be Gigabytes
hrSize = dec.format(g).concat("GB")
}
if (t > 1) {
//If the size is more than 1073741824 bytes the output will be Terabytes.
hrSize = dec.format(t).concat("TB")
}
} catch (Exception e) {
println("This program ran into a problem, root cause" + e)
}
println(hrSize)
}
}
}
I really hope someone can help me because I'm struggling with this for days now. Probably this is something pretty basic but I did my best to solve it alone; no joy!