Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with an encryption program

    need help with an encryption program

    i haven't been able to get these to work

    -Print an error message and exit if the sentence

    * Contains a non-alphabetic character other than space
    or the following punctuation marks:
    ! , . ? - : ; ( ) [ ] / " '

    * Does not end with '.', '!' or '?'.

    so how do i scan the input string for those special characters?

    how do i Display an input dialog asking player 2 for a guess
    in the form: a=b. using the javax.swing.JOptionPane;

    this is what i got so far


    import javax.swing.JOptionPane;


    public class CodeCrackerGame

    {
    public static void main(String[] args)
    {

    //----------------------------------asks p1 to input 1st sentence---------------------------------

    String oSentence = JOptionPane
    .showInputDialog("player 1 enter asentence with up to 100 characters and at least 7 words");


    String[] test1 = oSentence.split(" ");// separates the whole sentence out by
    // words to find many words there are

    if (test1.length < 7 || oSentence.length() > 100)
    {
    JOptionPane.showMessageDialog(null, //checks length of sentence/characters
    "ERROR= input is less than 7 words"
    + " or has more than a 100 characters!");
    System.exit(0);

    }


    for (int i = 0; i < oSentence.length(); i++)
    {
    if (Character.isDigit(oSentence.charAt(i))) ;
    {
    System.out.println(" there is digit in the input @ " + i);
    }
    }

    // int start = 0;
    // char c = oSentence.charAt(start);
    // while (!Character.isDigit(c))
    // { start++;
    // / c = oSentence.charAt(start);

    // }
    // JOptionPane.showMessageDialog(null, // converts all to upper case
    // "ERROR= number on input" );
    // System.exit ( 0 );


    String newSentence = oSentence.toUpperCase();
    JOptionPane.showMessageDialog(null, //Converts 1st sentence to uppercase
    " Input Sentence= " + newSentence);


    //----------------------------------asks p2 to innpur 2nd sntence---------------------------------


    String o2Sentence = JOptionPane
    .showInputDialog("player 1 enter a keyword with up to 10 characters");

    if (o2Sentence.length() < 3 || o2Sentence.length() > 10)
    {
    JOptionPane.showInputDialog("ERROR= input is less than 3 characters " //checks length of sentence/characters
    + " or has more than a 10 characters!");

    }



    }
    }

    thanks for any help in advance

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Location
    Louisville
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To test if the sentence does not end with '.', '!', or '?' you would use the substring() method to just create a String that is equal to the very last character in the sentence.

    ####################################################
    int length = oSentence.length();
    String finalChar = oSentence.substring(length - 1, length);

    if(!(finalChar.equals(".") || finalChar.equals("!") || finalChar.equals("?")))
    {
    JOptionPane.showMessageDialog(null,"ERROR= invalid ending of sentence!");
    System.exit(0);
    }
    ####################################################

    I hope this helps, sorry I can't help with the whole thing.

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,816
    Thanks
    42
    Thanked 199 Times in 198 Posts
    me thinks you both are over-complicating it-
    make two regular expressions- one to check against all the invalid chars
    and one set to check against what the other player input-
    check length for validity (if that is a requirement) and then check the other players guess
    to see if it matches exactly the regex that the first player defined
    so...
    Code:
    regexPattern1 = define your pattern for excluding all invalid chars 
    string player1Str = scan
    validate player1Str is at least 3 letters and validate against regexPattern1
    regexPattern2 = validated player1Str
    string player2Str = scan
    validate player2Str against regexPattern2
    *Obviously that is not valid Java code in any way shape or form but I think it will point you in the write direction and help you learn it better than me coding it for you...
    here are some good links
    Java class lib for Pattern
    Java lesson
    explanatory article

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

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