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

  • Context: Java 
  • Thread starter Thread starter Nothing000
  • Start date Start date
  • Tags Tags
    Counter Java
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
15 replies · 3K views
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:
Physics 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
 
Could you help me write that with an array then?
 
Do you know the maximum number of lines the user might enter? Arrays are of fixed length.

- Warrens
 
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?
 
Oh, MAXIMUM number. I could just choose a large number that I would never use, right?
I will never use more than 10 lines.
 
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
 
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:
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: