循环并调用字符串和字符方法 [英] Loops and calling string and character methods

查看:86
本文介绍了循环并调用字符串和字符方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个Java程序,它要求姓氏,出生年份和员工编号。姓氏必须以字母开头,不能包含两个连续的空格,并且只能包含字母和空格。如果一切正常,则打印有效。关于出生年份的一部分,我已经想到了。最后,他们输入员工编号,必须以字母开头,后跟9位数字。例如,A123-45-6789。如果这样可行,则再次打印有效。以下是我到目前为止的情况。请帮我弄清楚剩下的!



我尝试过:



I need to create a Java Program which asks for a last name, birth year, and employee number. The last name must begin with a letter, can't contain two consecutive spaces, and it can only have letters and spaces in it. If this all works, it prints valid. There's a part about year of birth which I have figured out. Lastly, they enter the employee number and it must start with a letter and be followed by 9 digits. For example, A123-45-6789. If this works, it prints "valid" again. Below is what I have so far. Please help me figure out the rest!

What I have tried:

import java.util.*; // for Scanner
public class ThreeQuestions {

	public static void main(String[] args) {
		boolean validYear;
		boolean validLastName;
		boolean validEmployeeNumber;
		Scanner console = new Scanner(System.in);
		System.out.println("Enter last name:");
		String lastName;
		lastName = console.next( );
		lastName=lastName.toLowerCase();
		Character.isDigit(lastName.charAt(0));
			if ((lastName.charAt(0) >= 'a' && lastName.charAt(0) <= 'z') || (lastName.charAt(0) >= 'A' && lastName.charAt(0) <= 'Z'))	{
		validLastName=true;
		System.out.println("Valid");
			} else {
				System.out.println("Invalid");
				validLastName = false;
			}
			
		
		
		System.out.println("Enter year of birth:");
		int yearOfBirth;
		yearOfBirth = console.nextInt();
		if (yearOfBirth<=1999 && yearOfBirth>= 1900) {;
			System.out.println("Valid");
			 validYear = true;
		}
			else {
				System.out.println("Invalid");
				 validYear = false;
			}
		System.out.println("Enter employee number in the format LDDD-DD-DDDD:");
		String employeeNumber;
		employeeNumber = console.next();
		
		if (validYear && validLastName ) {
			System.out.println("All entries are valid.");
			
		} else {
			System.out.println("Entries are invalid. Try again.");
		}
			
	}

}

推荐答案

制作RegEx对于字符串验证,这就是我要使用的。



只是一些有趣的链接来帮助构建和调试RegEx。

这是一个链接到RegEx文档:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx的工具的链接调试它们:

.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]

这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]

这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:

Regexper [ ^ ]
RegEx are made for string validation, that is what I would use.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]


这篇关于循环并调用字符串和字符方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