I have written a program to read integers from a file, but my output is not correct. let say if my input is (3 5 8 12 4 15 7). the first numbers tells me how big the array is.. so by looking at this input the array would be (3). So my out put should be (3 5 instead my out put is(5 8 12). if I try to print nElements it prints(3 3 3) so nElements is holding that first array. but I am tring to print(3 5 can someone help me.... This is my code........
import java.io.*; import java.util.*; public class assmtO {
FileReader fr = new FileReader("file.text"); BufferedReader first = new BufferedReader(fr); String numbers = new String(); StringTokenizer st; long j; int nElements = 0; long[ ] longArray = null; int size = 0; Vector vec = new Vector();
while ((numbers = first.readLine()) !=null) {
nElements = 0; st = new StringTokenizer(numbers);
//find the size of the array
if (st.hasMoreElements() ) {
size = Integer.parseInt(st.nextToken()); longArray = new long [size];
}
//read all elements and place them in the longArray[...] while (st.hasMoreElements()) {
Whats wrong with your output? If your first element says how long the array is with the numbers you are passing through the file it says three. Which means the array size is three. So we have to ignore anyway the first element in the file since its the size of the array and not the number itself. Numbers start from the position 1. and you are getting 5 8 12 as your output. Whats wrong with you? Isn't that what it should do?