How can I use ArrayList to count the characters in multiple lines?

In summary: WarrenYou would create an ArrayList like so:ArrayList lengths = new ArrayList();lengths.add(line.length); // adds the line length to the end of the list lengthslengths.get(enter a line number here); // returns the length of the line at the specified index of the list, for example if you place 3 in the parantheses then it returns the length of the thrid line, provided there is a third entry in your arraylist.
  • #1
Nothing000
403
0
Hello. I am writing a program, but I can't figure out how to do something. I want to do something like this:

Sample input:

abcdeft
01234567
asfgSample output:

The length of line 1 is: 6
The length of line 2 is: 7
The length of line 3 is: 3
 
Last edited:
Technology news on Phys.org
  • #2
So I want to input any number of lines and then I want to give the length of each one of those lines. I want to write it with a while loop and use a sentinel value so it knows when I want to quite entering lines of text. Once I enter the sentinel value the program then outputs the length of each line.
 
  • #3
Should I do it like this:

final String SENTINEL = stop;
Scanner scan = new Scanner(System.in);
String line = "";
int length = 0;
int n = 0;

while( !line.equals(SENTINEL) )
{
line = scan.nextLine();
length = line.length();i
n = n +1;
}

System.out.println("The length of line " + n + " is: " + length);
 
Last edited:
  • #4
So my question is how do I get each n and length to show up (not just for the final line I enter)?
 
Last edited:
  • #5
Looks reasonable, except you'd need to put the System.out.println statement INSIDE the while loop. You want to print the length of every line, yes? Not just the last one?

- Warren
 
  • #6
chroot said:
You want to print the length of every line, yes? Not just the last one?

Yes, exactly right. But I don't want to print the data until after I am done entering the lines of text.
 
  • #7
Then you need to store the information somewhere, e.g. in an array or Vector.

- Warren
 
  • #8
We have not yet covered arrays. Is there another way to do it?
 
  • #9
Then you do not yet have the resources to write this program.

- Warren
 
  • #10
Could you help me write that with an array then?
 
  • #11
Do you know the maximum number of lines the user might enter? Arrays are of fixed length.

- Warrens
 
  • #12
No. One of the things I want to do is write as many lines as I want ( not keep the number of lines a fixed number ).

What about a vector?
 
  • #13
Oh, MAXIMUM number. I could just choose a large number that I would never use, right?
I will never use more than 10 lines.
 
  • #14
If you intend to never read more than 10 lines, then you can make an array of ten integers, like this:

int lengths[10];

You'd then modify your for loop to store the line length in lengths[n].

After you're done reading input, you'd need another for loop (from 0 to n-1) to print out the information stored in that array.

- Warren
 
  • #15
Ok, hold on. Like I said, I am not familiar with arrays. So at the top where I am declaring all of the variables I would just write:

int lengths[10];

Then in the while loop I would do:

length[n] = line.length();

Do I have to change the while loop to a for loop?
 
Last edited:
  • #16
I would suggest an ArrayList, It is similar to an Array but can grow or shrink to fit the needed size. ArrayList lengths = new ArrayList();
lengths.add(line.length); // adds the line length to the end of the list lengths
lengths.get( enter a line number here); // returns the length of the line at the specified index of the list, for example if you place 3 in the parantheses then it returns the length of the thrid line, provided there is a third entry in your arraylist
 
Last edited:

1. What is a Java character counter?

A Java character counter is a program that counts the number of characters in a given string or text. It is commonly used to analyze the length of a text input, such as a word or sentence.

2. How can I create a Java character counter?

To create a Java character counter, you can use built-in methods in the Java String class, such as the length() method. You can also use loops and conditional statements to iterate through the string and count the characters.

3. Can a Java character counter count spaces and punctuation?

Yes, a Java character counter can count all types of characters, including spaces and punctuation. However, you may need to specify which characters you want to include or exclude in your count.

4. What is the difference between a Java character counter and a word counter?

A Java character counter counts individual characters, while a word counter counts the number of words in a string. A word counter may also exclude certain characters, such as punctuation, from the count.

5. How can I use a Java character counter in my program?

To use a Java character counter in your program, you can create a method that takes a string as input and returns the character count as an integer. You can then call this method in your code whenever you need to count the characters in a string.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
765
  • Programming and Computer Science
Replies
3
Views
878
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top