How to capture the first letter of a String name and if the letter is 'C' save the name in an array?

Asked

Viewed 3,006 times

7

I have an algorithm that reads a name of someone and if the name starts with the letter "C" should capture the name and save in the vector. I’ve tried it anyway but I can’t do it.

public static void main(String[] args) {

    String nome;
    String []soC = new String[10];
    Scanner sc = new Scanner(System.in);

    for(int c = 0; c < soC.length; c++ ){
        System.out.println(" Digite o seu nome:");
        nome = sc.next();
        if(nome){

        }
    }
}   

I did so:

public static void main(String[] args) {

    String nome;
    String []soC = new String[10];
    Scanner sc = new Scanner(System.in);
    int total = 0;
    for(int c = 0; c < soC.length; c++ ){
        System.out.println(" Digite o seu nome:");
        nome = sc.next();
        if(nome.charAt(0) == 'C'){
            total++;
            soC[total] = nome;
        }
    }

    for(int i = 0; i<=total; i++){
        System.out.println(soC[i]);
    }
}

But it’s printing like this:

null
Carla
Carlina
Clau
Craudio

How do I get this null?

  • 1

    You remove this null by incrementing the total variable after you set the name in the vector. Or you do this: "Soc[total++] = name" or "Soc[total] = name; total++;"

2 answers

3


Try to use the method charAt, this method returns the char of the String according to the specified index, in this case the index 0 which is the first letter of the name.

public static void main(String[] args) {

    String nome;
    String []soC = new String[10];
    Scanner sc = new Scanner(System.in);
    int total = 0;
    for(int c = 0; c < soC.length; c++ ){
        System.out.println(" Digite o seu nome:");
        nome = sc.next();
        if(nome.charAt(0) == 'C'){
            soC[total] = nome;
            total++;
        }
    }

    for(int i = 0; i<=total; i++){
        if (soC[i] != null){
            System.out.println(soC[i]);
        }
    }
} 
  • Help me? I edited the question.

  • Placing the "total++" increment after the assignment "Soc[total] = name" will ensure that the assignment happens starting from the first element of the vector to the last, remembering that only this does not solve your problem. The line "if (Soc[i] != null)" ensures that "null is not shown".

  • Awww worked! But I just didn’t understand the null at the end of the vector. The null in the icing of the vector I understood, as it sums one with zero it guards at position 1 of the vector. But this null in the last position I didn’t understand...but thank you.

  • The vector will not always be completely filled in, for example, vc informs 9 Strings starting with "C" so no value has been assigned to the last element of the vector. This null at last position is only the default value of type String. Search for default or default values.

1

to remove the null you just need to change the time when the variable total is incremented:

    for(int c = 0; c < soC.length; c++ ){
    System.out.println(" Digite o seu nome:");
    nome = sc.next();
        if(nome.charAt(0) == 'C')
        {
        soC[total++] = nome;
        }
    }
  • I did but it didn’t work out that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.