-
Vector and StreamTokenizer problem
Hi all ,
im a java noob and i have been working on this coding for a couple of days now. I now understand all of the methods associated with vectors and streamtokenizers. Anyhows i am trying to store the values of every token parsed through the inputStream so that i can store it either as a vector or arraylist or array(preferably), from where i will create objects from certain locations in the array/vector.
The problem i have though is that the Vector cannot store primitive values just objects and arrays can only have 1 type of value, however the txt file in the inputStream holds int, String and char values and arraylist cannot be searched through. So obviously, thats why im using a StreamTokenizer (TT_WORD, TT_NUMBER etc..) , but i just am at a complete loss, i think i have looked at this coding for too long and am stuck any ideas anyone?
Code:
import java.io.*;
import java.util.*;
class t
{
/* Main method */
public static void main(String[] args) throws IOException
{
FileReader file = new FileReader("Stock.txt");
StreamTokenizer inputStream = new StreamTokenizer(file);
int tokenType = 0;
int numberOfTokens = -1;
// Process the file and output the number of tokens in the file
do {
tokenType = inputStream.nextToken();
outputTtype(tokenType,inputStream);
numberOfTokens++;
}
while (tokenType != StreamTokenizer.TT_EOF);
// Output result and close file
System.out.println("Number of tokens = " + numberOfTokens);
}
/* OUTPUT TTYPE: Methof to output the ttype of a stream token and
its value. */
private static void outputTtype(int ttype, StreamTokenizer inStream) {
switch (ttype) {
///////***////TT_EOL/////////***/////
case StreamTokenizer.TT_EOL:
System.out.println("EOL");
break;
/////////////////////////
//////***/////TT_EOF/////***/////////
case StreamTokenizer.TT_EOF:
System.out.println("TT_EOF");
break;
/////////////////////////
///***////////TT_WORD/////////***/////
case StreamTokenizer.TT_WORD:
System.out.println("TT_WORD: sval = " + inStream.sval);
break;
/////////////////////////
//////***////TT_NUMBER//////***/////////
case StreamTokenizer.TT_NUMBER:
int IntCounter=0;
IntCounter++;
System.out.println("TT_NUMBER: nval = " + inStream.nval);
break;
/////////////////////////
//////***/////DEFAULT///***///////////
default:
System.out.println("Unknown: nval = " + inStream.nval +
" sval = " + inStream.sval);
break;
/////////////////////////
}
}
}
-
Hi,
Couple of things -
Firstly - you can store a primitive in a vector by wrapping it with its associated
for instance the object class for int is Integer and is created using
Integer i = new Integer(whatever the int is);
see http://java.sun.com/j2se/1.4.2/docs/...g/Integer.html
this can be stored in the Vector - there are other classes for the other primitives.
Second you can cycle through a Vector and temporarily getting the item out
and assigning it to an Object class and reassigning it once you know the type.
You can find the type of any class by using the instanceof operator
see http://acdk.sourceforge.net/acdk/doc...nstanceof.html for an example.
I hope this gives you some new ideas on how to process these different types.
Hope this helps
Graham
Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)
http://www.grahamrobinsonsoftware.com
-
Use Scanner?
Have you looked at the Scanner class (J2SE 5)? You can build strings, make an array of the tokens, etc. You can set your delimiter(s), and change them programmatically. I used it to break a large bit of text into tokens using one delimiter, then parsed each of the tokens into new tokens based on a different delimiter. It will recognize the type of the token ( integer, char, etc.) and you can then branch your program to take action based upon the type you find (using NextInteger or NextChar, etc). It is a powerful class, one which might help you do what you're looking for!
-
Good call - I keep forgetting about these new Java 5 features ;-)
Hope this helps
Graham
Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)
http://www.grahamrobinsonsoftware.com
-
Would there be a possiblity to show me some coding on that class. As normally when i read a new classes method, constructors etc.. it normally comes out in jibberish. It would be appreciated alot.
-
Use of Scanner Class
Here's a sample.
This assignment was to build a "trip planner" - it takes text input from a file, giving route "intersections" and the distances and segments each "node" connects with. The text file was delimited in a particular way to give the data I used to build a digraph - main sections separated by #, data within those sections separated by whitespace.
This shows how you can compile regular expressions (or just use a string) to find text within the token.
Code:
private void buildVertexList()
{
int total = 0;
try
{
scan = new Scanner( new File( fileName ) ).useDelimiter("#");
}
catch ( FileNotFoundException e )
{
System.err.println( "File was not found." );
}
while (!scan.hasNext(Pattern.compile("\\s+LOCATIONS\\s+" ) ) )
{
scan.next();
}
scan.next();
Scanner tempScan = new Scanner(scan.next() );
while ( !tempScan.hasNextInt())
{
tempScan.next();
}
totalLocs = tempScan.nextInt();
int counter = 0;
while ( counter < totalLocs )
{
Scanner newScan = new Scanner(scan.next() );
newScan.nextLine() ;
while ( newScan.hasNext() )
{
VertexNode node = new VertexNode( newScan.nextLine() );
myDigraph.addVertexNode( node );
counter++;
}
}
scan.close();
}
private void buildEdgeList( )
{
int start = 0;
int end = 0;
try
{
scan = new Scanner( new File( fileName ) ).useDelimiter("#");
}
catch ( FileNotFoundException e )
{
System.err.println( "File was not found." );
}
while (!scan.hasNext(Pattern.compile("\\s+ROAD SEGMENTS\\s+" ) ) )
{
scan.next();
}
scan.next();
Scanner tempScan = new Scanner(scan.next() );
while ( !tempScan.hasNextInt())
{
tempScan.next();
}
totalEdges = tempScan.nextInt();
int count = 0;
while ( count < totalEdges )
{
Scanner newScan = new Scanner(scan.next() );
newScan.nextLine();
while ( newScan.hasNext() )
{
start = newScan.nextInt();
end = newScan.nextInt();
EdgeNode node = new EdgeNode( start, end );
node.setDistance( newScan.nextDouble() );
node.setSpeed( newScan.nextDouble() );
node.calculateTime();
myDigraph.addEdgeNode( node );
count++;
}
}
scan.close();
}
private void getTrips()
{
int start = 0;
int end = 0;
char type = 0;
try
{
scan = new Scanner( new File( fileName ) ).useDelimiter("#");
}
catch ( FileNotFoundException e )
{
System.err.println( "File was not found." );
}
while (!scan.hasNext(Pattern.compile("\\s+TRIPS\\s+" ) ) )
{
scan.next();
}
scan.next();
Scanner tempScan = new Scanner(scan.next() );
while ( !tempScan.hasNextInt())
{
tempScan.next();
}
totalTrips = tempScan.nextInt();
int count = 0;
while ( count < totalTrips )
{
Scanner newScan = new Scanner(scan.next() );
newScan.nextLine();
while ( newScan.hasNext() )
{
start = newScan.nextInt();
end = newScan.nextInt();
type = (newScan.next()).charAt(0);
System.out.print( start);
System.out.print( end );
System.out.println( type);
count++;
}
}
scan.close();
}
A section of the text file follows:
Code:
# LOCATIONS
# number of locations
15
# intersections
1st St & 101st Ave
1st St & 102nd Ave
1st St & 103rd Ave
2nd St & 101st Ave
2nd St & 102nd Ave
2nd St & 103rd Ave
3rd St & 101st Ave
3rd St & 102nd Ave
3rd St & 103rd Ave
# Freeway North locations
Freeway North @ 101st Ave
Freeway North @ 102nd Ave
Freeway North @ 103rd Ave
# Freeway South locations
Freeway South @ 101st Ave
Freeway South @ 102nd Ave
Freeway South @ 103rd Ave
# ROAD SEGMENTS
# number of road segments
40
# 1st St
0 1 1.5 30.0
1 0 1.5 27.5
1 2 2.0 30.5
2 1 2.0 27.5
# 2nd St
3 4 1.5 26.5
4 3 1.5 28.0
4 5 2.0 30.2
5 4 2.0 24.5
# 3rd St
6 7 1.5 29.0
7 6 1.5 28.5
7 8 2.0 35.2
8 7 2.0 32.5
# 101st Ave
0 3 2.3 37.5
3 0 2.3 37.4
3 6 2.5 39.2
6 3 2.5 38.5
# 102nd Ave
1 4 2.3 30.3
4 1 2.3 32.5
4 7 2.5 31.5
7 4 2.5 30.9
Last edited by nspils; 02-23-2005 at 12:27 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|