Jump to content

StreamTokenizer (st.nextToken() returns -3???)

- - - - -

  • Please log in to reply
2 replies to this topic

#1
RnAoDm

RnAoDm

    Newbie

  • Members
  • PipPip
  • 13 posts
Hey everyone.
I'm working on a program that sorts shapes based on width, height or base area. Right now though, I am just working on bringing the shapes in from the a .dat file. This file will be brought in as an argument in main. I am having problems when using streamtokenizer. The first token I bring in is always -3, even though there is no -3 in my file. Also when it reads in a 'P' (Prism), and gets the next token, it reads the next token as a -3 as well. Can anyone help me out here? Here's my code and a sample of my current ouput and the text file im reading. Sorry for all the commented out code, I'm ignoring the rest of the program till I can get this working.

SOME OF THE INPUT FILE:
P S 50107.1987 2479088.581 C 1418390.1217 356287.7 C 3852169.1154 1240769.1145 R 2176529.965 246280.999 R 446014.392 1092283.216 P P 2616667.1845 1185752.1802 P O 1345795.1254 2324386.1802 R 3813771.1717 1913696.995 P P 34117.494 2225014.674 C 697064.1189 3228305.1596 R 1223771.340 604192.111 Y 1357700.1597 155494.259 R 1745132.1630 748668.1006 C 375474.914 1934646.1368 R 97479.1028 2144108.1641 Y 3836855.1966 2319236.1973 Y 687945.1727 911542.1549 R 240498.1739 255248.1996 P O 2737975.1485 1126339.728 C 2281792.824 87142.516 R 3784404.1387 229900.883 C 1962988.1634 2647517.1231 Y 1611904.1993 2105676.1085 P S 2207259.476 1003294.445 C 2929697.409 1399212.230 Y 2059928.1159 176662.602 P O 1534548.1648 2502072.1773 P P 338695.826 2548753.688 C 1617318.1571 2589387.594 Y 4052447.1162 2527977.1911 C 776604.475 2376931.80 Y 2051111.946 1896339.1174 C 3334633.1553 2134280.1591 R 3860856.956 436134.720 C 2521405.706 209821.1105 C 2054499.1590 1874607.532 C 1285727.1275 628395.13 R 2891010.1534 1514518.24 R 1094206.1613 2418457.1639 C 3975301.982 3065438.1126 R 1708373.922 2424425.397 Y 3929659.1424 1885934.1310 Y 1858228.1237 2748205.1445 R 2663872.737 3221667.337 Y 1564015.662 3107934.1636 P P 1850207.1090 3054358.1274 Y 146674.291 1522058.372 Y 683016.1498 2226143.1564 R 618671.280 2220495.618 C 1387347.725 1283540.1218 C 2821564.671 272699.1669 Y 1687225.1151 3154442.533 Y 2930449.216 917698.1360 Y 3943872.1033 711153.985 C 3786453.1714 2918280.963 C 1785914.1091 1548337.1148 P T 600280.1855 2886829.1998 Y 1821380.271 1434968.877 C 1564617.1732 3118816.148 C 699072.708 417590.638 C 832938.1638 2733722.13 Y 3662885.1672 1242495.605 P P 3354796.1309 1374330.1199 Y 3590448.1877 3090812.1114 Y 563914.1828 1951605.148 R 1506972.1748 839385.1419 P O 3288438.1734 2087229.1782 Y 2954315.1684 828335.114 R 3436901.562 2454348.1810 C 1383166.851 1715418.606 R 3088593.1800 2447494.489 C 2307162.867 2320034.1494 P P 1614456.1738 942960.21 P O 1693043.998 3189574.1164 C 398891.299 666694.280 R 1329398.386 759549.1579 P O 4297021.1282 477516.787 Y 4344057.1230 1040414.504 Y 3778047.88 2928097.953 Y 340336.834 1711627.1932 R 1388001.565 393902.717 P P 3416494.507 246193.484 C 3669193.390 2049437.102 C 3634741.1172 1349744.2051 R 492788.1530 1857724.813 Y 3552674.829 967147.1572 C 2232070.591 2780204.1769 C 1118071.1414 2528340.1576 P O 359748.1785 1963273.1246 P O 3336956.1812 1470957.23 P S 1333787.1644 313892.1783 R 2666183.204 462202.1428 Y 3447285.1724 1743616.1844 Y 530022.531 289389.780 R 3163234.1923 2597637.1992 C 1686646.35 665102.827 Y 3661800.395 302792.187 R 3981527.1912 84636.1906 C 3629735.319 1323468.172 Y 3150469.318 880670.1285 P S 3546869.1118 475707.1683 C 2554077.965 2180038.1681 Y 2498341.264 2945993.1452 R 4348403.989 2563603.1703 Y 183883.910 1421845.1314 P T 3874744.1018 1031726.99 P S 1390436.1669 3006826.96 P T 1664989.1856 2223904.1637 P O 390480.1459 1704424.1603 Y 4319667.1527 3164169.451 P T 1018793.304 791179.1365 P O 2337157.1847 2959048.1121 P S 4313846.1334 2554140.1347 R 139696.1357 2411204.1159
THE CODE:
package driver;

