Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help

 
Alan Green
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alan Green
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HEY WHAT WHAT I DO TO PRINT OUT IN DESCENDING ORDER.........
THANX..........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic