user3044112
user3044112

Reputation: 11

basic java program not working

For the code i need to write a method the decompresses a string. For example if the user entered "2d5t" the method would return "ddttttt". My code now will work for that input but if the input uses a character without a number before it the program wont work when it should. For example if the input was just "d" the program wouldnt work instead of just returning "d". The code also has to be recursive. Here is what my code is now please help.

public static String decompress(String compressedText) {
    if (compressedText.equals(""))
        return "";
    return decompress(compressedText, charInt(compressedText, 0), 0);
}

public static String decompress(String text, int count, int pos) {
    if (pos == text.length() || (pos == text.length()-2 && count == 0))
        return "";
    else if (count == 0)
        return decompress(text, charInt(text, pos+2), pos+2);
    return text.charAt(pos+1) + decompress(text, count-1, pos);
}

public static int charInt(String str, int idex) {
    return str.charAt(idex) - '0';
}

Upvotes: 1

Views: 126

Answers (4)

Jules Gagnon-Marchand
Jules Gagnon-Marchand

Reputation: 3801

**Edited to be recursive as per the OP's request

Tested left first one char lookahead parser using regex

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Parser{

private static String parse(StringBuilder output, String input, Integer offset){
    if(offset<input.length()){
        java.util.regex.Pattern p0 = 
            java.util.regex.Pattern.compile("\\d(?=[a-z])");

        java.util.regex.Pattern p1 = 
            java.util.regex.Pattern.compile("[a-z]");

        java.util.regex.Matcher m0 = p0.matcher(input);
        java.util.regex.Matcher m1 = p1.matcher(input);
        if (m0.find(offset) && m0.start() == offset) 
        {
            for(Integer i = 0;
                i < Integer.parseInt(String.valueOf(input.charAt(offset))); 
                ++i) {
               output.append(input.charAt(offset+1));
            }
            offset+=2;
        }  
        else if (m1.find(offset) && m1.start() == offset) {
            output.append(input.charAt(offset));
            ++offset;
        }
        else {
            ++offset;
        }
        return parse(output, input, offset);
    }
    else return output.toString();
}

public static void main(String[] args) 
{
    Integer offset = 0;
    StringBuilder output = new StringBuilder();
    parse(output, args[0], offset);
    System.out.println(output.toString());    
}
} 

Upvotes: 0

Nathan Merrill
Nathan Merrill

Reputation: 8396

Here's some pseudocode:

function createString(int times, char character){
    if times is 0, do nothing
    otherwise return character + createString(times-1, character);
}

function createString(string full){
    split string by number/character pairs
    for each pair, call createString(times, character), and append them
}

I don't believe in handing out real code, sorry. It's much better in the long run.

Upvotes: 1

cosjav
cosjav

Reputation: 2115

As mentioned by others, this can be solved with regular expressions. An example solution is:

  public static String decompress(String compressed) {
    Matcher matcher = Pattern.compile("(\\d+)([^\\d])").matcher(compressed);
    StringBuffer decompressed = new StringBuffer();
    while (matcher.find()) {
      Integer charNum = Integer.parseInt(matcher.group(1));
      StringBuilder decompressedChars = new StringBuilder();
      for (int i = 1; i <= charNum; i++) {
        decompressedChars.append(matcher.group(2));
      }
      matcher.appendReplacement(decompressed, decompressedChars.toString());
    }
    matcher.appendTail(decompressed);
    return decompressed.toString();
  }

This code won't support numbers larger than Integer.MAX_VALUE and you might want to put some error handling and validation in there also.

Upvotes: 0

Muhammad Maqsoodur Rehman
Muhammad Maqsoodur Rehman

Reputation: 34287

You need to validate your user input. Decide first, that what string values are acceptable to your method and then write a validate method. Then invoke that method inside your decompress method. Look into string manipulation functions and regular expressions in Java. And then try rewriting your code.

Upvotes: 0

Related Questions