import java.io.FileReader;
import java.io.IOException;
import java.io.StreamTokenizer;
import utility.Sorting;
import figure.*;

public class AppDriver
{
    public static void main(String[] args)
    {
        args = new String[1];
        //args[0] = "-tv";
        args[0] = "-fres/polygons1.dat";
        //args[2] = "-sz";
        Character compareType = 'V';
        //Character sortType = null;
        @SuppressWarnings("rawtypes")
        Comparable[] figureAry = null;
        double height;
        double radius;
        double edgeLength;
        //int firstToken = 0;
        int token = 0;
        String filename;
        for (int i = 0; i < args.length; i++)
        {
            /*if (args[i].contains("-t") || args[i].contains("-T"))
            {
                args[i] = args[i].substring(2);
                compareType = args[i].toUpperCase().charAt(0);
                System.out.println(compareType);
                switch (compareType)
                {
                case 'V':
                    //width

                    break;
                case 'H':
                    //height

                    break;
                case 'A':
                    //base area

                    break;
                }
            }*/
            if (args[i].contains("-f") || args[i].contains("-F")) //THIS IS THE IF FOR BRINGING IN THE FILE
            {    
                try
                {
                    args[i] = args[i].substring(2);
                    filename = args[i];
                    System.out.println(filename);
                    FileReader rd = new FileReader(filename);
                    StreamTokenizer st = new StreamTokenizer(rd);
                    
                    token = st.nextToken();
                    System.out.println("First token: " + token); //RETURNS -3
                    figureAry = new Comparable[18291];
                    for (int i2 = 0; i2 < figureAry.length; i2++)
                    {
                        while (token != StreamTokenizer.TT_EOF)
                        {
                            switch (token)
                            {
                            case StreamTokenizer.TT_WORD:
                                Character character = st.sval.charAt(0);
                                switch(character)
                                {
                                case 'Y':
                                    System.out.println("Cylinder found");
                                    height = st.nextToken();
                                    radius = st.nextToken();
                                    Cylinder y = new Cylinder(height, compareType, radius);
                                    figureAry[i2] = y;
                                    break;
                                case 'C':
                                    System.out.println("Cone found");
                                    height = st.nextToken();
                                    radius = st.nextToken();
                                    Cone c = new Cone(height, compareType, radius);
                                    figureAry[i2] = c;
                                    break;
                                case 'P':
                                    token = st.nextToken();
                                    System.out.println("Prism Token = " + token); //THIS RETURNS -3 AS WELL
                                    switch (token)
                                    {
                                    case  'S':
                                        System.out.println("Square Prism found");
                                        height = st.nextToken();
                                        edgeLength = st.nextToken();
                                        SquarePrism sp = new SquarePrism(height, compareType, edgeLength);
                                        figureAry[i2] = sp;
                                        break;
                                        
                                    case 'T':
                                        System.out.println("Triangular Prism found");
                                        height = st.nextToken();
                                        edgeLength = st.nextToken();
                                        TrianglePrism tp = new TrianglePrism(height, compareType, edgeLength);
                                        figureAry[i2] = tp;
                                        break;
                                    
                                    case 'P':
                                        System.out.println("Pentagonal Prism found");
                                        height = st.nextToken();
                                        edgeLength = st.nextToken();
                                        PentagonPrism pp = new PentagonPrism(height, compareType, edgeLength);
                                        figureAry[i2] = pp;
                                        break;
                                    
                                    case 'O':
                                        System.out.println("Octagonal Prism found");
                                        height = st.nextToken();
                                        edgeLength = st.nextToken();
                                        OctagonPrism op = new OctagonPrism(height, compareType, edgeLength);
                                        figureAry[i2] = op;
                                        break;
                                    }
                                    break;
                                case 'R':
                                    System.out.println("Pyramid found");
                                    height = st.nextToken();
                                    edgeLength = st.nextToken();
                                    Pyramid r = new Pyramid(height, compareType, edgeLength);
                                    figureAry[i2] = r;
                                    break;
                                }
                            }
                            token = st.nextToken();
                        }
                    }
                }
                catch (IOException e)
                {
                    System.out.println("File not found. Please check file path.");
                }
                    
            }
            /*if (args[i].contains("-s") || args[i].contains("-S"))
            {
                args[i] = args[i].substring(2);
                sortType = args[i].toUpperCase().charAt(1);
                System.out.println(sortType);
                switch (sortType)
                {
                case 'B':
                    Sorting.bubbleSort(figureAry, firstToken);
                    break;
                
                case 'S':
                    Sorting.selectionSort(figureAry, firstToken);
                    break;
                    
                case 'I':
                    Sorting.insertionSort(figureAry);
                    break;
                    
                case 'M':
                    //Sorting.mergeSort(a1, l1, r1, a2, l2, r2, a3, l3);
                    break;
                    
                case 'Q':
                    Sorting.quickSort(figureAry, 0, firstToken);
                    break;
                    
                case 'Z':
                    Sorting.shellSort(figureAry);
                    break;
                
                }
            }*/
            //System.out.println(figureAry);
        }
    }
}

