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

Thread: Need help writing a Java program

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help writing a Java program

    Hi,

    I need to write a program that will encipher a message that is entered into my program.
    How it will work is, once the user opens the program they will be prompted to enter the passphrase in (secret word).
    If they enter it correctly it then prompts them to enter a message they would like to encipher. If it is wrong then an error message will appear. For the purposes of this the code word will be donkey

    How the cipher will work is that all the letters of the normal alphabet will be converted into the cipher text alphabet
    so
    abcdefghijklmnopqrstuvwxyz will become
    donkeyabcfghijlmpqrstuvwxz

    So 'a' will be replaced with 'd', 'b' with 'o', 'd' with 'k' etc etc.

    My problem is I do not know how to write this program or what I need to use to convert all the letters and then show the new ciphered message on the screen

    Can someone out there please let me know how the code will work and what I need to use. This shouldnt take too long and I am stuck.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Need help writing a Java program

    Do you have any code so far (if so, please post it)?

    For getting input and displaying simple messages, I suggest using JOptionPane. Specifically, use JOptionPane.showInputDialog() for input and JOptionPane.showMessageDialog() for display messages.

    For replacing characters in a String with other characters, I'd look at the <String>.replace().
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help writing a Java program

    Sorry the program is on my home computer, so will post what I have later on.

    However I have managed to write the code so far (using JOptionPane) that gets the user to enter the passphrase (donkey). The program then prompts the user to enter in a message they would like to be encrypted using the cipher alphabet. The message can be however long, and can contain full stops, numbers etc, as only the letters will be replaced.

    Once the message is entered, the program is to use a loop/array that will detect how long the string is (entire message) and then go through and convert each letter at each index of the String, based on the cipher alphabet. I tried using the string.replace method but the problem I had is for example once I replaced all the 'a's with 'd's then when I go to replace the 'd's with 'k's it will replace all the 'd's that were converted from 'a'.

    I hope that made sense.

    Anyway I am baffled at the moment on what to do. I will submit what I have done later on.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Need help writing a Java program

    How would you do this by hand, with a piece of paper and a pencil and no computer?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Need help writing a Java program

    I feel like you are making the problem more complex than it is. With the cipher, you are not "replacing" letters in the alphabet, but rather "rearranging" them. Trying to use the String.replace(...) method will make this more complex, because it's not necessarily the "best" way of doing this.

    Like Kevin suggested, try to explain how you would do this by hand, and then try to replicate that as closely as possible (step-by-step) in your code.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    My Mood
    Torn
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help writing a Java program

    Dude you just in luck, i made one of this for my cipher.

    ok you need to understand, my explanations and self reasoning are all messed up. I do stuff that would confuse people...
    So i had a project for school last year which, i had to make my own cipher.
    what you want, is exactly what i got.
    This is the full code: Scroll further down for the code you will need

    (Spoonfeeding removed by moderator)
    Last edited by KevinWorkman; April 3rd, 2012 at 06:57 AM. Reason: Spoonfeeding is not helping!

  7. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Need help writing a Java program

    @Ajan

    Posting exact answers, particularly code, is discouraged. The goal of these forums is to help one reach a goal, not reach it for him/her. For a more in-depth reasoning, please read this.

    @aussiemcgr

    You can complete this without using <String>.replace()? I'm very curious now. Could you explain more (if it would give away the code, you can personal message me)?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    21
    My Mood
    Torn
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Need help writing a Java program

    @snow will i get banned now? i never knew that you couldnt do it for the people who asked. ill keep this in mind for the next question, soz

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: Need help writing a Java program

    Sure Snowguy. I just wrote it up and sent it to you.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,181
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Need help writing a Java program

    Quote Originally Posted by Ajan View Post
    @snow will i get banned now? i never knew that you couldnt do it for the people who asked. ill keep this in mind for the next question, soz
    You aren't going to be automatically banned, especially because you're being cool about it (instead of the usual reaction when people find out that handing out code is bad). But just like you probably wouldn't write a term paper for somebody, you probably shouldn't give away full code solutions like that. It's awesome that you want to help, but a better way is to guide the poster with questions that help them think through the problem instead of just dropping the solution in their laps.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. The Following User Says Thank You to KevinWorkman For This Useful Post:

    copeg (April 3rd, 2012)

Similar Threads

  1. [SOLVED] Help needed writing a program using JOptionPane
    By s1mmi in forum Object Oriented Programming
    Replies: 3
    Last Post: January 30th, 2012, 04:39 PM
  2. writing a program to interface with other programs
    By gib65 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: November 4th, 2011, 04:16 PM
  3. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  4. Question about writing a phonebook sorting program
    By Stockholm Syndrome in forum Java Theory & Questions
    Replies: 2
    Last Post: March 25th, 2011, 09:25 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM