...

View Full Version : need help with an encryption program



alegramer
02-14-2012, 02:46 AM
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

jasonc005
02-16-2012, 06:29 AM
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.

alykins
02-16-2012, 01:37 PM
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...


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 (http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html)
Java lesson (http://docs.oracle.com/javase/tutorial/essential/regex/)
explanatory article (http://java.sun.com/developer/technicalArticles/releases/1.4regex/)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum