This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Mimicking a do-until loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mimicking a do-until loop" Watch "Mimicking a do-until loop" New topic
Author

Mimicking a do-until loop

Jonathan Caldwell
Greenhorn

Joined: Aug 30, 2012
Posts: 21


is it possible to use == here?

My code is compiling and the window is being created, the index is filled...but im not getting the output.

I am basically trying to "do until"

but java doesn't have that

I was trying to use a while loop...but I cant think of how that would work?

I need the loop to loop exactly "Index" amount of times.


Thanks
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Quick response: no! That's not how for loop works.
If you want to iterate Index times, you use:


The quieter you are, the more you are able to hear.
Jonathan Caldwell
Greenhorn

Joined: Aug 30, 2012
Posts: 21
yes I figured out that it doesn't work that way,
but I am not getting the desired result with

i<index
or
i<=index
and especially not
i==index
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Can't say more until I see SSCCE.
Jonathan Caldwell
Greenhorn

Joined: Aug 30, 2012
Posts: 21
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

And the issue is...?
Your for loop goes useableIndex times and does what it's written inside. Can't connect it to your first question, but for loop seems ok if useableIndex has appropriate value.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

First point, never, ever do this, even in throwaway code:
If you are getting an exception, you have no way of knowing. If you're not sure how to handle it yet, or you can't be bothered doing anything complicated, at least print the stack trace.

Secondly, that's not an SSCCE - it's not something we can pick up and run. And so you need to tell us what the output is, and what you were expecting. "not getting the desired result" leaves us guessing.

Beyond that, without trying to unpick everything at the same time - do you really mean to have two loops? It seems as if you check that the scanner has more content...but then you read several lines in sequence that might not be there.

The other thing you want to check is that useableIndex is what you think it is - print out the value (or step through it with a debugger) to check.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
What about a do loop?
Jonathan Caldwell
Greenhorn

Joined: Aug 30, 2012
Posts: 21
Firstly, Thanks for all the responses.

yes

oops I don't know how to handle the exception yet.
(but if I am reading from a file, and the information is being read in...I can assume its working?)
(for testing purposes of course...not quite sure what a stack trace is?)

as far as SSCCE, even if I posted all of my code...some stuff is being taken from servers etc.

couldnt be run from your machine unless you some how infiltrated...




yes I mean to have two loops.
the permissions are listed in the same order in the file as the users list in the combo box.
so I am trying to use the selected index to control the amount of times the loop loops...

so the permission corresponding to the index will be isolated.

I tested the indexes.

they are being received correctly.

but for some reason (maybe its the two loops conflicting?)

its not looping index amount of times.

and I was trying to figure out how I could make this work in a do loop...but I am still stumped there?


thanks again.
hope this makes more sense this time.


Jonathan Caldwell
Greenhorn

Joined: Aug 30, 2012
Posts: 21
ok a little more info also...maybe this helps?
(or makes it more confusing)
:/


if the first item in the combo box is selected the selected index is 1
and so on
(right now there are 5 in the box)
if I print them out to the text field
1=1
2=2
3=3
4=4
5=5

but when im using the selected index as the loop determinant;

1=5
2=4
3=3
4=4
5=5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

Jonathan Caldwell wrote:I don't know how to handle the exception yet.

You don't have to know how to handle it, but you need to know if one is happening. That's why you (at minimum) print the stack trace - so you know you need to do something. THEN you can figure out what that would be.
Jonathan Caldwell wrote:(but if I am reading from a file, and the information is being read in...I can assume its working?)

nope. Not if you are swallowing the exceptions

Jonathan Caldwell wrote:not quite sure what a stack trace is?

it's basically a dump of all the method calls currently on the stack. More generally, it will show you where "something went horribly wrong", and let you start to figure out how to resolve it.

Jonathan Caldwell wrote:as far as SSCCE, even if I posted all of my code...some stuff is being taken from servers etc.

You clearly don't understand what a SSCCE is then, if you think this is what we want.

Jonathan Caldwell wrote:its not looping index amount of times.

how many times is it looping? is index changing? How do you know?


I stand by the fact that System.out.printnln() is your best friend. Put it all through your code. Put it in before you enter your loop, and make sure Index is what you think it should be. Put it as the first statement in the loop, and print Index and i. put it at the end of your loop, and print them again.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jonathan Caldwell
Greenhorn

Joined: Aug 30, 2012
Posts: 21
oh man
I feel like a goofball.

sorry Matthew Brown you were right. I didnt need both loops.

its working now.

now I have
1=1
2=2
and so on.

I was making it more complicated than I need to



I need to look into SSCCE I think when I was reading (quickly) I skipped over the self contained part

oops

I will also look into stack trace.

I am new to java and new to programming in general. im not sure if I am doing things the right way... not sure if process or results are more important?

thanks everyone.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Jonathan Caldwell wrote:not sure if process or results are more important?

The answer is: yes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mimicking a do-until loop