• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array counters  RSS feed

 
Austin smit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey im in a java class and we are currently on arrays. I need to read a line from a file like "abababababababaaabbb" that is 10 groups of 7 questions. I just need a direction of how i would go about a counter for A and B.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you let us know what is your thinking on the problem? How far have you gone with the solution ?
 
Shea Orr
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you're asking here, could you be more specific?
Also, take a look at the String class in the API, it may help
[ November 16, 2008: Message edited by: Shea Orr ]
 
Austin smit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what i have so far. I really dont know if its the right direction. But its as if "ABABABAAAABBBBAAAAA" is broken up into ten groups of 7 questions in each. (total of 70 a,b's... questions) In one of the 10 groups there is 7 questions that will be separated into 4 sub categories... 1223344 like so. ex. the first goes into subgroup 1. the second and thrid question go into subgroup of two and so on.

[ November 17, 2008: Message edited by: Austin smit ]
[edit]Add code tags and edit indentation. CR[/edit]
[ November 17, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks promising. I haven't been through it thoroughly, but there is a problem. You are creating a new array every time there is a new line in the input; you will end up with only the last line split into parts, and even that will be inaccessible because you have declared it as a local variable.

How about creating a List and putting each array into it? I have never tried a List<String[]> so I don't know whether it would work.

And your solution is dependent on there being 70 (or more) letters in each line in the file.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you are incrementing the loop twice; you have i += 7 in the () at the top and i++ at the end of the loop. That will cause errors.
 
Austin smit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay so i see that this may not work. Im thinking of starting another way cause if i keep this code i will run into errors later. What would be an ideal solution to start for breaking it up into ten groups of 7 questions?
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have problems if you get to the last line which has length 0.
Change the while to read

String line;
while ((line = input.nextLine()) != null && line.length() >= 70) . . .

What will happen is that the next line is assigned to line, then it is tested for being null, then it is tested for length >= 70. You need the inner () to promote the = operator which has a lower precedence than !=. That version of the loop will stop if any line is found < 70 long, even if there are later lines 70 characters long. The call to nextLine() is moved into the while() part.

This is a bit of awkward-looking syntax, and the exact placement of () is crucial, but (if I have written it correctly) it does work.

Only read the next paragraph if you are feeling brave:

An alternative would be to compare the line with a regular expression. You could probably use "[ab]{70,}"
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you doing with the String s at the end? You have declared it as a local variable, so it will vanish at the end of the for loop, but you appear not to use it.
 
Austin smit
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so im kind of new and your syntax is somewhat confusing. I somewhat understand what your saying but say the line is under 70 characters which is possible. would that still work? and Is there a simple way to do this that is more understandable for a rookie programer like myself?
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen if you have a line of 69 characters is that your counting will drop off the end of a String and you will suffer an Exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!