【发布时间】:2019-11-11 07:21:08
【问题描述】:
基本上我的程序要求用户输入一个新名称,程序将检查新名称是否符合所有要求。
是的,名称应该添加到数组中并打印在外部文件中。 如果不是,它应该要求用户输入一个新名称。
我的问题是,我不会如何使用 while 循环来确保只有名称才能跳出循环。
我也在 main 方法中签名我的问题
public static void main(String[] args)throws FileNotFoundException {
readFile();
listUserName();
while() { // my question is at here that I don't how to use while loop to make
//sure that only the name which pass all the check..method can
//jump out of the loop
inputNewName();
checkduplicate();
checklength();
checkcase();
checkstart();
checknumber();
checkspecial();
}
addNewName();
listUserName();
}
public static void readFile()throws FileNotFoundException {
//read file and reseve in array
Scanner input = new Scanner(new File("users.txt"));
int i=0;
while(input.hasNext()) {
String info=input.next();
userName[i]=info;
i++;
}
}
public static void listUserName() {//print name
for(int i=0;i<userName.length;i++) {
System.out.println(userName[i]);
}
}
public static void inputNewName() {// prompt uder for a new name
System.out.println("Create a new user:");
Scanner console=new Scanner(System.in);
newname=console.next();
}
public static void addNewName()throws FileNotFoundException {//add new name in array
System.out.println("User: \""+newname+"\" added successfully!");
System.out.println("List of usernames: ");
String[] tempuser=new String[userName.length+1];
for(int i=0;i<(userName.length+1);i++) {
if(i<userName.length) {
tempuser[i]=userName[i];
}else if(i==userName.length) {
tempuser[userName.length] =newname;
}
System.out.println(tempuser[i]);
}
userName=tempuser;
PrintStream out=new PrintStream(new File("users.txt"));
for(int i=0;i<userName.length;i++) {
out.println(userName[i]);
}
}
public static void checkduplicate() { //check duplicate
for(int i=0;i<userName.length;i++) {
if(newname.equals(userName[i])) {
System.out.println("Invalid Name.Name already in use.");
}else {
valid=true;
}
}
}
public static void checklength() {//check length
if(newname.length()>7) {
System.out.println("Invalid Name"+"\n"+"Name too long.");
}
if(newname.length()<4) {
System.out.println("Invalid Name"+"\n"+"Name too short.");
}
}
public static void checkcase() {//check case
boolean upcase=false;
boolean lowcase=false;
for(int i=0;i<newname.length();i++) {
if((0+newname.charAt(i))>=65&&(0+newname.charAt(i)<=90)) {
upcase=true;
}else if((0+newname.charAt(i))>=97&&(0+newname.charAt(i))<=122) {
lowcase=true;
}
}
if(upcase==false||lowcase==false) {
System.out.println("Usernames must have lower-case and upper-case");
}
}
public static void checkstart() {
if(((0+newname.charAt(0))<65&&(0+newname.charAt(0)>99))||
((0+newname.charAt(0))<97&&(0+newname.charAt(0))>122)){
System.out.println("Invalid name. Name must start with a letter");
}
}
public static void checknumber() {
boolean check=false;
for(int i=0;i<newname.length();i++) {
if((0+newname.charAt(i))>=48&&(0+newname.charAt(i))<=57) {
check=true;
}
}
if(check!=true) {
System.out.println("Username must have at least one number");
}
}
public static void checkspecial() {
boolean check=false;
for(int i=0;i<newname.length();i++) {
if((0+newname.charAt(i))==33||(0+newname.charAt(i))==35||(0+newname.charAt(i))==63) {
check=true;
}
}
if(check!=true) {
System.out.println("Username must have at least one special character.");
}
}
}
【问题讨论】:
-
对于具有真/假结论的启动方法应返回
boolean- 在您的循环中检查此值并根据需要中断或继续
标签: java arrays while-loop