Results 1 to 12 of 12
  1. #1
    bergadur is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Help with program

    i need help with a project which is a encrypting program. the file is attached and the deadline is right around the corner. I can't get it to work in the codeMessage() method. What im trying to do is replace the letters in the message with whatever you type in the fields below and then print it out in the outputField. Please help.
    Attached Files Attached Files

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Most people here aren't going to download attached files, you should post the code here. If it's a lot of code, post relevant code, use code tags also.
    [code]
    YOUR CODE HERE
    [/code]

    Ask specific questions.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Even better create an SSCCE (google it) and post that here.

  4. #4
    bergadur is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    sorry if i do this wrong.
    Java Code:
    public void actionPerformed(ActionEvent e)   {
            System.out.println("");
            characterizeMessage();
            changeFields();
            checkCharacters();
            codeMessage();
        }
        
    public void codeMessage(){ 
        System.out.println("");   
        
        for (int counter5 = 0; counter5 < messageFieldChar.length; counter5++){
            System.out.print("");
            if (messageFieldChar[counter5] == 'A' || messageFieldChar[counter5] == 'a'){
                messageFieldChar[counter5] = letters[0];
            }
        }
            for (int counter6 = 0; counter6 < messageStr.length(); counter6++){
                System.out.print(messageFieldChar[counter6]);
            }
    }
    
    
    public void characterizeMessage(){
            
            messageStr = inputField.getText();
            char[] messageFieldChar = messageStr.toCharArray();
            
            for (int counter1 = 0; counter1 < messageStr.length(); counter1++){
                System.out.print(messageFieldChar[counter1]);
            }
            
            
    }
    
    public void checkCharacters() {
            letters = new char[27];
            letters[0] = aChar;
            letters[1] = bChar;
            letters[2] = cChar;
            letters[3] = dChar;
            letters[4] = eChar;
            letters[5] = fChar;
            letters[6] = gChar;
            letters[7] = hChar;
            letters[8] = iChar;
            letters[9] = jChar;
            letters[10] = kChar;
            letters[11] = lChar;
            letters[12] = mChar;
            letters[13] = nChar;
            letters[14] = oChar;
            letters[15] = pChar;
            letters[16] = qChar;
            letters[17] = rChar;
            letters[18] = sChar;
            letters[19] = tChar;
            letters[20] = uChar;
            letters[21] = vChar;
            letters[22] = wChar;
            letters[23] = xChar;
            letters[24] = yChar;
            letters[25] = zChar;
            letters[26] = _Char;
            
            System.out.println("");
            for (int counter2 = 0; counter2 < letters.length; counter2++){
                System.out.print(letters[counter2]);
            }
    }
    
    public void changeFields() {
            aStr = aField.getText();
            char[] aFieldChar = aStr.toCharArray();
            aChar = aFieldChar[0];
            
            bStr = bField.getText();
            char[] bFieldChar = bStr.toCharArray();
            bChar = bFieldChar[0];
            
            cStr = cField.getText();
            char[] cFieldChar = cStr.toCharArray();
            cChar = cFieldChar[0];
            
            dStr = dField.getText();
            char[] dFieldChar = dStr.toCharArray();
            dChar = dFieldChar[0];
            
            eStr = eField.getText();
            char[] eFieldChar = eStr.toCharArray();
            eChar = eFieldChar[0];
            
            fStr = fField.getText();
            char[] fFieldChar = fStr.toCharArray();
            fChar = fFieldChar[0];
            
            gStr = gField.getText();
            char[] gFieldChar = gStr.toCharArray();
            gChar = gFieldChar[0];
            
            hStr = hField.getText();
            char[] hFieldChar = hStr.toCharArray();
            hChar = hFieldChar[0];
            
            iStr = iField.getText();
            char[] iFieldChar = iStr.toCharArray();
            iChar = iFieldChar[0];
            
            jStr = jField.getText();
            char[] jFieldChar = jStr.toCharArray();
            jChar = jFieldChar[0];
            
            kStr = kField.getText();
            char[] kFieldChar = kStr.toCharArray();
            kChar = kFieldChar[0];
            
            lStr = lField.getText();
            char[] lFieldChar = lStr.toCharArray();
            lChar = lFieldChar[0];
            
            mStr = mField.getText();
            char[] mFieldChar = mStr.toCharArray();
            mChar = mFieldChar[0];
            
            nStr = nField.getText();
            char[] nFieldChar = nStr.toCharArray();
            nChar = nFieldChar[0];
            
            oStr = oField.getText();
            char[] oFieldChar = oStr.toCharArray();
            oChar = oFieldChar[0];
            
            pStr = pField.getText();
            char[] pFieldChar = pStr.toCharArray();
            pChar = pFieldChar[0];
            
            qStr = qField.getText();
            char[] qFieldChar = qStr.toCharArray();
            qChar = qFieldChar[0];
            
            rStr = rField.getText();
            char[] rFieldChar = rStr.toCharArray();
            rChar = rFieldChar[0];
            
            sStr = sField.getText();
            char[] sFieldChar = sStr.toCharArray();
            sChar = sFieldChar[0];
            
            tStr = tField.getText();
            char[] tFieldChar = tStr.toCharArray();
            tChar = tFieldChar[0];
            
            uStr = uField.getText();
            char[] uFieldChar = uStr.toCharArray();
            uChar = uFieldChar[0];
            
            vStr = vField.getText();
            char[] vFieldChar = vStr.toCharArray();
            vChar = vFieldChar[0];
            
            wStr = wField.getText();
            char[] wFieldChar = wStr.toCharArray();
            wChar = wFieldChar[0];
            
            xStr = xField.getText();
            char[] xFieldChar = xStr.toCharArray();
            xChar = xFieldChar[0];
            
            yStr = yField.getText();
            char[] yFieldChar = yStr.toCharArray();
            yChar = yFieldChar[0];
            
            zStr = zField.getText();
            char[] zFieldChar = zStr.toCharArray();
            zChar = zFieldChar[0];
            
            _Str = _Field.getText();
            char[] _FieldChar = _Str.toCharArray();
            _Char = _FieldChar[0];
    }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First of all, couldn't you use arrays to eliminate 99% of the redundancy in your code?

    edit: or perhaps a HashMap<Character, JTextField>.
    Last edited by Fubarable; 05-13-2011 at 02:35 AM.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Java Code:
    aStr = aField.getText();
    char[] aFieldChar = aStr.toCharArray();
    aChar = aFieldChar[0];
    :eek:
    Java Code:
    aChar = aField.getText().charAt(0);

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Junky View Post
    Java Code:
    aStr = aField.getText();
    char[] aFieldChar = aStr.toCharArray();
    aChar = aFieldChar[0];
    :eek:
    Java Code:
    aChar = aField.getText().charAt(0);

    Java Code:
    // in a loop
    charMap.put(charIndex, charFieldMap.get(charIndex).getText().charAt(0));

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Only if you know maps ;)

  9. #9
    bergadur is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks for the code simplicity but i am unable to get the message to exchange it's characters with the corresponding textFields

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here's a complex version of what I meant:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.*;
    import javax.swing.text.*;
    
    @SuppressWarnings("serial")
    public class CharMapGUI extends JPanel {
       public static final String AVAIL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
       private static final int CHAR_FIELD_COLUMNS = 5;
       private JPanel charFieldPanel = new JPanel(new GridLayout(0, 3, 25, 10));
       private Map<Character, Character> charMap = new HashMap<Character, Character>();
       private Map<Character, JTextField> charFieldMap = new HashMap<Character, JTextField>();
    
       public CharMapGUI() {
          for (char c : AVAIL_CHARS.toCharArray()) {
             charMap.put(c, c);
             JTextField charField = new JTextField("" + c, CHAR_FIELD_COLUMNS);
             ((PlainDocument) charField.getDocument()).setDocumentFilter(new SingleCharDocFilter());
             charField.addFocusListener(new CharFieldFocusListener());
             JPanel labelFieldPanel = new JPanel(new BorderLayout());
             labelFieldPanel.add(new JLabel("" + c + ":"), BorderLayout.WEST);
             labelFieldPanel.add(charField, BorderLayout.EAST);
             charFieldPanel.add(labelFieldPanel);
             charFieldMap.put(c, charField);
          }
    
          setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
          setLayout(new BorderLayout());
          add(charFieldPanel, BorderLayout.CENTER);
          add(new JPanel() {{
             add(new JButton(new AssignCharactersAction()));
          }}, BorderLayout.PAGE_END);
       }
    
       private class SingleCharDocFilter extends DocumentFilter {
          private static final int TEXT_LENGTH = 1;
    
          @Override
          public void insertString(FilterBypass fb, int offset, String insertText, AttributeSet attr)
                   throws BadLocationException {
             Document doc = fb.getDocument();
             StringBuilder sb = new StringBuilder(doc.getText(0, doc.getLength()));
             sb.insert(offset, insertText);
             if (checkText(sb)) {
                super.insertString(fb, offset, insertText.toUpperCase(), attr);
             }
          }
    
          @Override
          public void replace(FilterBypass fb, int offset, int length, String replaceText,
                   AttributeSet attrs) throws BadLocationException {
             Document doc = fb.getDocument();
             StringBuilder sb = new StringBuilder(doc.getText(0, doc.getLength()));
             sb.replace(offset, offset + length, replaceText);
             if (checkText(sb)) {
                super.replace(fb, offset, length, replaceText.toUpperCase(), attrs);
             }
          }
    
          @Override
          public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
             Document doc = fb.getDocument();
             StringBuilder sb = new StringBuilder(doc.getText(0, doc.getLength()));
    
             sb.delete(offset, offset + length);
             if (checkText(sb)) {
                super.remove(fb, offset, length);
             }
          }
    
          private boolean checkText(StringBuilder sb) {
             if (sb.length() != TEXT_LENGTH) {
                return false;
             }
             if (!CharMapGUI.AVAIL_CHARS.contains(sb.toString().toUpperCase())) {
                return false;
             }
             return true;
          }
    
       }
    
       private class CharFieldFocusListener extends FocusAdapter {
          @Override
          public void focusGained(FocusEvent e) {
             ((JTextComponent) e.getComponent()).selectAll();
          }
       }
    
       private class AssignCharactersAction extends AbstractAction {
          AssignCharactersAction() {
             super("Assign Characters");
          }
    
          @Override
          public void actionPerformed(ActionEvent arg0) {
             for (char c : AVAIL_CHARS.toCharArray()) {
                String fieldText = charFieldMap.get(c).getText().trim();
                if (fieldText.length() == 1) {
                   charMap.put(c, charFieldMap.get(c).getText().charAt(0));
                } else {
                   charMap.put(c, null); 
                }
             }
    
             // show it works:
             for (char c : AVAIL_CHARS.toCharArray()) {
                System.out.printf("%s: %s%n", c, charMap.get(c));
             }
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Foo001");
          frame.getContentPane().add(new CharMapGUI());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Don't try to use this code or you'll definitely be busted. :)

  11. #11
    bergadur is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks that works great but the problem i keep running into is trying to go through the message array and change the letters with what ever shows up in that field. The codeMessage() method won't work. I think it is because it doesn't like the if statement inside a for loop.

    It's this I don't think it likes:
    Java Code:
    for (int counter5 = 0; counter5 < messageFieldChar.length; counter5++){
            System.out.print("");
            if (messageFieldChar[counter5] == 'A' || messageFieldChar[counter5] == 'a'){
                messageFieldChar[counter5] = letters[0];
            }
        }
    Last edited by bergadur; 05-13-2011 at 02:26 PM.

  12. #12
    bergadur is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •