我是 Java 的初学者,遇到了这个学校问题:
The authority of XYZ gated residential colony wants its residents' name datum Should be stored in the following format - residents' name his/her father's name. Write a program to concat the father's name to the residents' name. The name should be validated,on validation the name should contain only alphabets and space is allowed. If the name is not valid display the message "Invalid name". If valid string then convert it to uppercase and print it
Sample Input 1:
Inmate's name:Aron
Inmate's father's name:Terby
Sample Output 1:
ARON TERBY
错误:每当我输入一个由两个字母组成的囚犯姓名时,如 Aron Kumar,它会打印出“无效输入”,否则对于单个单词字符串输入代码工作正常。
这是我写的代码:
Scanner sc=new Scanner(System.in);
System.out.println("Inmate's name:"); //if I enter 2 word string,output-"Invalid name1"//
String name=sc.nextLine();
System.out.println("Inmate's father's name:");
String fname=sc.nextLine();
String s3=name.toUpperCase();
String s4=fname.toUpperCase();
char[] a1= s3.toCharArray();
char[] a2= s4.toCharArray();
for(int i=0;i<a1.length;i++)
{
if(a1[i]>='A' && a1[i]<='Z')
count=1;
else {
System.out.print("Invalid name1");
count=0;
break; }
}
if(count==1)
{
for(int i=0;i<a2.length;i++)
{
if(a2[i]>='A' && a2[i]<='Z')
count=2;
else {
System.out.print("Invalid name");
break; }
}
}
if(count==2) {
System.out.print(s3+" "+s4);
}
}
}
最佳答案
问题是您没有检查空格字符。检查如下:
if (a1[i] >= 'A' && a1[i] <= 'Z' || a1[i] == ' ')
您的代码的另一个问题是在每次迭代中将 count
的值更改为 1
和 2
而它应该在循环时更改终止。下面给出了更正后的代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int count = 0, i;
Scanner sc = new Scanner(System.in);
System.out.println("Inmate's name:");
String name = sc.nextLine();
System.out.println("Inmate's father's name:");
String fname = sc.nextLine();
String s3 = name.toUpperCase();
String s4 = fname.toUpperCase();
char[] a1 = s3.toCharArray();
char[] a2 = s4.toCharArray();
for (i = 0; i < a1.length; i++) {
if (!(a1[i] >= 'A' && a1[i] <= 'Z' || a1[i] == ' ')) {
System.out.print("Invalid name1");
count = 0;
break;
}
}
// If 'i' reached a1.length, it means no invalid character was found
if (i == a1.length) {
count = 1;
}
if (count == 1) {
for (i = 0; i < a2.length; i++) {
if (!(a2[i] >= 'A' && a2[i] <= 'Z' || a2[i] == ' ')) {
System.out.print("Invalid name");
break;
}
}
// If 'i' reached a2.length, it means no invalid character was found
if (i == a2.length) {
count = 2;
}
}
if (count == 2) {
System.out.print(s3 + " " + s4);
}
}
}
补充说明:
您可以使用正则表达式使您的代码更短,如下所示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Inmate's name: ");
String name = sc.nextLine();
System.out.print("Inmate's father's name: ");
String fname = sc.nextLine();
if (name.matches("[A-Za-z\\s]+") && fname.matches(("[A-Za-z\\s]+"))) {
System.out.println(name.toUpperCase() + " " + fname.toUpperCase());
} else if (!name.matches("[A-Za-z\\s]+")) {
System.out.println("Inmate's name is invalid");
} else if (!fname.matches(("[A-Za-z\\s]+"))) {
System.out.println("Inmate's father's name is invalid");
}
}
}
正则表达式的解释,[A-Za-z\\s]+
:
A-Za-z
用于字母表。\\s
用于空格。[A-Za-z\\s]+
末尾的+
表示允许多次出现。
示例运行:
Inmate's name: Ram Kumar
Inmate's father's name: Raj Kumar
RAM KUMAR RAJ KUMAR
另一个样本运行:
Inmate's name: Ram5 Kumar
Inmate's father's name: Raj Kumar
Inmate's name is invalid
另一个样本运行:
Inmate's name: Ram Kumar
Inmate's father's name: Raj5 Kumar
Inmate's father's name is invalid
关于输出中的 Java 字符串连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61177893/