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 400163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:55:02+00:00 2026-05-12T16:55:02+00:00

I have a text file which looks like: name1 1 0 1 0 1

  • 0

I have a text file which looks like:

name1
1 0 1 0 1
0 1 1 1 0
0 0 0 0 0
name2
1 0 1 0 1
0 0 1 1 0
0 0 0 0 1

i.e., a plaintext label followed by a few rows with 1/0 separated by spaces. The number of rows of 1/0 is variable, but each row between any two particular labels should have the same number of 1/0s (though might potentially not).

How do I grab each name+rows chunk with a scanner? Is there any elegant way to enforce the consistency on the number of rows (and provide some sort of feedback if they aren’t consistent)?

I’m thinking there might be a convenient way with clever delimiter specification, but I can’t seem to get that working.

  • 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-12T16:55:03+00:00Added an answer on May 12, 2026 at 4:55 pm

    Even better, after a helpful answer to another question (thanks Bart):

    static final String labelRegex="^\\s*\\w+$";
    static final Pattern labelPattern = Pattern.compile(labelRegex, Pattern.MULTILINE);
    Matcher labelMatcher = labelPattern.matcher("");
    
    static final String stateRegex = "([10] )+[10]\\s+";
    static final String statesRegex = "("+stateRegex+")+";
    static final Pattern statesPattern = Pattern.compile(statesRegex, Pattern.MULTILINE);
    Matcher stateMatcher = statesPattern.matcher("");
    
    static final String chunkRegex = "(?="+labelRegex+")";
    static final Pattern chunkPattern = Pattern.compile(chunkRegex,Pattern.MULTILINE);
    Scanner chunkScan;
    
    public void setSource(File source) {
        if(source!=null && source.canRead()) {
         try {
          chunkScan = new Scanner(new BufferedReader(new FileReader(source)));
          chunkScan.useDelimiter(chunkPattern);
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
    }
    
    public Map<String, List<GraphState>> next(int n) {
     Map<String,List<GraphState>> result = new LinkedHashMap<String,List<GraphState>>(n);
      String chunk, rows;
      int i=0;
      while (chunkScan.hasNext()&&i++<n) {
        chunk = chunkScan.next().trim();
        labelMatcher.reset(chunk);
        stateMatcher.reset(chunk);
       if (labelMatcher.find()&&stateMatcher.find()) {
        rows = stateMatcher.group().replace(" ", "");
        result.put(labelMatcher.group(), rowsToList(rows.split("\\n")));
       }
      }
      return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file, which looks like this: node13 state = free np
I have a text file which has a particular line something like sometext sometext
I have a text file which contains something like this: Hello, my name is
I have a text file (tab separated) which consist of 17 column. I would
So I have a a text file that looks something like this: public >ret
I have a file universities.txt which looks like this: Alabama Air University Alabama A&M
I have XML file which is looks like: <Report> <Total> <RecordValues> <Record> <FieldValue fieldName=index
Say I have an XML file which looks like this: <?xml version=1.0 encoding=UTF-8?> <Project
I need help in parsing a very long text file which looks like: NAME
I have an xml file which looks like this: <section> <title> title of section

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.