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 9 of 9

Thread: help with writing a letters encoding program

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy help with writing a letters encoding program

    hi everyone , i hope you're having a nice day
    so , a friend asked me to do him an encoding/decoding program for his class ...and well,i need help too xD

    you see , the idea of the program is about encoding a set of words inputed by the user (the input keeps showing until the word 'space' is written ) with an idea of conversion every letter in that word to the letter next to it by 3 letters
    (example :zydan is encoded to cbgdq ..note that if the letter is one of the letters "z,y,x" ,it returns to the letters at the first ..z is c and so on ..)

    now,this conversion needs to use string "S" as the entered word,and the encoded to string "Sc" ..and when showing the output,it should be something like S = S + "-" + Sc ; so it would be shown as zydan - cbgdq ....
    so yeah ... can you guys pleeaaaaase help me and try to do it ? ...my english is not the best i know but i really need this program to be done .... i tried so many times but i failed (i'm a beginner) ..i would normally try more but we're running out of time and i need this program as soon as possible ,
    any help would be really appreciated .... i'm so sorry i don't have anything in return but i do hope that one cool handsome programmer would help me out in my problem ,no ?
    thanx in advance ^.^

    also one last thing , their teacher want this one done with the use of only the following : "char , int , String,while,for,return,if,else,systemoutprintln " , in other words the more basic codes ....also they are not allowed to use arrays and charAt() ....though if it's not possible in any other way then use charAt() ....

    after this program is done,can you teach me how to switch it to a decoder that does the oppisite ?
    many thanx to all fellow programmers , i swear i'll do it myself next time but i really need it done this time around ...though i'll learn from this experince to be a better programmer for sure

    p.s : if my thread is in the wrong place i'm really sorry but i'm new here and didn't really got used to the place :p


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: help with writing a letters encoding program

    Quote Originally Posted by Samedge20 View Post
    a friend asked me to do him an encoding/decoding program for his class ..... i know but i really need this program to be done ...
    Wait I thought this was for your friend...

    Quote Originally Posted by Samedge20 View Post
    i tried so many times but i failed
    Post what you have tried, what happens when you run it, the full text of any error messages you may have, an explanation about what you are trying to do, a description of what is happening instead of what should happen, and last but certainly not least, your question about the whole thing. Note that do it for me is not a question. If it was, the answer would be no.


    Quote Originally Posted by Samedge20 View Post
    ..i would normally try more but we're running out of time and i need this program as soon as possible ,
    any help would be really appreciated .... i'm so sorry i don't have anything in return but i do hope that one cool handsome programmer would help me out in my problem ,no ?
    sorry, but no. The sooner you get started the easier the next one gets.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with writing a letters encoding program

    The cipher described is known as "Caesar cipher." You can find many discussions and examples of programming this simple cipher on the web (and maybe here in the Forum).

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with writing a letters encoding program

    Quote Originally Posted by GregBrannon View Post
    The cipher described is known as "Caesar cipher." You can find many discussions and examples of programming this simple cipher on the web (and maybe here in the Forum).
    thank you sooooooooooooo much bro ^.^ , i actually did find the one i'm searching for in here :
    http://www.sccs.swarthmore.edu/users...w14cipher.html
    still , the question is ....what is the java code for it ? i've found many codes but it uses higher level codes (ones we are yet to learn) , do you have one that we could write with the more basic codes (the ones i mentioned in the thread)
    thanx alot anyway

    --- Update ---

    Quote Originally Posted by jps View Post
    Wait I thought this was for your friend...

    Post what you have tried, what happens when you run it, the full text of any error messages you may have, an explanation about what you are trying to do, a description of what is happening instead of what should happen, and last but certainly not least, your question about the whole thing. Note that do it for me is not a question. If it was, the answer would be no.


    sorry, but no. The sooner you get started the easier the next one gets.
    i wrote a lenghty explanation but got deleted by accident ,sorry
    long story short .... i'm in a university ,we have this "programming" subject along side 5 other subjects , my exams are after 4 days and this homework is like 50% of the final mark , i have only 2 days to do it or i'll fail...
    the teacher is so bad that he literally teach us the basics only , no one does know how to solve this homework nor what to do .... i swear i do always solve my problems by myself normally , but this is nearly impossible that i could study new things by myself in less than 2 days and solve this one ...i am planning though on taking heavy classes on programming because it is really not acceptable that i can't write a program by myself ,
    still , if you didn't want to help it's ok with me ... but i really am in a need for help,i thought that i could get help in this forums ...well,thankfully greg brannon did help me with a first step ,probs to you man

    what i tried is below , i couldn't get the program to ask for more inputs before it stops with the word "space" , also the last three letters "z,y,x" seem to appear with different characters instead of "c,b,a" , those are the two main problems i have

    import javax.swing.JOptionPane;
    class gg
    {

    public static void main(String[] args)
    {
    String S;
    char SC;
    int i=0;


    S= JOptionPane.showInputDialog("Input some words:");

    while (i<=S.length()-1)
    {
    SC= S.charAt(i);
    int t = (int)SC;
    if( (t>=97) && (t<=122) )
    {
    t+=3;
    }
    char sc = (char)t;

    i++;
    System.out.print(sc);
    }

    System.out.println("\n"+S);

    }

    }

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with writing a letters encoding program

    You actually used the "It's for a friend," ploy and couldn't keep it together? Pretty funny.

    Java is not JavaScript. Maybe you meant Java source code?

    As for pointing you to Java source code on the web that you could copy and turn in as your own, I wouldn't do that, and I recommend you don't try it. Instructors are pretty skilled at detecting copies and finding the same code, and the consequences aren't pleasant.

    The code you posted is a reasonable start. The problem you've highlighted about 'z' not resulting in 'c' is because the cipher you've programmed doesn't wrap back to the beginning of the alphabet. That can be accomplished by using the modulus operator. For example, if the resulting ciphered character is > 122, using the modulus operator, t %= 123, returns the remainder after dividing 't' by 123.

    It might also be useful to know that a char can often be used as a number or the ascii value. For example, after using the modulus operator as described above, the result will have to be offset to the beginning of the alpha characters, or to 'a' = 97. This can be done using the equation t += 97 or t += 'a'. The purpose of the second equation is clearer. This same principle can be applied to the if statement:

    if( ( t >= 'a' ) && ( t <= 'z' ) )

    I don't know what you mean about reading more than one word from the input. Try typing in more than one word separated by spaces and see what happens.

    Good luck!

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with writing a letters encoding program

    Quote Originally Posted by GregBrannon View Post
    You actually used the "It's for a friend," ploy and couldn't keep it together? Pretty funny.

    Java is not JavaScript. Maybe you meant Java source code?

    As for pointing you to Java source code on the web that you could copy and turn in as your own, I wouldn't do that, and I recommend you don't try it. Instructors are pretty skilled at detecting copies and finding the same code, and the consequences aren't pleasant.

    The code you posted is a reasonable start. The problem you've highlighted about 'z' not resulting in 'c' is because the cipher you've programmed doesn't wrap back to the beginning of the alphabet. That can be accomplished by using the modulus operator. For example, if the resulting ciphered character is > 122, using the modulus operator, t %= 123, returns the remainder after dividing 't' by 123.

    It might also be useful to know that a char can often be used as a number or the ascii value. For example, after using the modulus operator as described above, the result will have to be offset to the beginning of the alpha characters, or to 'a' = 97. This can be done using the equation t += 97 or t += 'a'. The purpose of the second equation is clearer. This same principle can be applied to the if statement:

    if( ( t >= 'a' ) && ( t <= 'z' ) )

    I don't know what you mean about reading more than one word from the input. Try typing in more than one word separated by spaces and see what happens.

    Good luck!
    well yeah , the thing is in my university they don't have the best professors out there , they literally only gave us the basics like... i don't even know what you mean by "modulus operator" ,i really can't describe how bad the teaching was for programming.... they would write us a program and expect us to understand it and do another one by ourselves ..it's about that

    and yeah i meant java source code i've got them mixed up while i was posting the thread (that's why in my first reply i used the word "java code" )..

    i know i've been asking alot but , can you show me what you've explained on my program that i've posted ? like an example and then teach me how to ably it to cover up the three letters "z,x,y " problem
    and about the "read more than one input" , i meant that the program should be continuing to show me the input dialog until i write the word "space" ... so that the final result on the output is like "zydan-cbgdq another word - encoded one a third word - encoded .......etc "
    any idea how can that be achived ?

    i repeat my apology if i'm doing you any troubles or if you quiet possibly are mad at me at this moment ..damn that's embaracing to ask for help in that amount >< sorry ...

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: help with writing a letters encoding program

    Quote Originally Posted by Samedge20 View Post
    well yeah , the thing is in my university they don't have the best professors out there , they literally only gave us the basics like... i don't even know what you mean by "modulus operator"
    Try a search engine. Raw definitions are readily available, usually including sample code showing usage if you pick one of the Oracle links in your search on java keywords. Just a suggestion.

    Quote Originally Posted by Samedge20 View Post
    they would write us a program and expect us to understand it and do another one by ourselves
    That is about par for the course. Welcome to the game. Rewrite that sample over and over. Change values of variables one at a time and run it again. See what changes. Try to understand the program in terms of flow. Step through the program as if you were a computer running the program, and follow execution line for line instead of reading like a book top to bottom. See where decisions are made in the code and follow the correct path, you may need to track the values of variables on paper for this. The professor can show you 1 program or 100 that do the same thing. 99 would have been a waste of time. S/He give you the 1, you write the 99.

    Quote Originally Posted by Samedge20 View Post
    and about the "read more than one input" , i meant that the program should be continuing to show me the input dialog until i write the word "space" ... so that the final result on the output is like "zydan-cbgdq another word - encoded one a third word - encoded .......etc "
    any idea how can that be achived ?
    Look at the loop you set up to read the given String. Set up a loop that will keep asking the user in the same manner, but make this test the word using the String's equals method. (Another keyword you can search)


    Post the modified code after you add the loop if you have any questions. Also please see the Announcements page for the use of code tags when posting code.

    I apologize for pointing out the "for a friend" thing, it was not meant to create a disturbance in the force. You are not the only one who has tried to hide asking for help. I was the same way when I was new. No one was born knowing it and asking questions is the best way to learn. I was my biggest roadblock in learning to program...

  8. #8
    Banned
    Join Date
    Jul 2013
    Posts
    49
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: help with writing a letters encoding program

    hahahahaha......moderators at their best....and seriously i wont even go to such extremes for my girl friend:-)

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with writing a letters encoding program

    so ... mean moderator i'm back ! (just kidding )
    after a LOT of arguments with the instructors they agreed to extend the period another 10 days ... and now (again) i have 3 days left
    so,what i've done so far is this :


    import javax.swing.JOptionPane;
    class encode {
    public static void main(String[] args){
    String S;
    char SC;
    int i=0;

    S= JOptionPane.showInputDialog("Input some words:");

    while (i<=S.length()-1){

    SC= S.charAt(i);


    int t = (int)SC;
    t+=3;

    if( (t>=97) && (t<=122) ){
    char Sc = (char)t;
    i++;
    System.out.print(Sc);
    }

    else{
    t = t-26;
    char Sc=(char)t;
    i++;
    System.out.print(Sc);
    }

    }
    System.out.println("-"+S);

    }

    }


    first off ... it actually works and encode all letters ,good
    but the problem i had is i still can't have a successful loop to stop the input .. my program here only takes one input , when i tried to ably,say, do {} while (c !=32); like this :


    import javax.swing.JOptionPane;
    class encode {
    public static void main(String[] args){
    String S;
    char SC;
    int i=0;
    int c ;
    do {
    S= JOptionPane.showInputDialog("Input some words:");
    c = Integer.parseInt (S);
    } while (c != 32);


    (with 32 being the space) ... it doesn't work and although it says process complete it still shows an error in the execution screen :
    Capture.JPG

    now that the while is misplaced ... i did try to put it in like every line in the code .. same result..i already did encode correctly ,why is this happening to me ?
    any ideas what can i use to have a successful loop that asks the user to enter several inputs until he presses "space" ?
    though i know you wouldn't help me ... but asking could be worth the try
    thanx anyway...

Similar Threads

  1. [SOLVED] Encoding Program
    By geabus1043 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 22nd, 2013, 08:52 PM
  2. A Program that will ask five letters and create a possible words
    By jms25 in forum Java Theory & Questions
    Replies: 11
    Last Post: September 23rd, 2012, 09:40 PM
  3. [SOLVED] How do I make my program accept letters?
    By tyb97 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 30th, 2011, 06:27 PM
  4. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 05:58 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM