I'm new to java and I can't figure it out, any tips?
I'm trying to sum all this numbers
like 719488 4093600 4096453 4096453 4096453 = Something
The result I'm getting is:
1438976
8187200
8192906
8192906
8192906
65536
655360
262144
262144
655360
262144
655360
65536000
2097152
8192906
Basically is multiplicating by 2 each number in each line by its own value.
Please, I need help. Thank you
adding more info:
String strHDD = "df -i\n"
"Filesystem Inodes IUsed IFree IUse% Mounted on\n"
"/dev/mapper/vg0-rootvol 719488 42967 676521 6% /\n"
"devtmpfs 4093600 436 4093164 1% /dev\n"
"tmpfs 4096453 1 4096452 1% /dev/shm\n"
"tmpfs 4096453 1384 4095069 1% /run\n"
"tmpfs 4096453 16 4096437 1% /sys/fs/cgroup\n"
"/dev/sda1 32768 351 32417 2% /boot\n"
"/dev/mapper/vg0-optvol 327680 19465 308215 6% /opt\n"
"/dev/mapper/vg0-homevol 131072 322 130750 1% /home\n"
"/dev/mapper/vg0-tmpvol 131072 166 130906 1% /tmp\n"
"/dev/mapper/vg0-varvol 327680 4486 323194 2% /var\n"
"/dev/mapper/vg0-logvol 131072 163 130909 1% /var/log\n"
"/dev/mapper/vg0-auditvol 327680 34 327646 1% /var/log/audit\n"
"/dev/mapper/vg1-optSPvol 32768000 156936 32611064 1% /opt/SP\n"
"/dev/mapper/vg0-varcrashvol 1048576 11 1048565 1% /var/crash\n"
"tmpfs 4096453 1 4096452 1% /run/user/10603\n"
"[1;31mvg1108yr:resolve:[0m/opt/SP/resolve \$"
String strRegex = "(\\d )\\s (\\d \\s (\\d )(?=\\s \\d \\%))"
Pattern pattern = Pattern.compile(strRegex);
Matcher matcher = pattern.matcher(strHDD);
while (matcher.find()) {
String[] stringArray = matcher.group().split("\\s ")
List<String> list = Arrays.asList(stringArray)
List<Integer> newList = list.stream()
.map(s -> Integer.parseInt(s))
.collect(Collectors.toList());
int [][] num = newList
int columTotal = 0
for (int i = 0; i < num.length; i ){
columTotal = num[i][0]
}
println(newList)
}
output:
[719488, 42967, 676521]
[4093600, 436, 4093164]
[4096453, 1, 4096452]
[4096453, 1384, 4095069]
[4096453, 16, 4096437]
[32768, 351, 32417]
[327680, 19465, 308215]
[131072, 322, 130750]
[131072, 166, 130906]
[327680, 4486, 323194]
[131072, 163, 130909]
[327680, 34, 327646]
[32768000, 156936, 32611064]
[1048576, 11, 1048565]
[4096453, 1, 4096452]
Process finished with exit code 0
or:
int columTotal = 0
for (int i = 0; i < num.length; i ){
columTotal = num[i][0]
}
println(columTotal)
1438976
8187200
8192906
8192906
8192906
65536
655360
262144
262144
655360
262144
655360
65536000
2097152
8192906
Process finished with exit code 0
But it never sums only multiply by two
CodePudding user response:
In your String Inodes = IUsed IFree. When you find with the matcher one by one in each iteration all 3 values Inodes, IUsed, IFree are getting picked up. So each iteration is printing Inodes IUsed IFree. That's why you are getting double values here.
So your code should be like this:
public static void main(String[] args) throws JSONException {
String strHDD = "df -i\n" "Filesystem Inodes IUsed IFree IUse% Mounted on\n"
"/dev/mapper/vg0-rootvol 719488 42967 676521 6% /\n"
"devtmpfs 4093600 436 4093164 1% /dev\n"
"tmpfs 4096453 1 4096452 1% /dev/shm\n"
"tmpfs 4096453 1384 4095069 1% /run\n"
"tmpfs 4096453 16 4096437 1% /sys/fs/cgroup\n"
"/dev/sda1 32768 351 32417 2% /boot\n"
"/dev/mapper/vg0-optvol 327680 19465 308215 6% /opt\n"
"/dev/mapper/vg0-homevol 131072 322 130750 1% /home\n"
"/dev/mapper/vg0-tmpvol 131072 166 130906 1% /tmp\n"
"/dev/mapper/vg0-varvol 327680 4486 323194 2% /var\n"
"/dev/mapper/vg0-logvol 131072 163 130909 1% /var/log\n"
"/dev/mapper/vg0-auditvol 327680 34 327646 1% /var/log/audit\n"
"/dev/mapper/vg1-optSPvol 32768000 156936 32611064 1% /opt/SP\n"
"/dev/mapper/vg0-varcrashvol 1048576 11 1048565 1% /var/crash\n"
"tmpfs 4096453 1 4096452 1% /run/user/10603\n";
String strRegex = "(\\d )\\s (\\d \\s (\\d )(?=\\s \\d \\%))";
Pattern pattern = Pattern.compile(strRegex);
Matcher matcher = pattern.matcher(strHDD);
int columTotal = 0;
while (matcher.find()) {
String[] stringArray = matcher.group().split("\\s ");
List<String> list = Arrays.asList(stringArray);
List<Integer> newList = list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());
columTotal = newList.get(0);
System.out.println("Value to add : " newList.get(0));
}
System.out.println("Total : " columTotal);
}
Result:
Value to add : 719488
Value to add : 4093600
Value to add : 4096453
Value to add : 4096453
Value to add : 4096453
Value to add : 32768
Value to add : 327680
Value to add : 131072
Value to add : 131072
Value to add : 327680
Value to add : 131072
Value to add : 327680
Value to add : 32768000
Value to add : 1048576
Value to add : 4096453
Total : 56424500