输出中的 Java 字符串连接错误

标签 java arrays string

我是 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 的值更改为 12 而它应该在循环时更改终止。下面给出了更正后的代码:

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]+:

  1. A-Za-z 用于字母表。
  2. \\s 用于空格。
  3. [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/

相关文章:

javascript - Jquery - 从选择框进行数组操作

javascript - 关于空数组的 boolean 逻辑。

c++ - 对象数组的默认复制行为

c++ - 两个字符串的交换元素C++

c - 如何在c编程中将用户输入与一个while循环的字符串进行比较

java - 尝试使图像随机移动但它没有移动

java - Android:如何检测 "Turn on USB storage"广播?

java - android权限唤醒锁有什么用?

jquery - 如何使用 Jquery 替换整个 HTML 页面中的占位符?

java - JPA 字段名称中的井号