1

I need to make a JTextArea that takes in a text document of votes, which are just 'y' and 'n', using a BufferedReader given to me by my professor; and it needs to count the 'y' and 'n' characters within that first row and for each row after. He gave the class a hint that an array would be useful, but I'm not sure how to do this and any help would be greatly appreciated. Thanks!

Edit: This is not really a homework assignment, but I need to know how to do this by tonight so I can actually be able to implement it for my final, which is tomorrow.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • This task sounds suspiciously pointless, since the number of characters in rows of a `JTextArea` will be different for different declared numbers of columns, as well as how big/small the GUI is stretched or compressed. Does your professor have any idea what s/he is doing? – Andrew Thompson May 12 '11 at 02:11
  • 1
    @Alex: "i need to know how to do this by tonight" It is counter-productive to mention your time schedules. It will not only *not* get answers faster, but will also lower the chance of getting an answer at all! – Andrew Thompson May 12 '11 at 02:14
  • See I'm kind of new to this site my friend recommended this site and i thought it would be a good idea. He is very knowledgeable but he does give us pointless tasks. It seems confusing that he would ask this of us. My thoughts were the same because JTextArea's width is variable. – Alex Manley May 12 '11 at 02:18
  • 1
    The tasks may only _seem_ pointless until you understand more. Cf. _The Karate Kid_. – trashgod May 12 '11 at 02:29
  • 1
    @trashgod: (grumbles) That always irritated me. Would it have killed Mr. Miyagi to say "This task will increase speed, strength & reflexes. Also, *I'll* get my cars shined & *you* can keep one!"? – Andrew Thompson May 12 '11 at 09:29
  • to add to the confusion - I fail to see how an _array_ would help here, that's the last thingy I would think of – kleopatra May 12 '11 at 10:31
  • Guys what would be the code to get the text in a single row? Hope not a complex one. – Boro May 12 '11 at 11:17
  • 1
    @Boro: I'd consider a line-by-line [filter](http://en.wikipedia.org/wiki/Filter_(software)), as shown below. – trashgod May 12 '11 at 12:29
  • The other thing I wonder is how does the number of columns in a text area relates to a number of characters it can fit in a row? As having this you can calculate what fits what not plus you need to consider word wrapping etc. – Boro May 12 '11 at 12:58
  • @trashgod @Alex Manley @Andrew Thompson @kleopatra @mKorbel please find [here my question](http://stackoverflow.com/questions/5979795/how-to-calculate-the-number-of-rows-and-columns-in-each-row-a-text-takes-in-a-j) where I tried to split the problem into steps. I hope this will help to solve this interesting issue, which grinds my gears too. :) – Boro May 12 '11 at 14:49

1 Answers1

2

You'll find suitable examples in the article How to Use Text Areas.

Addendum: A thoughtful observer asks,

What would be the code to get the text in a single row?

BufferedReader may be good choice, as suggested in this example that copies stdin to stdout:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((s = in.readLine()) != null) {
    // process s
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045