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

  • Home
  • SEARCH
  • 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 8262375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:44:17+00:00 2026-06-08T03:44:17+00:00

Hey StackOverflow Community, So, I have this line of information from a txt file

  • 0

Hey StackOverflow Community,

So, I have this line of information from a txt file that I need to parse.

Here is an example lines:

-> date & time AC Power Insolation Temperature Wind Speed
-> mm/dd/yyyy hh:mm.ss kw W/m^2 deg F mph

Using a scanner.nextLine() gives me a String with a whole line in it, and then I pass this off into StringTokenizer, which then separates them into individual Strings using whitespace as a separator.

so for the first line it would break up into:
date
&
time
AC
Power
Insolation
etc…

I need things like “date & time” together, and “AC Power” together. Is there anyway I can specify this using a method already defined in StringTokenizer or Scanner? Or would I have to develop my own algorithm to do this?

Would you guys suggest I use some other form of parsing lines instead of Scanner? Or, is Scanner sufficient enough for my needs?

ejay

  • 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-06-08T03:44:19+00:00Added an answer on June 8, 2026 at 3:44 am

    oh, this one was tricky, maybe you could build up some Trie structure with your tokens, i was bored and wrote a little class which solves your problem. Warning: it’s a bit hacky, but was fun to implement.

    The Trie class:

    class Trie extends HashMap<String, Trie> {
    
        private static final long serialVersionUID = 1L;
        boolean end = false;
    
        public void addToken(String strings) {
            addToken(strings.split("\\s+"), 0);
        }
    
        private void addToken(String[] strings, int begin) {
            if (begin == strings.length) {
                end = true;
                return;
            }
    
            String key = strings[begin];
            Trie t = get(key);
            if (t == null) {
                t = new Trie();
                put(key, t);
            }
            t.addToken(strings, begin + 1);
    
        }
    
        public List<String> tokenize(String data) {
            String[] split = data.split("\\s+");
            List<String> tokens = new ArrayList<String>();
            int pos = 0;
            while (pos < split.length) {
                int tokenLength = getToken(split, pos, 0);
                tokens.add(glue(split, pos, tokenLength));
                pos += tokenLength;
            }
    
            return tokens;
        }
    
        public String glue(String[] parts, int pos, int length) {
            StringBuilder sb = new StringBuilder();
            sb.append(parts[pos]);
            for (int i = pos + 1; i < pos + length; i++) {
                sb.append(" ");
                sb.append(parts[i]);
            }
            return sb.toString();
        }
    
        private int getToken(String[] tokens, int begin, int length) {
            if (end) {
                return length;
            }
            if (begin == tokens.length) {
                return 1;
            }
    
            String key = tokens[begin];
            Trie t = get(key);
            if (t != null) {
                return t.getToken(tokens, begin + 1, length + 1);
            }
            return 1;
        }
    }
    

    and how to use it:

    Trie t = new Trie();
    t.addToken("AC Power");
    t.addToken("date & time");
    t.addToken("date & foo");
    t.addToken("Speed & fun");
    
    String data = "date & time AC Power Insolation Temperature Wind Speed";
    
    List<String> tokens = t.tokenize(data);
    for (String s : tokens) {
        System.out.println(s);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey stackoverflow - This is my first question here. I have 2 tables in
Hey StackOverflow community, My question is as follows: I have a table, say USER_ADDR
I have this ajax_update script that updates file.php every 60 seconds.. Now file.php outputs
hey guys having this really simple problem but cant seem to figure out have
hey guys here at stackoverflow.com I'm a student and developing a small todo app
Possible Duplicate: multi image upload wrong quantity on file-upload Hey so I have a
Hello StackOverflow Community, <plist> <dict> <key>Non Random Key</key> <dict> <key>Random Key</key> <dict> <key>Hello</key> <string>Hey</string>
I was just looking at this stackoverflow question: Display web browser settings And that
There's a feature in Twitter called hashtags that extracts tags from messages. For example,
Hey StackOverflow friends, I have a situation where I would like to be able

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.