Hello
I am doing algorithms(bubblesort) in which 'FileReader' class reads from file(numbers.txt). Numbers are read & display but can't do sorting because all numbers are stored in type integer 'int tok'(please see the code). I require numbers to be stored in array so that with the help of 'for' loop, I can do sorting.
Here is the code(just paste, compile, run)
import java.io.*;
public class FileMethods01
{
public static void main(String[] args) throws Exception
{
BufferedReader in= new BufferedReader(new FileReader("d:/numbers.txt"));
//put some numbers in numbers.txt file
StreamTokenizer input=new StreamTokenizer(in);
int tok;
int count=0;
while((tok=input.nextToken())!=StreamTokenizer.TT_EOF)
if(tok==StreamTokenizer.TT_NUMBER)
{
System.out.println("Numbers Found:" +input.nval);
count++;
}
System.out.println("Found " + count+" numbers in numbers.txt");
}//end of main
}//end of class
so how to take numbers in Array?
-
How to pass data from integer to array? (2 messages)
- Posted by: p s
- Posted on: January 18 2005 02:48 EST
Threaded Messages (2)
- How to pass data from integer to array? by Kishore Senji on January 19 2005 16:57 EST
- How to pass data from integer to array? by p s on January 21 2005 01:24 EST
-
How to pass data from integer to array?[ Go to top ]
- Posted by: Kishore Senji
- Posted on: January 19 2005 16:57 EST
- in response to p s
...
Have a List;
List list = new ArrayList();
while((tok=input.nextToken())!=StreamTokenizer.TT_EOF) if(tok==StreamTokenizer.TT_NUMBER) {
// add numbers to the list
list.add(String.valueOf(input.nval)); System.out.println("Numbers Found:" +input.nval); count++; }
...
After all the processing, sort the list. -
How to pass data from integer to array?[ Go to top ]
- Posted by: p s
- Posted on: January 21 2005 01:24 EST
- in response to Kishore Senji
Thanks for info.