Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java program (beginner) help?!

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java program (beginner) help?!

    Hi,

    I have to write a program that decompresses a string in RLE recursively WITHOUT any loops. I've just learned recursion...and I think I understand it mostly (calling a method within itself). But I can't seem to wrap my head around this problem!

    Ex:
    Given a String str = 5w2Kr3qO
    Output: wwwwwKKrqqqO

    (so you output the letter according to the number/count which precedes it; if there is no number preceding the letter, then the letter is printed once)


    I'm really confused about how to approach this problem, any hints would be appreciated.



    This was my initial approach, but it does not work:

    public static String decompress (String str){
     
    if(str.length()==1)
    return str;
     
    String first="";
    String rest="";
    char c = str.charAt(0);
     
    if(Character.isLetter(c) == true){
    first = str.substring(0,1);
    rest = str.substring(1);
    return first + decompress(rest);
     
    }else{
    first = str.substring(0,2);
    rest = str.substring(2);
    int x = str.charAt(0);
    char y = str.charAt(1);
    return repeat(x, y) + decompress(rest); // repeat is a method which will repeat y for x times
    }
     
     
    }
    Last edited by rk2010; April 9th, 2012 at 11:12 PM.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: Java program (beginner) help?!

    What are the results of using that decompress method? Does it throw an exception or does it give an incorrect String?

    It appears that when the decompress method receives a 2-character input String made up of number and character (like "2d"), the "rest" String will end up becoming an empty String. Then that empty String will be placed in the recursive call of the decompress method. It does not appear that your method handles empty Strings. Perhaps this change will work:
    public static String decompress (String str){
     
    if(str == "" || str.length()==1)
    return str;
    .....
    }

    I hope that helps!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java program (beginner) help?!

    Quote Originally Posted by Gigggas View Post
    What are the results of using that decompress method? Does it throw an exception or does it give an incorrect String?

    It appears that when the decompress method receives a 2-character input String made up of number and character (like "2d"), the "rest" String will end up becoming an empty String. Then that empty String will be placed in the recursive call of the decompress method. It does not appear that your method handles empty Strings. Perhaps this change will work:
    public static String decompress (String str){
     
    if(str == "" || str.length()==1)
    return str;
    .....
    }

    I hope that helps!

    Thank you for your suggestion. I understand now that I didn't account for null strings. I'm still getting an error though, and I'm not sure why or how to fix it. I've tried tracing my code with paper, but I can't seem to figure out what's wrong. The error says:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String Index out of range: 0

    at java.lang.String.charAt(String.java:695)
    at rle.decompress(rle.java:20)
    at rle.decompress(rle.java:31)
    at test.main(test.java:8)

    (class with the decompress method)
    line 20: char c = str.charAt(0);
    line 31: return repeat(x, y) + decompress(rest);

    (class that calls/tests decompress method)
    line 8: String answer = rle.decompress(x);

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Java program (beginner) help?!

    Quote Originally Posted by rk2010 View Post
    Thank you for your suggestion. I understand now that I didn't account for null strings. I'm still getting an error though, and I'm not sure why or how to fix it. I've tried tracing my code with paper, but I can't seem to figure out what's wrong. The error says:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String Index out of range: 0

    at java.lang.String.charAt(String.java:695)
    at rle.decompress(rle.java:20)
    at rle.decompress(rle.java:31)
    at test.main(test.java:8)

    (class with the decompress method)
    line 20: char c = str.charAt(0);
    line 31: return repeat(x, y) + decompress(rest);

    (class that calls/tests decompress method)
    line 8: String answer = rle.decompress(x);
    Can you post the repeat(x, y) method?
    if(str == "")
    You shouldn't use the == operator to compare Strings. You should use the String's equals() method.
    if(str.equals(""))

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java program (beginner) help?!

    Sure! Here is the repeat method that I'm using:

    public static String repeat(int i, char c){
         String tst = "";
     
         for(int j = 0; j < i; j++){
         tst = tst+c;
         }
     
         return tst;
    }



    And thanks! I can't believe I missed that. I always forget to use .equals for strings. I fixed the base case in my decompress method to (str.equals("") || str.length()==1). Now I don't have any errors!!! But the problem is that my program is not outputting the correct amount of letters.

    For instance,
    If I input: 2d
    I get: dddddddddddddddddddddddddddddddddddddddddddddddddd

    (I get 51 d's)
    But I should just get: dd
    Last edited by rk2010; April 10th, 2012 at 11:59 AM.

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Java program (beginner) help?!

    I think the problem is in your the parameters you pass when you call the repeat(int i, char c) method. To be more specific:
    int x = str.charAt(0);
    In the abode code x has the value of the char at index 0. You can put a print statement after that piece of code to see the value of x. If i were you, i would take the substring that contains the int and then use the Integer's parseInt() method to take the int from the String input.
    Hope i was clear.

  7. The Following User Says Thank You to andreas90 For This Useful Post:

    rk2010 (April 10th, 2012)

  8. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java program (beginner) help?!

    Thank you so much andreas!!!!
    I used: int x = Integer.parseInt(str.substring(0,1));
    And now my program works perfectly!

  9. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Java program (beginner) help?!

    Quote Originally Posted by rk2010 View Post
    Thank you so much andreas!!!!
    I used: int x = Integer.parseInt(str.substring(0,1));
    And now my program works perfectly!
    You are welcome.
    Glad you got it work.

Similar Threads

  1. Need Help on Looping Program!! Beginner!!
    By PinkFly in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 4th, 2012, 12:05 AM
  2. Beginner needs help with a program
    By MartinC in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 09:51 AM
  3. Need Beginner Calculator Program help!
    By theJastro in forum What's Wrong With My Code?
    Replies: 18
    Last Post: December 17th, 2011, 07:30 PM
  4. Beginner Java Program Help/ txt database search
    By JavaConfused in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 21st, 2011, 09:20 AM
  5. [SOLVED] Java Beginner: Help with methods and returning values (hailstone program)
    By alf in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 14th, 2010, 06:28 PM