public static void main(String[] args) {
System.out.println(Pattern.matches(regx, "我我我我我我我"));
System.out.println(Pattern.matches(regx, "aBcDefghij"));
}
代码如下:
import?java.awt.Dimension;
import?java.awt.FlowLayout;
import?javax.swing.JButton;
import?javax.swing.JComboBox;
import?javax.swing.JFrame;
import?javax.swing.JTextField;
public?class?App?extends?JFrame?{
private?JComboBoxString?cbxNicks;
//?昵称
private?String[]?nicks?=?{?
"测试",?"李二牛",?"牛大胆",?
"张三",?"李四",?"王五"
};
public?App()?{
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new?FlowLayout());
JTextField?txtNick?=?new?JTextField();
this.add(txtNick);
JButton?btnQuery?=?new?JButton("查询");
btnQuery.addActionListener(e?-?{
cbxNicks.removeAllItems();
for?(String?nick?:?nicks)?{
if?(nick.contains(txtNick.getText()))?{
cbxNicks.addItem(nick);
});
this.add(btnQuery);
cbxNicks?=?new?JComboBoxString();
this.add(cbxNicks);
public?static?void?main(String[]?args)?{
new?App().setVisible(true);
运行结果:
//只能按照你的题目意思给你写一个,这个是满足你现在题目要求的,如果名字不固定就不好说了
Scanner?sc?=?new?Scanner(System.in);
System.out.println("请输入你的名字:");
String?name?=?sc.nextLine();//注意要用nextLine()
//截取第一个字母
String?first?=?name.substring(0,1);
//通过空格分割
String?strs[]?=?name.split("?");
name?=?first+strs[strs.length-1];
System.out.println(name);
//结果
请输入你的名字:
John?Smith
JSmith
可以通过File的exists方法进行查询是否匹配,如果存储就返回真,不存在返回假.
举例:
public static String fileExists(String filePath){
File file=new File(filePath);
if(!file.exists()) {
return "false";//如果存在输出结果
} else{
return "true";
备注:以上放只需要传入"文件的路径+对应的文件名"(filePath),之后即可判断是否存在,如果循环判断,那么就多次调用此方法即可.
public?static?void?main(String[]?args){
String?新郎="新郎名字";
String?新娘="新娘名字";
System.out.println("祝新郎"+新郎+"和新娘"+新娘+"新婚幸福");
①写一个操作数据库的通用DAO(假设为Dao.java),提供
-加载数据库驱动和获取数据库连接的方法:void getConnection();
-执行查询的方法:ResultSet executeQuery(String sql);
-执行更新的方法:Integer executeUpdate(String sql);
-关闭资源的方法:void releaseResource(Connection con);
-username = yourTextField.getText();
-password = yourPasswordField.getText();
-验证username、password是否是有效输入值(例如:不能包含特殊字符,不能有注入嫌疑等)
-String sql = "select * from yourTable where username='"+username+"' and password='"+password+"'" ;
-Dao.executeQuery(sql):如果有结果说明合法,否则不合法.
不懂Connect 我