I know “while (((str.nextToken()!=str.TT_EOL)))” is an infinite loop, but I want to know how can I end this loop in a similar way that in StringTokenizer as “while (st.hasMoreTokens())”, it only needs to read 20 tokens, this is my code:
Help really appreciated:
import java.io.*;
import java.util.Arrays;
public class StonePile {
public static void main(String args[]) throws IOException {
StreamTokenizer str = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//int diff = 0;
str.nextToken();
int[] array = new int[(int) str.nval + 1];
int array2[] = new int[array.length - 1];
//System.out.println(array.length);
int i = 0;
int diff = 0;
while (((str.nextToken() != str.TT_EOL))) {
if (str.ttype == StreamTokenizer.TT_NUMBER) {
System.out.println(i);
array[i++] = (int) str.nval;
}
// if (i > 4) {
// break;
// }
}
for (int j = 0; j < array.length - 2; j++) {
diff = Math.abs(array[j + 1] - array[j]);
array2[j] = diff;
// System.out.println("El array es en" + j+" es"+ array2[j]);
}
Arrays.sort(array2);
System.out.println(array2[1]);
}
}
If you want the first 20 any tokens use:
If you want the first 20 TT_NUMBER use: