This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Array counters  RSS feed

 
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.
 
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 ?
 
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 ]
 
Marshal
Posts: 60192
188
  • 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: 60192
188
  • 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: 60192
188
  • 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: 60192
188
  • 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: 60192
188
  • 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!