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

  • Thread starter Thread starter Nothing000
  • Start date Start date
  • Tags Tags
    Counter Java
AI Thread Summary
The discussion centers around creating a program that accepts multiple lines of input and outputs the length of each line after a sentinel value is entered. The user initially attempts to implement this using a while loop but struggles with storing and displaying the lengths of all lines. It is suggested that the user should place the output statement inside the while loop to display each line's length immediately, but the user prefers to display all lengths after input is complete. Since the user is unfamiliar with arrays, the conversation shifts to using an ArrayList, which allows dynamic resizing. The ArrayList can store the lengths of each line as they are entered, enabling the user to retrieve and print the lengths later. The final suggestion involves declaring an ArrayList, adding line lengths to it, and using methods to access the stored lengths. This approach aligns with the user's requirement to handle an unspecified number of input lines without being constrained by a fixed array size.
Nothing000
Messages
403
Reaction score
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
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.
 
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:
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:
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
 
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.
 
Then you need to store the information somewhere, e.g. in an array or Vector.

- Warren
 
We have not yet covered arrays. Is there another way to do it?
 
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:
Back
Top