Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2012, 02:46 AM   PM User | #1
alegramer
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
alegramer is an unknown quantity at this point
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
alegramer is offline   Reply With Quote
Old 02-16-2012, 06:29 AM   PM User | #2
jasonc005
New to the CF scene

 
Join Date: Feb 2012
Location: Louisville
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jasonc005 is an unknown quantity at this point
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.
jasonc005 is offline   Reply With Quote
Old 02-16-2012, 01:37 PM   PM User | #3
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:56 AM.


Advertisement
Log in to turn off these ads.