• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}

}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HEY WHAT WHAT I DO TO PRINT OUT IN DESCENDING ORDER.........
THANX..........
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic