Tyler R
Tyler R

Reputation: 25

Checking a string array's words and their first letter

I am a beginner coder at my Uni. We are tasked with reading a text file, verifying all the first letters of each word are capital, and then they are put in alphabetical order. I am trying to tackle one point at a time. So I decided to go with verifying all the first letters are capital by just making my own array of words and then figuring out how to do it from a text file after. This is what I have so far and it's nothing

I have tried Character.isUpperCase(arr.charAt(0)); but i am gettin an error

public void sortStrings() throws IOException {
    String[] arr = {"Zebra", "test","Butter"};
    String[] arr2 = {"Legends","Apex","Best"};

    for(int i = 0; i < arr.length; i++) {
        if(Character.isUpperCase(arr.charAt(0)));
    } 


Cannot invoke charAt(int) on the array type String] is the error I am getting with arr.charAt(0)

Appreciate any tips !

Upvotes: 0

Views: 96

Answers (1)

molamk
molamk

Reputation: 4116

You forgot the index to access the item in your array. Replace arr.charAt(0) with arr[i].charAt(0)

String[] arr = {"Zebra", "test","Butter"};

for(int i = 0; i < arr.length; i++) {
    if(Character.isUpperCase(arr[i].charAt(0)))
        doSomethingHere();
}

Upvotes: 3

Related Questions