aspose file tools*
The moose likes Java in General and the fly likes Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help" Watch "Help" New topic
Author

Help

Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
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
{
public static void main(String [ ] args) throws IOException
{
try
{

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())
{
j = Long.parseLong(st.nextToken());
longArray[nElements++] = j;

if(nElements>= size)
break;
}
//if there are less elements then the size, delete the empty fields (zeros).
if(nElements<size)
{
int misses = size-nElements;
long[ ] newLongArray = new long[size - misses];
System.arraycopy(longArray,0,newLongArray,0,size - misses);
vec.add(newLongArray);
}
else
vec.add(longArray);
}
//debug: read the longArray elemtns.
for( int v = 0; v<vec.size(); v++ )
{
longArray = (long[])vec.elementAt(v);
System.out.println("--- row:" + v + " / elements:"+longArray.length+" ---");
for (int i = 0; i < longArray.length; i++)
{
System.out.println(longArray[i] );
}
}
first.close();
}
catch(EOFException e)
{
System.out.println("End of Stream");
}
catch(IOException e)
{
System.out.println(e.getMessage());
}

}
}
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
First - without indenting your code - nobody wants to try to read it :roll: .
Next, are you saying that you want the first token of the input string to be the first element of the array? And the array should only have as many elements as the first element indicates? So that means that the output should be:
3
5
8
If that is what you mean, then you need to "prime" your array with the first read that you did. Something like"

[ September 08, 2003: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
HEY WHAT WHAT I DO TO PRINT OUT IN DESCENDING ORDER.........
THANX..........
 
 
subject: Help