SOME OF THE OUTPUT:
res/polygons1.dat
First token: -3
Prism Token = -3
Cone found
Cone found
Pyramid found
Pyramid found
Prism Token = -3
Prism Token = -3
Pyramid found
Prism Token = -3
Cone found
Pyramid found
Cylinder found
Pyramid found
Cone found
Pyramid found
Cylinder found
Cylinder found
Pyramid found
Prism Token = -3
Cone found
Pyramid found
Cone found
Cylinder found
Prism Token = -3
Cone found
Cylinder found
Prism Token = -3
Prism Token = -3
Cone found
Cylinder found
Cone found
Cylinder found
Cone found
Pyramid found
Cone found


#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Hi,

StreamTokenizer.nextToken() does not return the next character, it returns ttype field, which can have one of this values:
- TT_WORD (-3) indicates that the token is a word.
- TT_NUMBER (-2) indicates that the token is a number.
- TT_EOL (10) indicates that the end of line has been read. The field can only have this value if the eolIsSignificant method has been called with the argument true.
- TT_EOF (-1) indicates that the end of the input stream has been reached.

So you get -3 number. That means that current character is word and you can read it value from st.sval. That can look like this:


if (st.nextToken() == StreamTokenizer.TT_WORD) { // don't hardcode -3, because this number may change in future

    String chr = st.sval; // the real value of current character

}


You can get all information you need to work with StreamTokenizer from official docs: StreamTokenizer (Java 2 Platform SE v1.4.2)

#3
RnAoDm

RnAoDm

    Newbie

  • Members
  • PipPip
  • 13 posts
Wow thank you so much. I am not sure I how i missed this when I was looking at the API. Thanks!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users