开发者

Replacing the output of one method into the output of another method

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 (!lin开发者_JAVA技巧e.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 


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