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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: How to I possibly compress a java string to say byte space?

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How to I possibly compress a java string to say byte space?

    [/COLOR]
    <I pledge that every program with my name on it shall be written by me
    (and my co-authors, if any) and that I fully understand the program.
    Every program I submit shall be entirely my own work unless otherwise
    attributed.  I understand that academic dishonesty not only includes
    copying other people's work, but also abetting or facilitating
    copying.  Code that is similar to any other submission past or present
    will get no credit whatever the explanation.  I understand that the
    consequence of academic dishonesty is a grade of 'F' for the class. I
    pledge to devote my efforts to learning Java by writing my own
    programs.  I shall strive to be attentive to detail and write programs
    that not only compile and execute correctly, but are easily
    understandable by myself and other programmers.
    >

    This is what I have to print out. It is currently 804 bytes. I made a program here to print this paragraph out:

    <public class pledge1 {
     
    	public static void main(String[] args) {
     
     
    		System.out.println("I pledge that every program with my name on it shall be written by me");
    		System.out.println("(and my co-authors, if any) and that I fully understand the program.");
    		System.out.println("Every program I submit shall be entirely my own work unless otherwise");
    		System.out.println("attributed.  I understand that academic dishonesty not only includes");
    		System.out.println("copying other people's work, but also abetting or facilitating");
    		System.out.println("copying.  Code that is similar to any other submission past or present");
    		System.out.println("will get no credit whatever the explanation.  I understand that the"); 
    		System.out.println("consequence of academic dishonesty is a grade of 'F' for the class. I");
    		System.out.println("pledge to devote my efforts to learning Java by writing my own");
    		System.out.println("programs.  I shall strive to be attentive to detail and write programs");
    		System.out.println("that not only compile and execute correctly, but are easily");
    		System.out.println("understandable by myself and other programmers.");
    	}
    }>

    How, the program is 1191 Bytes, and I have to compress that (presumably) to under 793 bytes. Maybe I don't have to use system.out.println so many times, and I can save byte space. Also, the paragraph is 804 bytes. I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program. How do I go about doing this?

    --- Update ---

    I believe this is a way to compress texts, and I can pass in that compressed text to java to execute. Maybe that will save space? The teacher required that the program cannot take any input. I am assuming he means human input. So if that's the case, I can pass in a compressed text file into java.
    Last edited by redbull; January 14th, 2013 at 07:18 PM. Reason: previous question was confusing


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    I'm not sure I quite understand what you want, do you have a code example?

    As a side note I doubt you'd need to worry about compressing 10 lines of string data.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    I Delete this
    Last edited by redbull; January 14th, 2013 at 07:17 PM. Reason: confusing

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    How about create a byte array the same size as the paragraph and simply put the lower order bytes of the chars into the byte array, and then save it as a non-text file.

    --- Update ---

    Heck, String has a getBytes() method. Have you tried working with this?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,624
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Where is the code that outputs the unicode? Where is the unicode text written? How did you copy and paste here the unicode values shown in your post?

    When I compile and execute the program I get the text not unicode.

    What does this mean:
    When you convert the string to unicode
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    So your program takes a file input which you have to be able to parse, write to an output file (presumably smaller than the original input file), then be able to re-construct the original content and display it?

    A few questions:

    1. Is unicode output support a requirement? If it isn't, you can quickly chop your data size in half by switching to an 8-bit encoding like ASCII.

    2. Does your input file look exactly like what you posted? That file only has ~100 actual characters. You can use various functions in the Character class to parse the string "\u0063" into the actual unicode character.

    3. I doubt you're being asked to implement a fully-fledged compression algorithm. If your professor is looking for use of an actual compression algorithm, take a look at the java.util.zip package. It has implementations of the zip and gzip algorithms which you can call to compress/decompress files.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    I deleted this
    Last edited by redbull; January 14th, 2013 at 07:17 PM. Reason: confusing

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,624
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    unicode was supplied by the teacher
    I am confused about what the program is doing.
    You said:
    Finally, the unicode is:
    \u0063\u006C...

    Is that the contents of a text file that you are supposed to read and convert to a Java String?
    \u0063 to c
    \u006C to l
    etc
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    how do i undelete? if not, I can repost

    --- Update ---

    <I pledge that every program with my name on it shall be written by me
    (and my co-authors, if any) and that I fully understand the program.
    Every program I submit shall be entirely my own work unless otherwise
    attributed.  I understand that academic dishonesty not only includes
    copying other people's work, but also abetting or facilitating
    copying.  Code that is similar to any other submission past or present
    will get no credit whatever the explanation.  I understand that the
    consequence of academic dishonesty is a grade of 'F' for the class. I
    pledge to devote my efforts to learning Java by writing my own
    programs.  I shall strive to be attentive to detail and write programs
    that not only compile and execute correctly, but are easily
    understandable by myself and other programmers.
    >

    This is what I have to print out. It is currently 804 bytes. I made a program here to print this paragraph out:

    <public class pledge1 {
     
    	public static void main(String[] args) {
     
     
    		System.out.println("I pledge that every program with my name on it shall be written by me");
    		System.out.println("(and my co-authors, if any) and that I fully understand the program.");
    		System.out.println("Every program I submit shall be entirely my own work unless otherwise");
    		System.out.println("attributed.  I understand that academic dishonesty not only includes");
    		System.out.println("copying other people's work, but also abetting or facilitating");
    		System.out.println("copying.  Code that is similar to any other submission past or present");
    		System.out.println("will get no credit whatever the explanation.  I understand that the"); 
    		System.out.println("consequence of academic dishonesty is a grade of 'F' for the class. I");
    		System.out.println("pledge to devote my efforts to learning Java by writing my own");
    		System.out.println("programs.  I shall strive to be attentive to detail and write programs");
    		System.out.println("that not only compile and execute correctly, but are easily");
    		System.out.println("understandable by myself and other programmers.");
    	}
    }>

    How, the program is 1191 Bytes, and I have to compress that (presumably) to under 793 bytes. Maybe I don't have to use system.out.println so many times, and I can save byte space. Also, the paragraph is 804 bytes. I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program. How do I go about doing this?

    --- Update ---

    I believe this is a way to compress texts, and I can pass in that compressed text to java to execute. Maybe that will save space? The teacher required that the program cannot take any input. I am assuming he means human input. So if that's the case, I can pass in a compressed text file into java.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,624
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    This is what I have to print out
    You could put all of the text into a single String. Use the line separator character in the String at the places where you want a new line to begin.

    I have to make the paragraph no more than 655 bytes, with a total of 793 bytes for the whole entire program.
    So the total text of the program outside of the paragraph has to be 138 bytes?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Quote Originally Posted by Norm View Post
    You could put all of the text into a single String. Use the line separator character in the String at the places where you want a new line to begin.


    So the total text of the program outside of the paragraph has to be 138 bytes?
    This is what the teacher said:
    < The total number of characters in the program should be fewer that the total number of characters in the pledge (655 printable, 793 total).  Your programs file size should be under 793 bytes>

    And as for putting the text together in a single string, they want no more than 100 characters/words across a single line, as per proper programming style. So even if I use \n, it looks like this:

    <public class pledge1 {
     
    	public static void main(String[] args) {
     
     
    		System.out.println("I pledge that every program with my name on it shall be written by me \n(and my co-authors, if any) and that I fully understand the program.  \nEvery program I submit shall be entirely my own work unless otherwise \nattributed.   I understand that academic dishonesty not only includes \ncopying other people's work, but also abetting or facilitating \ncopying.   Code that is similar to any other submission past or present \nwill get no credit whatever the explanation.   I understand that the \nconsequence of academic dishonesty is a grade of 'F' for the class.  I \npledge to devote my efforts to learning Java by writing my own \nprograms.   I shall strive to be attentive to detail and write programs \nthat not only compile and execute correctly, but are easily \nunderstandable by myself and other programmers.");
     
    	}
    }
     
    >


    --- Update ---

    The previous program I pasted, I stand at 934 bytes. So I have to cut out about 150 bytes

    --- Update ---

    You know what, screw style, I just want this under 793. Also, in the "public static void main (String [] args)", I just changed the "args" to "a", saving 3 bytes

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,624
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Seems a big waste of time. What is supposed to be learned in this exercise?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    ok I just added the "+" at the end of end line, to make it look more readable. But it adds a "+", which are extra characters.

    --- Update ---

    Quote Originally Posted by Norm View Post
    Seems a big waste of time. What is supposed to be learned in this exercise?
    please, this is not the time to be asking these questions, my time deadline is coming up.

    --- Update ---

    I need to compress this string now, to save me some space.

    --- Update ---

    Quote Originally Posted by Norm View Post
    Seems a big waste of time. What is supposed to be learned in this exercise?
    IDK, how to use oracles java docs, or java api. Or how to learn compression by yourself?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,624
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    How are you learning compression by retyping the lines of a program?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    ok, I have another idea. I'm going to use an external program to convert string to ascii. Then I am going to copy and paste that into a java program, and compress that ascii code. Will that work? But java will have to read that ascii code. Hopefully there is a java class for that.

  16. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    Norm is commenting on the assignment itself, not your questions. I agree that this looks like a pointless exercise.

    Are you counting bytes of source code, or bytes of the compiled program?

    I just compiled a pretty much as bare-bones program I that will work by reading an uncompressed text file in and the printing it out and that's an 1008 byte binary.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,624
    Thanks
    45
    Thanked 1,958 Times in 1,932 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    changed the "args" to "a", saving 3 bytes
    Putting all the code on one line will save the lineend characters for each line.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Quote Originally Posted by helloworld922 View Post
    Norm is commenting on the assignment itself, not your questions. I agree that this looks like a pointless exercise.

    Are you counting bytes of source code, or bytes of the compiled program?

    I just compiled a pretty much as bare-bones program I that will work by reading an uncompressed text file in and the printing it out and that's an 1008 byte binary.

    I am counting bytes of the java file. Like suppose I save it to the desktop, I'll right click it, check the properties, and check the size there.

    I cannot pass in any inputs for this. Here's what I was thinking. Convert this string to ascii, and compress that ascii code (because there may be repeating characters). Then place this compressed ascii code in a java program, and somehow find a class to uncompress it??? That way, this should not take up too much space.

    The paragraph is 804 bytes alone. If the program has to be less than 793 bytes, it's obvious that I will not be able to type this paragraph in this java program. So I will have to type in a compress ascii code (unicode will take up more space) in this program, and just uncompress and execute that code

    --- Update ---

    sh*t, i think the ascii codes for this paragraph will be more than the byte size of the entire text

    --- Update ---

    for example, "abc" is like "979899"

    --- Update ---

    Quote Originally Posted by Norm View Post
    Putting all the code on one line will save the lineend characters for each line.
    I will do that

    --- Update ---

    String to hex?

  19. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    That's rather silly to count the bytes of the source file (that's the .java file, the .class file is the compiled binary). It encourages bad programming practices like single letter variable names, no tabbing, no comments, and no line separators. Even taking all of these things out the source file is 860 characters long.

    I cannot pass in any inputs for this. Here's what I was thinking. Convert this string to ascii, and compress that ascii code (because there may be repeating characters). Then place this compressed ascii code in a java program, and somehow find a class to uncompress it??? That way, this should not take up too much space.
    I wouldn't bother, Java source files are usually UTF-8 encoded, meaning if you only use characters in the lower 128 ASCII charset you'll get the exact same file either way (that's the case here).

    The only way I can think of to get your source file smaller is to use an external file, then your java program opens that file and prints it out.

  20. #20
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    that's what I will have to do. I am going to use an external program to get the hex codes for this string, compress that string in hex codes, then bring these hex codes into my program. The compressed hex codes should be less than the paragraph (i am hoping). Then decompress those hex codes and print it???

  21. #21
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    When is your assignment due? I would recommend asking a member of the teaching staff if you're able to. These seem like rather silly requirements, and they might shed some light on what path you're expected to take.

    Having an externally compressed text file won't help much because then you'll need to have Java code which will decompress it. If you do end up compressing it, leave the text "as-is" so you don't have to do an extra step converting hex to a character code. The best solution I can think of is reading from an uncompressed text file and printing it out.

  22. #22
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    Quote Originally Posted by helloworld922 View Post
    When is your assignment due? I would recommend asking a member of the teaching staff if you're able to. These seem like rather silly requirements, and they might shed some light on what path you're expected to take.

    Having an externally compressed text file won't help much because then you'll need to have Java code which will decompress it. If you do end up compressing it, leave the text "as-is" so you don't have to do an extra step converting hex to a character code. The best solution I can think of is reading from an uncompressed text file and printing it out.
    how do you do that? He did hint on changing the extension, possibly to a text

    --- Update ---

    No, the program can't take any input. I heard someone else mention something about converting this text into an image.

  23. #23
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to I possibly compress a java string to say byte space?

    There's no way to directly store an image in a java source file, besides you're storing the same data either way. The character 'A' has a value of 65, which is 1 byte regardless of if it's interpreted as a pixel or as an UTF-8 character.

  24. #24
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    i am totally lost...

    --- Update ---

    ok, maybe it involves javap -c <class>....

  25. #25
    Member
    Join Date
    Sep 2012
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to I possibly compress a java string to say byte space?

    OK, the assignment is past due and was extra credit. How do I do this? Reduce my program from 930 bytes to under 793 bytes. And most of my program is java string. Last, I cannot import any files into my program. Supposedly its a very complicated problem.

Page 1 of 2 12 LastLast

Similar Threads

  1. how can i compress a video file for example an avi file in java?
    By vigneshwaran in forum Java Theory & Questions
    Replies: 1
    Last Post: January 15th, 2013, 01:39 AM
  2. [SOLVED] [Asking] Convert String to Byte Array
    By ardisamudra in forum Java Theory & Questions
    Replies: 8
    Last Post: October 30th, 2012, 09:58 AM
  3. Compress my Code!
    By aStudentofJava in forum Loops & Control Statements
    Replies: 23
    Last Post: March 4th, 2012, 05:14 AM
  4. Java Program Loop Possibly needed
    By mikec2500 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 25th, 2011, 12:35 AM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 06:06 AM

Tags for this Thread