Employ the use of arrays
while (sc.hasNextLine()) { if(count==0) lev0 = sc.nextLine(); if(count==1) lev1 = sc.nextLine(); if(count==2) lev2 = sc.nextLine(); if(count==3) lev3 = sc.nextLine(); if(count==4) lev4 = sc.nextLine(); if(count==5) lev5 = sc.nextLine(); if(count==6) lev6 = sc.nextLine(); if(count==7) lev7 = sc.nextLine(); if(count==8) lev8 = sc.nextLine(); if(count==9) lev9 = sc.nextLine(); if(count==10) lev10 = sc.nextLine(); count++; }
You should refactor your 'lev0through
lev10` to utilize an array. This is effectively just a for loop and could be simplified to the following.
for (int i = 0; i <= count; count++) {
// optional point of validation
lev[i] = sc.nextLine();
}
Likewise here:
StringTokenizer tokens = new StringTokenizer(lev0," "); int firstcount = tokens.countTokens(); //System.out.println(firstcount); StringTokenizer tokens1 = new StringTokenizer(lev1," "); int secondcount = tokens1.countTokens(); StringTokenizer tokens2 = new StringTokenizer(lev2," "); int thirdcount = tokens2.countTokens(); StringTokenizer tokens3 = new StringTokenizer(lev3," "); int fourthcount = tokens3.countTokens(); StringTokenizer tokens4 = new StringTokenizer(lev4," "); int fifthcount = tokens4.countTokens(); StringTokenizer tokens5 = new StringTokenizer(lev5," "); int sixthcount = tokens5.countTokens(); StringTokenizer tokens6 = new StringTokenizer(lev6," "); int seventhcount = tokens6.countTokens(); StringTokenizer tokens7 = new StringTokenizer(lev7," "); int eighthcount = tokens7.countTokens(); StringTokenizer tokens8 = new StringTokenizer(lev8," "); int ninthcount = tokens8.countTokens(); StringTokenizer tokens9 = new StringTokenizer(lev9," "); int tenthcount = tokens9.countTokens(); StringTokenizer tokens10 = new StringTokenizer(lev10," "); int eleventhcount = tokens10.countTokens();
Would be better as:
StringTokenizer[] tokens = new StringTokenizer[10];
int[] tokenCount = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
tokens[i] = new StringTokenizer(lev[i], " ");
tokenCount[i] = tokens[i].countTokens();
}
Notice how the use of arrays before lends itself to the following tokenizer loop.
This also applies to your max value logic. Interestingly, you loop through to find it, but then essentially 'loop' again to find out which one it was, why not simply store the position of the element (more about the use of arrays)?
So, your logic loop could be refactored thus:
int maxValue = tokenCount[0];
int maxPosition = 0;
for (int i = 1; i < tokenCount.length; i++) {
if (tokenCount[i] > maxValue) {
maxValue = tokenCount[i];
maxPosition = i;
}
}
So you may just proceed with System.out.println(lev[maxPosition]);
removing the need for:
if(maxValue == firstcount) System.out.println(lev0); if(maxValue == secondcount) System.out.println(lev1); if(maxValue == thirdcount) System.out.println(lev2); if(maxValue == fourthcount) System.out.println(lev3); if(maxValue == fifthcount) System.out.println(lev4); if(maxValue == sixthcount) System.out.println(lev5); if(maxValue == seventhcount) System.out.println(lev6); if(maxValue == eighthcount) System.out.println(lev7); if(maxValue == ninthcount) System.out.println(lev8); if(maxValue == tenthcount) System.out.println(lev9); if(maxValue == eleventhcount) System.out.println(lev10);
- 9.9k
- 4
- 50
- 118