ユーザーが入力した入力にスペースが含まれているかどうかを判断するにはどうすればよいですか? ユーザーの入力は文字列に格納されます。私が言及している部分は、アスタリスクの間です。正規表現を使用できると読みましたが、それを使用して文字列 s にスペースがあるかどうかを検出する方法がわかりません。
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LAnalyze{
public static void main(String[] args) {
int j = 0;
String s = null;
Scanner scan = new Scanner(System.in);
System.out.println("Enter an input: ");
s = scan.nextLine();
****************************************************
Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();
if(spaceIsFound)
*******************************************************
{
System.out.println("Invalid character(s) entered.. Program terminated!\n");
System.exit(0);
}
else if( s.charAt(0)=='a'||s.charAt(0)=='b'||s.charAt(0)=='c'||s.charAt(0)=='d'||s.charAt(0)=='e'||s.charAt(0)=='f'||
s.charAt(0)=='g'||s.charAt(0)=='h'||s.charAt(0)=='i'||s.charAt(0)=='j'||s.charAt(0)=='k'||s.charAt(0)=='l'||
s.charAt(0)=='m'||s.charAt(0)=='n'||s.charAt(0)=='o'||s.charAt(0)=='p'||s.charAt(0)=='q'||s.charAt(0)=='r'||
s.charAt(0)=='s'||s.charAt(0)=='t'||s.charAt(0)=='u'||s.charAt(0)=='v'||s.charAt(0)=='w'||s.charAt(0)=='x'||
s.charAt(0)=='y'||s.charAt(0)=='z'||s.charAt(0)=='A'||s.charAt(0)=='B'||s.charAt(0)=='C'||s.charAt(0)=='D'||
s.charAt(0)=='E'||s.charAt(0)=='F'||s.charAt(0)=='G'||s.charAt(0)=='H'||s.charAt(0)=='I'||s.charAt(0)=='J'||
s.charAt(0)=='K'||s.charAt(0)=='L'||s.charAt(0)=='M'||s.charAt(0)=='N'||s.charAt(0)=='O'||s.charAt(0)=='P'||
s.charAt(0)=='Q'||s.charAt(0)=='R'||s.charAt(0)=='S'||s.charAt(0)=='T'||s.charAt(0)=='U'||s.charAt(0)=='V'||
s.charAt(0)=='W'||s.charAt(0)=='X'||s.charAt(0)=='Y'||s.charAt(0)=='Z') {
System.out.print( (s) + ": Identifier\n");
for (int k = 0; k < s.length(); k++) {
j++;
System.out.println("Token " + (j) + " = " + (s.charAt(k)));
}
}
else if(s.charAt(0)=='0'||s.charAt(0)=='1'||s.charAt(0)=='2'||s.charAt(0)=='3'||s.charAt(0)=='4'||s.charAt(0)=='5'||
s.charAt(0)=='6'||s.charAt(0)=='7'||s.charAt(0)=='8'||s.charAt(0)=='9') {
System.out.print( (s) + ": Unsigned Integer\n");
for (int m = 0; m < s.length(); m++) {
j++;
System.out.println("Token " + (j) + " = " + (s.charAt(m)));
}
}
else {
System.out.println("Invalid character(s) entered.. Program terminated!\n");
System.exit(0);
}
}
}