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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:57:12+00:00 2026-05-31T01:57:12+00:00

Really struggling to work this out… I have a text file with data like

  • 0

Really struggling to work this out…

I have a text file with data like this (17000 lines of it)

45226   1
45226   1
45226   1
45226   3
45226   5
23470   1
45226   5
45226   5
29610   4
37417   2
37417   3
37948   1

What I want to do is sort the text file (using java) so all the left numbers are grouped if the right value is 1.
or the left value is group if the right is not equal to 1 (so any other number).
for example (but doesn’t have to be like this)

3 x 45226   1
4 x 45226   MIXED
1 x 23470   1
1 x 29610   MIXED
2 x 37417   MIXED
1 x 37948   1

I know I may need to use array? or some sort of sort? but I just can’t work it out :'(

Any help, code or suggestions – greatly appreciated!

Thank you!

  • 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-31T01:57:13+00:00Added an answer on May 31, 2026 at 1:57 am

    My code:

    public class Sort {
    
    
    private static class Counter {
    
        private int one;
        private int mixed;
    
        public void incrementOne() {
            one++;
        }
    
        public void incrementMixed() {
            mixed++;
        }
    }
    
    public static void main(String[] args) throws IOException {
        Map<String, Counter> map = new LinkedHashMap<String, Counter>();
        String fileName = "input.txt";
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
    
        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            StringTokenizer tokenizer = new StringTokenizer(line);
            String key = tokenizer.nextToken();
            String value = tokenizer.nextToken();
            Counter counter = map.get(key);
            if (counter == null) {
                counter = new Counter();
                map.put(key, counter);
            }
            if (value.equals("1")) {
                counter.incrementOne();
            } else {
                counter.incrementMixed();
            }
        }
        reader.close();
        BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
        for(Map.Entry<String, Counter>entry:map.entrySet()){
            Counter counter = entry.getValue();
            if(counter.one>0){
                writer.write(String.valueOf(counter.one));
                writer.write(" x ");
                writer.write(entry.getKey());
                writer.write("   1");
                writer.newLine();
            }
            if(counter.mixed>0){
                writer.write(String.valueOf(counter.mixed));
                writer.write(" x ");
                writer.write(entry.getKey());
                writer.write("   MIXED");
                writer.newLine();
            }
        }
        writer.close();
    
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really struggling to get this to work in xcode 4. I have one
I'm struggling to figure this one out, sessions work when i run my application
Hey everyone. I'm really struggling to figure out the logic with this one and
OK i am really struggling with this! I know had to add an image
For some reason I'm really struggling with this. I'm new to wpf and I
Hey, I'm really struggling with this one. I'am trying to port a small piece
I'm sure this is a piece of cake but I'm really struggling with something
I have been struggling with a problem trying to get PLJava to work on
Hello android developers, I was struggling to get this work but failed and now
I'm really struggling to get an inner shadow to work on an app I'm

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.