Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:35:54+00:00 2026-05-31T13:35:54+00:00

If I have a file with some structure to it: type 2 0 0

  • 0

If I have a file with some structure to it:

type
2
0 0 name
100 100 name
1
1 2 name name

How can I use a StreamTokenizer to process this file? Is the only way the procedural approach?

i.e.

StreamTokenizer st = new StreamTokenizer(new FileReader(filename));

if (st.nextToken() != StreamTokenizer.TT_EOF) {
    st.nextToken();
    if (st.sval == "typea") {
        st.nextToken();

        int i = (int) st.nval;
        if (i > 0) {
            while (i > 0) {
                // process node sets
            }
        }
    } else if (st.sval == "typeb") {
        st.nextToken();

        int i = (int) st.nval;
        if (i > 0) {
            while (i > 0) {
                // process node sets
            }
        }
    }
}

It’s fine to skip if unexpected tokens exist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:35:55+00:00Added an answer on May 31, 2026 at 1:35 pm

    You will probably need to do it in a procedural fashion. Consider breaking things into separate functions and gathering your node sets as you go along. Here’s something simple that parses your given example. You can build on it as things get more complicated. I’m sure someone else can offer you something better.

    import java.util.Formatter;
    import java.util.Collections;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.StreamTokenizer;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import static java.io.StreamTokenizer.*;
    
    
    public class NodeToker {
    
       public static void main(String[] args) throws IOException  {
          final String fname = "/data/types.txt";
          InputStream stream = Class.class.getResourceAsStream(fname);
    
          if (stream == null) {
             System.err.println("Could not open stream " + fname);
          } else {
             NodeToker typeToker = new NodeToker();
             AllTypes allTypes = typeToker.parse(stream);
    
             try { stream.close(); }
             catch (IOException ex) { }
    
             Formatter fmtr = new Formatter(System.out);
    
             int groupCount = 0;
             for (NodeSet nodeSet : allTypes) {
                groupCount++;
                int nodeCount = 0;
                for (Node node : nodeSet) {
                   fmtr.format("Set %s, Node %s: %s%n",
                           groupCount, ++nodeCount, node);
                }
             }
          }
       }
    
       public AllTypes parse(InputStream stream) throws IOException {
          Reader reader = new BufferedReader(new InputStreamReader(stream));
    
          StreamTokenizer tok = new StreamTokenizer(reader);
          setupToker(tok);
    
          AllTypes allTypes = new AllTypes();
    
          while (tok.nextToken() != TT_EOF) {
             String text = tok.sval == null ? "" : tok.sval;
    
             if (text.startsWith("type")) {
                // slurp newline
                tok.nextToken();
                handleNodeSet(tok, allTypes);
             }
          }
    
          return allTypes;
       }
    
       private void handleNodeSet(
               StreamTokenizer tok,
               AllTypes allTypes) throws IOException {
          while (tok.nextToken() != TT_EOF) {
             if (tok.ttype == TT_NUMBER) {
                final int numRows = (int) tok.nval;
                tok.nextToken();
    
                NodeSet nodeSet = allTypes.newNodeSet();
    
                for (int i = 0; i < numRows; i++) {
                   Node node = nodeSet.newNode();
                   handleNode(tok, node);
                }
             } else {
                // maybe beginning of next type?
                tok.pushBack();
                break;
             }
          }
       }
    
       private void handleNode(StreamTokenizer tok,
               Node node) throws IOException {
          while (!endOfRow(tok)) {
             if (tok.ttype == TT_NUMBER) {
                node.addNumber((int)tok.nval);
             } else if (tok.ttype == TT_WORD) {
                node.addName(tok.sval);
             }
          }
       }
    
       private void setupToker(StreamTokenizer tok) {
          tok.eolIsSignificant(true);
       }
    
       private boolean endOfRow(StreamTokenizer tok) throws IOException {
          return (tok.nextToken() == TT_EOL) || (tok.ttype == TT_EOF);
       }
    
       public static class AllTypes implements Iterable<NodeSet> {
          private List<NodeSet> typesList = new ArrayList<NodeSet>();
    
          public NodeSet newNodeSet() {
             NodeSet typeGroup = new NodeSet();
             typesList.add(typeGroup);
             return typeGroup;
          }
    
          public Iterator<NodeSet> iterator() {
             return Collections.unmodifiableCollection(typesList).iterator();
          }
    
       }
    
       public static class NodeSet implements Iterable<Node> {
          private List<Node> nodeList = new ArrayList<Node>();
    
          public Node newNode() {
             Node node = new Node();
             nodeList.add(node);
             return node;
          }
    
          public Iterator<Node> iterator() {
             return Collections.unmodifiableCollection(nodeList).iterator();
          }
    
       }
    
       public static class Node {
          private List<Integer> numbers = new ArrayList<Integer>();
          private List<String> names = new ArrayList<String>();
    
          public void addName(String name) {
             names.add(name);
          }
    
          public void addNumber(int number) {
             numbers.add(number);
          }
    
          @Override
          public String toString() {
             return "Node{" + "numbers=" + numbers + " names=" + names + '}';
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this file structure: folderIWantStuffIn/ - old_stuff Now I want to add some
I have text file with some text information and i need to split this
I have one file (for example: test.txt), this file contains some lines and for
I have this exported file of some weird (standard for this industry!) format, which
I have a .txt file having some text. I want to read this file
i have form action file in another directory but some file send to this
I have a pipe delimited file with some NULL date fields. I load this
I have a XML file template like this that comes from some IC chips,
i have some issue to navigate on my XML File. Here is the structure
MYSQL table structure: id , name , status , color how can i use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.