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
Click For Summary

Discussion Overview

The discussion revolves around using an ArrayList in Java to count and store the lengths of multiple lines of input text. Participants explore how to implement this functionality using a while loop and a sentinel value for input termination, while also considering alternatives to arrays due to limitations in prior knowledge.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes a need to input multiple lines and output their lengths, suggesting the use of a while loop with a sentinel value to stop input.
  • Another participant proposes a code snippet but questions how to display the lengths of all lines instead of just the last one entered.
  • There is a suggestion to store line lengths in an array or Vector, but one participant indicates they have not yet covered arrays.
  • A participant expresses a desire to enter an unlimited number of lines, leading to a discussion about using a Vector or an ArrayList instead of a fixed-size array.
  • One participant suggests using an ArrayList, explaining its dynamic sizing and how to add and retrieve lengths from it.

Areas of Agreement / Disagreement

Participants generally agree on the need to store the lengths of all lines entered, but there is no consensus on the best data structure to use, with differing opinions on arrays versus ArrayLists and the implications of fixed versus dynamic sizing.

Contextual Notes

Some participants express uncertainty about their familiarity with arrays and the limitations of fixed-length structures, which affects their ability to implement the proposed solutions.

Who May Find This Useful

Individuals interested in Java programming, particularly those learning about data structures and input handling in console applications.

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:

Similar threads

  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
55
Views
7K
Replies
2
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K