• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declare counter with value outside for loop  RSS feed

 
Vincent Jenkins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to declare a counter with a value outside of a for loop?

I have a counter that will end prematurely in a while loop during various iterations and I want to pick it back up in a catch all for loop at the end

Let's say I have a while loop

while(something) {
total++
}

then after I have a for loop that I want to start at total but would rather do that then make a new counter variable.

for(total;total < 20;total++)

Is something like this possible or is this a horrible thing to want anyways?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You mean you want to run a loop, and later start a second loop starting where the first loop left off? Of course you can, but please explain more of what you want because there is probably a better way to do that.
 
Vincent Jenkins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You mean you want to run a loop, and later start a second loop starting where the first loop left off? Of course you can, but please explain more of what you want because there is probably a better way to do that.


Thanks for the welcome! Basically I'm doing a problem where passengers move from an airport security queue to one of the airport security check gates. The queue starts off with 20 passengers. Here's what I have so far:



Additional information for this problem is that there are 5 security check gates which I will differentiate in my output. Gate 1 can handle 1 person/min, Gate 2 & 3 can handle 2 people/min and Gate 4 & 5 can handle 3 people/min. They always have to go to the next available gate so the security assignment would look like this Gate #1, 2, 3, 4, 5, 4, 5, 2, 3, 4, 5
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want:


...which means to start wherever total left off.

Also, you have some bad formatting. It makes it look like the code after:


...isn't under the control of the for loop. From the while loop down should be indented; then you can clearly see you're missing a brace.
 
Vincent Jenkins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:You probably want:


...which means to start wherever total left off.

Also, you have some bad formatting. It makes it look like the code after:


...isn't under the control of the for loop. From the while loop down should be indented; then you can clearly see you're missing a brace.


Sorry ya. I had that indented when I did it in notepad and only sent part of the code >< I don't like the indenting in this intellij IDE. I think it's terrible how it forces some things for some reason. I might just use notepad++ and compile later. I appreciate the help. I learned something new about the for loop
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can endorse Notepad++ for text editor / compile Java programming.
 
Vincent Jenkins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also here's my total code. Maybe you could throw me some pointers on things I'm doing badly?

 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Java class names should start with an uppercase letter.

2) There is a lot of code in main(). Generally, main should just launch your program. This is also why all your method have to be static, which is a bad sign.

3) In the method printSec() you have what are called "magic numbers", that is, what's the significance of 7, 2, 8? In general, any number that it not patently obvious should be put into a well-named constant.

Minor: Import for Queue is never used; Lines are too long, try to keep them under 80 characters
 
Vincent Jenkins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:1) Java class names should start with an uppercase letter.

2) There is a lot of code in main(). Generally, main should just launch your program. This is also why all your method have to be static, which is a bad sign.

3) In the method printSec() you have what are called "magic numbers", that is, what's the significance of 7, 2, 8? In general, any number that it not patently obvious should be put into a well-named constant.

Minor: Import for Queue is never used; Lines are too long, try to keep them under 80 characters


So basically I should assign everything to a different class, create an instance of that class object in main and go from there?

Oh ya the code has changed again. My teacher is forcing us to use a queue and so I have a problem now actually. It was working before with the linkedlist but since Queue doesn't have a (get) function because of what it is I'm struggling to figure out how I'm going to implement printQueue now. I've tried poll() since i've read that Java is pass by val but I guess that methods still alter the original object so that doesn't work.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Also don't write such long lines; I have corrected the worst offenders by changing your // comments to /* comments */.
You also have repeated code in lines 60-90; that should be refactored into a method of its own. You would probably reduce the total amount of code by 15 lines like that. You would have to work out how to represent 4 gates in a data structure.

I also think you should not be writing any code at present. I think you should go back to pencil and paper and work out what your algorithm is going to be. Maybe you let 10 people through each time and each person goes to the gate with the shortest queue.
I believed you when you said you were using a Queue and implementing it with a LinkedList. You aren't. You have not used it as a Queue at all. You declared it a a linked list and you are using it as a linked list. You ought to declare it as a Queue, like this:-Don't use linked list: use this instead. If you were threading you would use this
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vincent Jenkins wrote:
Knute Snortum wrote:2) There is a lot of code in main(). Generally, main should just launch your program. This is also why all your method have to be static, which is a bad sign.

So basically I should assign everything to a different class, create an instance of that class object in main and go from there?


It doesn't even have to be a different class. Something like this works fine:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!