🌐
GitHub
github.com › thesaravanakumar › Cognizant-Early-Engagement › tree › main › Java Programming Funcamentals › String Concatenation
Cognizant-Early-Engagement/Java Programming Funcamentals/String Concatenation at main · thesaravanakumar/Cognizant-Early-Engagement
Find solutions for the Cognizant Early Engagement Program [ Continuous Skill Development ]. - Cognizant-Early-Engagement/Java Programming Funcamentals/String Concatenation at main · thesaravanakumar/Cognizant-Early-Engagement
Author   thesaravanakumar
🌐
W3Schools
w3schools.com › java › java_strings_concat.asp
Java Strings Concatenation
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The + operator can be used between strings to combine them.
🌐
Blogger
myctsbag.blogspot.com › 2016 › 04 › java-questions-simple-string.html
Java Questions :Simple String Manipulation | My CTS Bag
Write a program to read a string and return a modified string based on the following rules. Return the String without the first 2 ...
🌐
W3Schools
w3schools.com › java › ref_string_concat.asp
Java String concat() Method
Java Examples Java Videos Java ... System.out.println(firstName.concat(lastName)); ... The concat() method appends (concatenate) a string to the end of another string....
🌐
YouTube
youtube.com › simplify coding
String concatenation - YouTube
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new features · © 2023 Google LLC · YouTube, a Google company
Published   March 7, 2021
Views   2K
🌐
FACE Prep
faceprep.in › c › concatenate-two-strings-in-c-c-java-and-python-faceprep
Concatenate Two Strings in C, C++, Java and Python
July 6, 2023 - FACE Prep - India's largest placement focused skill development company. FACE Prep helps over 5 lakh students every year get placed, making us one of the most trusted placement prep brands. Upskill yourself, through our Articles, Videos, Webinars, tests and more.
🌐
Educative
educative.io › answers › how-to-concatenate-strings-in-java
How to concatenate strings in Java - Educative.io
String concatenation combines multiple strings into a single new string. It’s useful in creating file paths, personalizing content, constructing URLs, bioinformatics (DNA/protein sequences), etc.
🌐
Javatpoint
javatpoint.com › java-string-concat
Java String concat() method
Java String concat() method with method signature and examples of concat, compare, touppercase, tolowercase, trim, length, equals, split, string concat in java etc.
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › string-concatenation-in-java
String Concatenation in Java
One of the easiest and simplest way to concatenate strings in Java is using the + operator.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › beginners-guide-to-java-string-concatenation-1e2fbccda0bc
Beginner’s Guide to Java String Concatenation
March 26, 2024 - String concatenation in Java is ... web applications. At its core, string concatenation is the process of combining two or more strings end-to-end to form a new string....
Top answer
1 of 4
2

The problem is that you are not checking for a space character. Check it as follows:

if (a1[i] >= 'A' && a1[i] <= 'Z' || a1[i] == ' ')

Another problem with your code is changing the value of count to 1 and 2 in each iteration whereas it should be changed when the loop terminates. Given below is the corrected code:

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);
        }
    }
}

Additional note:

You can make your code much shorter by using regex as follows:

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");
        }
    }
}

The explanation of the regex, [A-Za-z\\s]+:

  1. A-Za-z is for alphabets.
  2. \\s is for space.
  3. The + at the end of [A-Za-z\\s]+ means more than one occurrences are allowed.

A sample run:

Inmate's name: Ram Kumar
Inmate's father's name: Raj Kumar
RAM KUMAR RAJ KUMAR

Another sample run:

Inmate's name: Ram5 Kumar
Inmate's father's name: Raj Kumar
Inmate's name is invalid

Another sample run:

Inmate's name: Ram Kumar
Inmate's father's name: Raj5 Kumar
Inmate's father's name is invalid
2 of 4
0

When you compare char values in Java, you're relying on the ASCII value of that char. The ASCII value of A is 65, whereas the ASCII value of Z is 90.

Your current code is simply evaluating each char in the character array to make sure it's in the range of 65 to 90, inclusive. The ASCII value of the space char, however, is 32, falling well outside of that range.

Rewrite your code to accept capital letters or spaces (as dictated by the problem description) like so:

if((a1[i]>='A' && a1[i]<='Z') || (a1[i] == 32))
🌐
ThoughtCo
thoughtco.com › concatenation-2034055
Understanding the Concatenation of Strings in Java
May 18, 2025 - In Java, you can join strings using the + operator or the concat() method. The + operator can join various data types, but concat() only works with string objects. Using concat() can result in errors if strings are null, while + handles them ...
🌐
CodeGym
codegym.cc › java blog › strings in java › string concatenation in java
String Concatenation in Java
April 1, 2025 - Java String concatenation is an operation to join two or more strings and return a new one. Also, the concatenation operation can be used to cast types to strings. You can concatenate strings in Java in two different ways...
🌐
Baeldung
baeldung.com › home › java › java string › string concatenation in java
String Concatenation in Java | Baeldung
January 8, 2024 - In this article, we provided a quick overview of string concatenation in Java. Additionally, we discussed in detail the use of concat() and the “+” operator to perform string concatenations.
🌐
Vega IT
vegaitglobal.com › media-center › knowledge-base › string-concatenation-in-java
String concatenation in Java | Vega IT
March 27, 2024 - For simple string concatenation without “for” or “while” loops, you don't need to use StringBuilder. Java compiler will jump right in and optimize your '+' string concatenation by replacing it with a StringBuilder, if all of the substrings ...
🌐
DataFlair
data-flair.training › blogs › string-concatenation-in-java
String Concatenation in Java - DataFlair
October 29, 2025 - These methods empower Java developers with a wide array of options for efficiently manipulating strings in their applications. The simplest way to concatenate two strings is by using the + operator.
🌐
Javatpoint
javatpoint.com › string-concatenation-in-java
String Concatenation in Java - javatpoint
June 25, 2018 - String Concatenation. There are two ways to concat the string. Let's take the example of string concatenation
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-concat-examples
Java String concat() Method with Examples - GeeksforGeeks
The concat() method in Java is used to append one string to another and returns a new combined string. It does not modify the original string since strings are immutable. ... class GFG { public static void main(String args[]) { // String ...
Published   April 7, 2026
🌐
TechVidvan
techvidvan.com › tutorials › java-string-concatenation
Java String concat() Method with Examples - TechVidvan
February 17, 2025 - Concatenation is combining two or more strings to form a new string by appending the next string to the end of the previous strings. In Java, two strings can be concatenated using the + or += operator or the concat() method, defined in the ...
🌐
Baeldung
baeldung.com › home › java › java string › concatenating strings in java
Concatenating Strings in Java | Baeldung
May 8, 2025 - At first glance, this may seem much more concise than the StringBuilder option. However, when the source code compiles, the + symbol translates to chains of StringBuilder.append() calls. Due to this, mixing the StringBuilder and + method of concatenation is considered bad practice.