Trouble with regex.. need help

Joined
Feb 12, 2011
Messages
1
Reaction score
0
ok what am i doing wrong, my program has to output and evaluate a string,

heres 3 examples of how it is supposed to run,

eg.1 (no string entered)
"java Word
Usage: java Word <string>"


eg.2(string containing punctuation and numbers gives the output "The string must consist of letters!")
"java Word AAABB@I5P
The string must consist of letters!"


eg.3(continous character gives the output)
"java Word AAAAAAAwwwwwwwwwwwTTT
7A11w3T"


and heres the code i got so far

import java.util.Scanner;
import java.io.PrintStream;

public class Word
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
PrintStream output = System.out;

output.print("java Word ");
String s = input.nextLine();
if (s.equals(""))
{
output.print("Usage: java Word <string>");
}
if (s.matches("[^0-9]*[0-9]+[^0-9]*"))
{
output.print("The string must consist of letters!");
}
if (s.matches("[\\p]")) //is this the correct code for all punctuations??
{
output.print("The string must consist of letters!");
}

}
}


when i enter a number i get the correct output, however I am not sure on how to exclude punctuations from the string and i get a weird error when typing in [\\p]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top