• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mimicking a do-until loop

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick response: no! That's not how for loop works.
If you want to iterate Index times, you use:
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't say more until I see SSCCE.
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about a do loop?
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jonathan Caldwell
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


The answer is: yes.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic