Count the number of characters in a String?

Asked

Viewed 1,607 times

-1

I need to count the total number of characters of a String when the word is typed. I would like to know how to create the code, I believe it is a.

public class ExercicioDeString {
    public String transformarParaMaiusculo(String texto) {
    return texto.toUpperCase();
}

public String transformarParaMinusculo(String texto) {
    return texto.toLowerCase();
}

public String contarLetras(String texto) {

}

}

I tried using . lenght(); but could not.

Erro Erri2

  • Need to be a contact while the person is typing? Why only use the text to count.lenght();

  • I’m trying to do this but accuses an error, I will update the code and show the images, for better understanding.

  • And it doesn’t have to be while the person is typing, only after the already typed word.

  • 1

    I reversed your post, because with each Dit you are changing the problem. As I already answered, it was a type error and an error in Return. When you edit a question by changing the direction, invalidate the answers given, when you post the new problem separately

  • a blz, sorry.

2 answers

9

Basic error of type.

If you want to resume a number in the function, it’s public int or public Integer and not public String

public Integer contarLetras(String texto) {
    return texto.length();
}

In addition the return in your code.

Already evade the question, but note that you have the primitive int and the class Integer in Java, depending on the intention you need to see which is best for each case.

Primitive only accepts numbers, class is nullable.

  • Really lack of attention from me, but no . lenght continues to accuse something... see the new image.

  • Just to understand that you will only not use Return when the method is of type Static void.

  • 3

    @Juliovieira you typed wrong, is length and not lenght

  • Wow, that careless, that’s right, thank you!

  • Pity that you marked the right answer that was fixed only after mine, but the important thing is that solved (you can only mark one)

  • 1

    @Juliovieira the answer of Bacco is quite complete, apart that it speaks of a basic difference of the Integer and the int, of course I could extend the subject, but I would avoid the focus. Congratulations Bacco, I am happy to see that there are still a few good users in order to bring the answer and also add technical explanations to those who want to become a real programmer and understand how it actually works with what works.

  • 4

    @Marques has a history with date and time, his 2 issues came after mine, including the one that fixes the wrong length (only after I commented). I agree it’s good that you fix it, but your original one was nowhere near solving the problem, that’s why I mentioned it. - I do not think it bad that you have fixed, I just think it is important that the author of the question understand how the system works to be fair and to leave the site better organized.

  • 1

    Guys, sorry about the trouble I caused, I’m a beginner here, but I thank everyone who helped me!

  • @Marques again, nothing against your post. I’m just explaining to the author understand the sequence of things. Your post is correct and now more complete so ta ok. It is not by the points, it is only for the author to evaluate well in futras occasions

  • @Marques would not think it fair for you to remove yours. For me now that yours is correct I see no problem in accepting. Jaja I’ll give a clean comments here to leave mess in the posts that evades the question.

  • 1

    I was wrong to modify the question with the images, I should have put the images already starting, but I’ll be aware when asking a new question!

  • 1

    @Juliovieira instead of preferred images always put text, code photos is something very complicated to test ;)

Show 7 more comments

1


If only the string size:

 suaVariavel.length();

The error in your code is first that you are not returning or displaying the value, second, the data type must be integer, not string, so your function looks something like:

public int ContaCaracteres(String texto){
   return texto.length();
}
  • I’m trying to do this but accuses an error, I will update the code and show the images, for better understanding.

  • 1

    The hard part is that after your Edit was basically the same thing I had already answered. Qq way got more complete now (only the length that is typed wrong).

  • 1

    Really lack of attention from me, but no . lenght continues to accuse something... see the new image.

  • If I use int or Int gives another error, I’m using Integer, it’s working, I’m testing for a test class by Junity too...

Browser other questions tagged

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