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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:41:52+00:00 2026-05-22T19:41:52+00:00

So I have this method that should read a file and detect if the

  • 0

So I have this method that should read a file and detect if the character after the symbol is a number or a word. If it is a number, I want to delete the symbol in front of it, translate the number into binary and replace it in the file. If it is a word, I want to set the characters to number 16 at first, but then, if another word is used, I want to add the 1 to the original number.

Here’s the input that i’m using:

Here’s my method:

    try {
       ReadFile files = new ReadFile(file.getPath());
       String[] anyLines = files.OpenFile();

        int i;


    int wordValue = 16;

// to keep track words that are already used
Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

for (String line : anyLines) {

if (!line.startsWith("@")) {
    continue;
}

line = line.substring(1);

Integer binaryValue = null;

if (line.matches("\\d+")) {
    binaryValue = Integer.parseInt(line);
}
else if (line.matches("\\w+")) {
    binaryValue = wordValueMap.get(line);



    // if the map doesn't contain the word value, then assign and store it
    if (binaryValue == null) {
        binaryValue = wordValue;
        wordValueMap.put(line, binaryValue);
        ++wordValue;
    }
}

   // --> I want to replace with this 
   System.out.println(Integer.toBinaryString(binaryValue));


}


        for (i=0; i<anyLines.length; i++) {

       // --> Here are a bunch of instructions that replace certain strings - they are the lines after @ symbols <--
       // --> I'm not going to list them ... <--

            System.out.println(anyLines[i]);

So the question is, how do I replace those lines that start with (“@” line-by-line), in order?

I basically want the output to look like this:

101
1110110000010000 
10000
1110001100001000 
10001
1110101010001000 
10001
1111000010001000 
10000
1110001110001000 
10010
1110001100000110 
10011
1110101010000111 
10010
1110101010000111 
  • 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-22T19:41:52+00:00Added an answer on May 22, 2026 at 7:41 pm

    I don’t quite understand the logic. If you are simply trying to replace all the @ symbols in order, why not read all the numbers into a List in order, until you see an @ symbol. Then you can start replacing them in order from that List (or Queue since you want first in first out). Does that satisfy your requirements?

    If you must keep the wordValueMap, the code below should loop through the lines after you have populated the wordValueMap and write them to the console. It uses the same logic that you used to populate the map in the first place and outputs the values that should be replaced.

    boolean foundAt = false;
    for (i=0; i<anyLines.length; i++) {
    
      // --> Here are a bunch of instructions that replace certain strings - they are the lines after @ symbols <--
      // --> I'm not going to list them ... <--
    
      if (anyLines[i].startsWith("@")) {
        foundAt = true;
        String theLine = anyLines[i].substring(1);
        Integer theInt = null;
        if (theLine.matches("\\d+")) {
          theInt = Integer.parseInt(theLine);
        }
        else {
          theInt = wordValueMap.get(anyLines[i].substring(1));
        }
    
        if(theInt!=null) {
          System.out.println(Integer.toBinaryString(theInt));
        }
        else {
          //ERROR
        }
      }
      else if(foundAt) {
        System.out.println(anyLines[i]);
      }
    }
    

    When I run this loop, I get the output you were looking for from your question:

    101
    1110110000010000 
    10000
    1110001100001000 
    10001
    1110101010001000 
    10001
    1111000010001000 
    10000
    1110001110001000 
    10010
    1110001100000110 
    10011
    1110101010000111 
    10010
    1110101010000111 
    

    I hope this helps, but take a look at my question above to see if you can do this in a more straight forward manner.

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

Sidebar

Related Questions

No related questions found

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.