Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


java help

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Daniel.
I post too much
Reputation: 72

Joined: 08 Nov 2007
Posts: 2938

PostPosted: Wed Oct 30, 2013 1:39 pm    Post subject: java help Reply with quote

Write your code in the file Compress.java. Write your test cases in assign4-testcases.txt.

Run-length encoding (RLE) is a simple "compression algorithm" (an algorithm which takes a block of data and reduces its size, producing a block that contains the same information in less space). It works by replacing repetitive sequences of identical data items with short "tokens" that represent entire sequences. Applying RLE to a string involves finding sequences in the string where the same character repeats. Each such sequence should be replaced by a "token" consisting of:

1. the number of characters in the sequence
2. the repeating character

Code:

public class Compress {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
      String letters = IO.readString();
      //IO.outputIntAnswer(letters.length());
      int i = 0;
      for ( int count = 0; count < letters.length(); count++) {
      
      
      while (i+1 < letters.length() && letters.charAt(i) == letters.charAt(i + 1))
      {
      i++;
      count++;
      
      }
      System.out.print(count+1 + "" + letters.charAt(count));
      }

   }

}


input: qqww
expected output: 2q2w
actual output: 2q4w

can anyone help me with my problem?

this is an example of what it's suppose to do

For example, consider the following string:

qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT
After applying the RLE algorithm, this string is converted into:

q9w5e2rt5y4qw2Er3T

_________________
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25824
Location: The netherlands

PostPosted: Wed Oct 30, 2013 4:53 pm    Post subject: Reply with quote

I always suck at the order of mathematical expressions so I ALWAYS use more braces than actually needed. (Makes it easier to read)

I think this might be one of those cases where that can help
Code:

(i+1 < letters.length() && letters.charAt(i) == letters.charAt(i + 1))


If I remember correctly && goes before almost everything, so this would turn into:
Code:

(i+1 < (letters.length() && letters.charAt(i)) == letters.charAt(i + 1))


If that's not what you wanted (I doubt it) try this:
Code:

 while ((i+1 < letters.length()) && (letters.charAt(i) == letters.charAt(i + 1)))


Also,
You need to reset Count to 0 after
Code:

System.out.print(count+1 + "" + letters.charAt(count));

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 893

PostPosted: Wed Oct 30, 2013 7:38 pm    Post subject: Reply with quote

Dark Byte wrote:
You need to reset Count to 0


Hehe. He can't, 'cause he's using it to iterate over the loop. I think the operator precedence is OK, he's just got some logic issues going on (using "count" when he means to be using i).

My best suggestion to the OP is to either get a pen and paper and manually trace the function for some iterations or start writing code in a proper IDE and make use of the debugging function. Being able to step through your code one line at a time and watch what the variables hold as the code executes might save you a lot of time and headaches while learning.
Back to top
View user's profile Send private message
DELETED_USER
Newbie cheater
Reputation: 1

Joined: 02 Oct 2013
Posts: 24
Location: Venezuela

PostPosted: Wed Oct 30, 2013 11:29 pm    Post subject: Reply with quote

I don't know the Java's syntax, however, I made it in C:

Code:
#include <stdio.h>
#include <string.h>

int main(void)
{
    unsigned int i = 0, j = 0, k = 0;
    char letters[100];

    printf("Please write the string: ");
    fgets(letters, 100, stdin);

    for (i = 0; i < strlen(letters)-1; i++)
    {
        for (j = 0; j < strlen(letters)-1; j++)
        {
            if (letters[i] == letters[i+j])
                k++;
            else
                break;
        }

        if (k > 1)
        {
            printf("%d%c", k, letters[i]);
            i = i + k - 1;
        }
        else
            putchar(letters[i]);

        k = 0;
    }

    getchar();

    return 0;
}


Input:

Quote:
qwwwwwwwwweeeeerrtyyyyyqqqqwEErTTT


Output:

Quote:
q9w5e2rt5y4qw2Er3T


Well... I hope you just don't convert this code into the Java's syntax without knowing that you're really doing.

I hope you read the code, and understand how it works, then write your own version.

Regards.
Back to top
View user's profile Send private message Send e-mail
Daniel.
I post too much
Reputation: 72

Joined: 08 Nov 2007
Posts: 2938

PostPosted: Thu Oct 31, 2013 11:05 am    Post subject: Reply with quote

My main problem is I don't know how to reset counter, it's what I've been trying to do
_________________
Back to top
View user's profile Send private message
DELETED_USER
Newbie cheater
Reputation: 1

Joined: 02 Oct 2013
Posts: 24
Location: Venezuela

PostPosted: Thu Oct 31, 2013 6:48 pm    Post subject: This post has 1 review(s) Reply with quote

Daniel. wrote:
My main problem is I don't know how to reset counter, it's what I've been trying to do


Are you serious?

Look at my snippet, you can see it right there.
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites