The Artima Developer Community
Sponsored Link

Java Answers Forum
Reading Integers

5 replies on 1 page. Most recent reply: Sep 8, 2003 10:29 PM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Isa

Posts: 6
Nickname: isa
Registered: Sep, 2003

Reading Integers Posted: Sep 7, 2003 5:28 PM
Reply to this message Reply
Advertisement
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 );

}
}

first.close();
}

catch(EOFException e)
{

System.out.println("End of Stream");

}
catch(IOException e)
{

System.out.println(e.getMessage());
}


}
}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Reading Integers Posted: Sep 7, 2003 10:33 PM
Reply to this message Reply
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?

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Reading Integers Posted: Sep 8, 2003 2:41 AM
Reply to this message Reply
Yeah...

This is what you must be expectiong.

If you want to include the firstnumber also
say for ur case

3 5 8 12 4 15 7

3 indicates 3 is the array length;

Ur logic tells leave 3 and make an array of length 3 with next 3 elements.

So it will print 5 8 12.[rememeber indexing start from 0 in array]

If u want to include 3 also in ur output ....

tokenize the line again

st = new StringTokenizer(numbers);
before ur while looop

//read all elements and place them in the longArray[...]
while (st.hasMoreElements())
{
   j = Long.parseLong(st.nextToken());
}

Isa

Posts: 6
Nickname: isa
Registered: Sep, 2003

Re: Reading Integers Posted: Sep 8, 2003 7:12 PM
Reply to this message Reply
I am sorry I am getting the right output.........
What I really having trouble with is printing my output in descending order

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Reading Integers Posted: Sep 8, 2003 10:18 PM
Reply to this message Reply
Thats exactly what I thought !!!

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Reading Integers Posted: Sep 8, 2003 10:29 PM
Reply to this message Reply
sort ur array before priniting....need a sorting code???


http://mindprod.com/jgloss/sort.html

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3308&lngWId=2


else gooogle it yaaar...

Flat View: This topic has 5 replies on 1 page
Topic: ASCII value to character Previous Topic   Next Topic Topic: Apache-Tomcat

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use