0

I want to write a method that check if a string matches the following format: "Name Surname" in Java. There will be max one space between Name and Surname, and both the starting letter of name and surname should be in capital letters.

Here is what I did:

public static Boolean correctFormat(ArrayList<String> e) 
   { 
       int x = 0; 
       for(int i = 0; i < e.size(); i++) 
       { 
           if (e.get(i).indexOf(" ") != -1 && str.substring() 
           { 
               x++; 
           } 
           
  }  

However, I could not find a way to check if name and surname's starting letter capital. Name and surname length can vary.

Note: The parameter of the method is ArrayList, so it takes ArrayList as an input.

Xemorphy
  • 3
  • 3
  • Java has a string method `toUpperCase`; that might be of help. – Scott Hunter Mar 07 '22 at 14:59
  • Is it only important that the first letter of each word is upper case, or is it also important that the other letters are lower case? – Gus Mar 07 '22 at 15:09
  • I tried to use toUpperCase; however, it gives an error that the int cannot be referenced. – Xemorphy Mar 07 '22 at 15:13
  • @Gus the other letters do not need to be lower, only the first letters. – Xemorphy Mar 07 '22 at 15:16
  • You can use this regex too `[A-Z]\w+\s+[A-Z]\w+` https://www.regexplanet.com/share/index.html?share=yyyyprxy9kr – Gus Mar 07 '22 at 15:21
  • 1
    Do not take an `ArrayList` as a parameter, take a `List`. Do not use a classical for loop to iterate over the list, use an enhanced for loop (for-each): `for (String s : e)`. Do not search for `indexOf("")` (the empty string) in a string, it will always match at position `0`; presumably, you want to search for a space: `" "`. Do not return `Boolean` unless you need the value to be able to be equal to `null`; use `boolean` instead. – David Conrad Mar 07 '22 at 16:23
  • Expecting the name to be in the form "Name Surname" is fine for an exercise but in the real world, names come in many other forms and that pattern should not be assumed. – David Conrad Mar 07 '22 at 16:26
  • @DavidConrad I edited Do not search for indexOf("") to indexOf(" ") earlier, may you look at the older version of the post? Also, can you explain more why I should List parameter instead of ArrayList and enhanced loop for this problem. – Xemorphy Mar 07 '22 at 16:54
  • Yes, the version I was looking at still had "". See [Program to interface, not implementation](https://stackoverflow.com/questions/2697783/what-does-program-to-interfaces-not-implementations-mean) but consider if you call your method like `correctFormat(List.of("Bob Lud"))`; this will fail because `List.of` returns a `List` that is not an `ArrayList`. For the enhanced for loop, never much around with indices unless you need to. When you can avoid it, you avoid a common source of errors. – David Conrad Mar 07 '22 at 17:02

2 Answers2

0

you can use string split and character.isuppercase() and looping process to solve your issue as shown below

public class Test {

    public static boolean validateString(String string){
        String[] stringArray = string.split(" ");
        if(null == string || stringArray.length>2 || stringArray.length == 1) return false;
        for(String s : stringArray){
            if(!(Character.isUpperCase(s.toCharArray()[0])))return false;
        }
        return true;
    }

    public static void main(String[] args) {
        String s  = "Hello World";
        List<String> names = new ArrayList<>();
        names.add(s);
        names.forEach(string -> System.out.println("Is the string "+string+" valid :"+ validateString(s)));
        
    }
    
}

output : Is the string Hello World valid :true

0

You may use regex to check your conditions and iterate through your ArrayList using for-each loop.

    public static Boolean correctFormat(ArrayList<String> e)
    {
        String camelCasePattern = "([A-Z][a-z]+ [A-Z][a-z]+)";
        for(String s : e)
            if(!s.matches(camelCasePattern))
                return false;
        return true;
    }

The above code will return true only when all elements in ArrayList e will match as per the defined pattern.