Tocas
Tocas

Reputation: 3

How to progressively count the number of characters in a String Array? -Java

I'm a 1st College student with no prior coding experience. I'm working on a text editor for my second assignment. One of the many features of this text editor is the ability to count the number of characters in a string, and, since this is a string array, there are multiple of them. Here's what I've got thus far:

int maxSize=100;
int [] nChars = new int[maxSize];
String [] linhas = new String[maxSize];

    for (int i=0; i<maxSize;i++){
        nChars[i]=lines[i].charAt(i);
        System.out.println(lines[i]+ " - " + nChars[i]); \\print the line and the respective number of characters

Any help is apreciated, I'm pretty much stuck.

Upvotes: 0

Views: 90

Answers (1)

Raven
Raven

Reputation: 3526

The functions you are searching for is simply the .legth() function which will return the numbers of characters in a String.

String myString = "testString";
System.out.println("The string has " + myString.length() + " characters");

// output: The string has 10 characters

If you have an array of strings then just iterate through it and use the length() function on each element.

Greetings Raven

Upvotes: 1

Related Questions