aspose file tools*
The moose likes Beginning Java and the fly likes I implemented runnable.... I think... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I implemented runnable.... I think..." Watch "I implemented runnable.... I think..." New topic
Author

I implemented runnable.... I think...

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

I have this project that I'm working with someone on. I'm worried that my code (which implements runnable) looks nothing like his. I'm thinking I did it wrong....

here it is:



[EDIT]And the following now works because of some other nice ranchers....


I think I didn't understand the interface well enough to do this..... I need help.


When you do things right, people won't be sure you've done anything at all.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18127
    
    8

Well, yeah, you did implement Runnable. No doubt about that. That's a perfectly good implementation of Runnable. But, this code here:

It's a lie. That's what you're missing, I think, you don't actually create any new threads. So somewhere you'll need to create new Threads which use those Runnable objects, and start those threads.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

I didn't mean to lie.....

I just guess I dunno how to use the interface correctly. It says in the API that:
void run()When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
The general contract of the method run is that it may take any action whatsoever.


I guess I just don't know what I'm doing.

how does JVM know I want a thread? I mean, without extending Thread if that's possible, which was not a part of the spec of the project.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

Alright... I thought I haf it figured out, but it's not working the way I'd like....



When I type 0 to exit the app, it gives an array index out of bounds runtime error....

I'm still not sure I've done this right. I would have liked to loop through, get all the inputs and output each to a new thread and start them all at once. Maybe name them on the fly too.... can I do that?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
Try moving the prompt to enter numbers to the end of the loop.

And have a look at this thread from last year. Note the strange-looking

while (!(name = input.nextLine()).equalsIgnoreCase("stop")) . . .

syntax. Count the number of round brackets/parentheses and see whether I have balanced them at all. Go through that complicated bit of code, work out what it does, work out why you need an extra pair of () round the = sign. It might look strange, and as I told RL your teacher will never believe you if you tell him you worked it out for yourself.

But it does work. It may allow you to use a while loop instead of a do-while loop, in many cases.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
Janeice DelVecchio wrote:I didn't mean to lie.....
He never said you were lying. He said the program was lying.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

Campbell Ritchie wrote:Try moving the prompt to enter numbers to the end of the loop.

And have a look at this thread from last year. Note the strange-looking

while (!(name = input.nextLine()).equalsIgnoreCase("stop")) . . .

syntax. Count the number of round brackets/parentheses and see whether I have balanced them at all. Go through that complicated bit of code, work out what it does, work out why you need an extra pair of () round the = sign. It might look strange, and as I told RL your teacher will never believe you if you tell him you worked it out for yourself.

But it does work. It may allow you to use a while loop instead of a do-while loop, in many cases.


Alright, I think you're confusing me with RL if you think this will be a "complicated bit of code"

Ahem.... the extra set of parenthesis is for order of operation. The order of that line is....

Dear JVM,
1. Find the nextLine from the input scanner
2. When you get that, set 'name' equal to that.
3. Figure out if it's the same as "stop"..... go ahead and ignore the case.
4. Reverse the boolean value of #3
5. Continue on while true.
Thanks

As for my application, if you're gonna dredge up the thread , I may as well post what I handed in.




I think I did a good job of making sure the program was not lying anymore. I washed its mouth out with soap.

Thanks, Campbell, for the pointer on avoiding the do/while.... and bringing my dead and unanswered thread back to life.
There are 2 do/while loops in my code. I understand why it's important to know how they differ from a plain old while loop. I don't understand why people shy away from using them. I would like to wager most things SHOULD run at least once before terminating the loop.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36498
    
  16
You're welcome
Yes, most things ought to run at least once, but you can usually fiddle it so the continuation condition comes to true at the beginning of a while loop. So do loops are used much less than while and for loops.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I implemented runnable.... I think...
 
Similar Threads
Power Strings
ArrayList index out of bounds? Prime numbers?
Need help with Prime number Loop and with boolean statement
Finding a prime factor