Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!

Richard Warner

+ Follow
since Feb 06, 2015
Richard likes ...
Eclipse IDE Java
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Richard Warner

Essentially, turning inside out which is nested in which?

I think that would work! Putting the ifs inside the for should work, though I do have two fors at the moment, I won't once they're outside the first if.

Thank you so much! That should solve it! And you're super quick!!!
5 years ago
Here's what will probably turn out to be a silly "Why doesn't this work?" question. It concerns a small method which is part of a card game.

I'm trying to check a condition from a section of an array, without a predetermined number of times any given call to this method will require a check. How much of the array I'm checking could vary greatly.

Is it at all possible to nest a for loop yielding a variable number of boolean && conditions inside an if? Am I just missing the right bracketing or is this nesting (if that's the word) even possible in the language?

To clarify, below is broken code. Compiler isn't letting me accomplish this goal as I envision it.

If I need to approach this differently, that's great, just so I know I am in fact, barking up the wrong tree and not just missing a few brackets somewhere.


5 years ago
Ah, thank you all so much!

I will rewrite this as you say, as a JFrame with a JPanel within it. I had gone one or the other but not both. I had not heard about Swing's timers, I'll learn how that works to implement this.

And the word "this" did indeed fix all my dead thread code. With just that keyword, it all started refreshing as I had hoped, but since I'm headed down a dead-end, outdated path, I'll start fresh!

Folks, this is huge for me! (Since my version 1.7, when I first encountered this about two weeks ago, I've made basically a new version of the game every night by 0.1 increments, up to 2.9) This was a huge leap forward to know that I should revamp this and approach a whole new way!
5 years ago
I am really struggling with getting an applet program to repaint outside of minimizing and then bringing to the foreground again my applet window (which it does repaint, and quite neatly, revealing the underlying mechanics at work as intended).

But resizing after each input is getting really, really old in testing my program. The compiler gets angry whenever I try to call any of the methods to repaint from events in my primary object (A Selection Class object named selector) trying to make me turn even more of my program bits and bobs static (actions I had taken to satiate demands from other classes) or - more disconcerting - screaming that I need to input a Graphics object parameter, but when I do so, all kinds of everything becomes broken. It gets even angrier and crazier on the rabbit-trails elsewhere. Goal: I would like the program to repaint every so often on its own.

*afterthought* maybe I could prebuild each Graphics object in another class extending Graphics class then treat them as blocks of image? Is that even possible? Trying to hold out on doing a load of custom artwork for the time being.

I read that there is no way to directly call the paint() method, yet, that is what repaint() seems to do, from everything else I read and see. But repaint() and I are NOT friends. Yet.

But for me it doesn't seem to work any which way I approach this thing. I don't want to post too much code initially, but I really don't know where to start. Tell me if you need more info. I'm thinking lines 51 of Flank Class should (indirectly) invoke the method at line 61 (the following Blocks of code Classes Deck and Card are there just to show you what lines 62/63 call.). But it acts like it doesn't. It probably doesn't and I have no clue why others can use it in threads to get what they want and I can't. Repaint() doesn't take parameters. Maybe there's a command in there to resize my window to some arbitrary size and back again like I've been doing manually? I'm desperate to get this working.

I've tried rewriting the whole damn thing as a JPanel, as a JFrame (before I learned those two objects are just brothers with different layout managers) and then as a Canvas (also before I did any research on what it was). In the end, Applet is as good as any for what I need, so it seems from what I read, anyway.

I've mimicked and modified, mixing and matching successful techniques from what I've seen others do and all of my versions fail in the same brutal way. Maybe I just don't understand Graphics objects, though from everything I read they seem straightforward. It is possible I don't fully understand threading, but that seems straightforward to me as well (thread just branches the program much like using conditionals, but flitting back and forth between each branch's execution priority, right?). I have yet to implement the other thread pathway, which I'm guessing I'll need separated out to manage some animations, etc.

Thank you to those willing to take the time to look at this! The input I've received here is always really, really helpful.
5 years ago

This makes a lot more sense to me why to make setters and getters. Since they were doing exactly the same thing, I thought, "Well, that's goofy, one's just a private version of the other." With them actually creating isolating barriers it all suddenly makes sense. One guy making tutorials online was just letting Eclipse generate those generic placeholders, so that's what I was doing. Now, with it better, I see I've got an index out of bounds exception from my other stuff right away, which is a good thing. Thank you once again! And I came here for the "buts," of course!
5 years ago
Again, for the sake of other greenhorns, Pile class is now:

This works great, too! Thank you again, Tony!
5 years ago
I have some plans for add, remove, etc. From the main or probably from another class. Will the other approach you prefer allow the same, but just requires Getters and setters? I haven't programmed enough yet to really see the value in private over public. But on your advice, I'll go that other way if your foresight can save me valuable time reprogramming much later.
5 years ago
Oh! That makes sense. THANK YOU SO MUCH! I've been fighting that for days!

For future reference for any other Greenhorns making the mistake of doublebagging and forgetting that methods go poof with everything inside them when they're done, here's how my new Pile class looks (and it works!):

Thanks again!
5 years ago
I think I may be struggling simply to correctly reference (from a separate class) objects which have been built by each other.

I made an abstract class called Card and many Sub Classes of it, each with their own (nearly identical) constructors.

I made a LinkedList<Card> class called Pile, which extends LinkedList<Card> and itself has its own constructor, which populates (with randomly chosen cards) a LinkedList to a size dictated by an int parameter depth.

I made a Deck class which builds two Pile[] arrays. They are given constants which gives them a number of rows. Lines 14 through 35 are just there to give the deck a stagger effect on both ends. (I don't think that's the problem, but included just in case Note: int Flank.ROWS = 6; and int FLANK.COLS = 6; are public static final from the main class, and the constructor called here is simply fed those two as parameters where applicable (rows, cols, etc.)

When ran, various constructors from Subclasses of Card ARE being called randomly as intended; however, whether they are being catalogued correctly, I'm less confident.

The error comes in my main class (called Flank, the game's name) which asks inside a thread (a thread induced after performing these above actions):

I am expecting a card. I get "null".

Anticipating that this error may be (as before) that I'm asking for an Object and expecting a String,

returns a NullPointerException error. The Card abstract class has a method in it:

And all non-abstract Card subclasses' constructors have as their first line. Where x is their unique card name. I being shown no compiler errors (well, no red errors. Excepting a few yellow unused for future)

I at first tried this toString method being overridden by each Subclass (and there's a mess of them, lots of copy-paste) as suggested by Eclipse's mouse over F2 box thing.

Didn't work. Then tried a single, inherited method from Card class as above.

Up until now, I had been fighting NPEs like mad, but now that the program as a whole finally runs without any, I thought I had it licked. Until I went to actually test it.

I think I understand what a NPE is, but in practice... I can't seem to anticipate them. Or again, I am likely expecting something to do something it was never intended to do.

MID-POST UPDATE: obsoleted buildDeck() by moving the two separate Array definitions and calls to the main class to tidy up the Deck class having a single constructor Deck() and having Deck() look almost exactly like buildDeck() did. All functions otherwise the same, same error. I'm throwing all sorts of irrelevant Hail Marys at this thing. Now allows allyDeck[0].peek() or axisDeck[0].peek() without compiler errors (no need to type "deck.") but still shows null on syso. Not sure why I thought that would help, but it is cleaner, I guess.

Thank you folks and your brilliant minds willing to try to make heads or tails of my mess! I feel I am at a huge impasse that keeps me from gaining any ground on this game!
5 years ago
I'm brand new to Java too, fellow Greenhorn! Since I've so recently learned it, maybe this can help: I find some symbols we may know from english have ever so subtle differences in Java. "=" here is not really "equals." "X=x+y" should read more like "X is now set to the value of x+y." Or "from now on, plug in x and y whenever I mention variable X." Though that's possibly backwards of the actual order or how something might be processed, it helps for sussing out later "what in the world was I doing with x."

Java does this value setting using the original value so much, folks often will even write this formula in a slightly abbreviated format (I scoffed at first, but it is way easier to type!) If you're using the same x both as your new value and also as part of how you got to this new value: "x += y" says the same thing as "x = x + y"

The double equals, "==" is closer to what might be anglicized as "equals." and seems to be used more for testing things (like in if statements) while the single equals is used primarily in "primitive" assignment. (Plugging values or other variables into the various different species of variables). At least, that's how I'm conceptualizing it myself so far, others more familiar with it will have clearer conceptualizations.
5 years ago
Plus, like you say, I've been generating all Cards. Even polymorphed through randomly calling their subclasses, they'll all register as Card Objects when tested, not the variety of sub-Class Objects! Thanks, that's much clearer, I think I can tackle this better.
5 years ago
I was thinking that since my abstract class Card was the only one containing a toString method that toString would be referencing each inheriting card's class name, then seeing if it contained that class of object. I was expecting contains() to give me a report of true, but as you say, I must be trying to use it for something other than its intended purpose. So, my problem is that I'm generating a String (hopefully a unique String of each card I'm testing) but contains() is testing for an Object? Even if the name is identical, different data types are apples and oranges, I see. Sorry all. Thanks for the feedback.
5 years ago
Hey folks! Is this a bad "print to console" approach or bad "list populating" approach or something else completely?
Lines 9 and 10 are having a little "he said, she said" moment and line 3 is what I hear crying to me from the console.

Oh, and I know it's not shown here (trying to be terse with the code here) but my abstract class Card (which is...
I guess... "nested" for now) is the only buckaroo with a toString method I have, and the compiler seems to be finding it just fine.

5 years ago
My apologies, all for the cutesy Pseudo Code / Red Herring faux pas (read the HowToAskQuestionsOnJavaRanch!)

Thanks folks! I'm going the route with all the card subclasses with this project now that I've found some tutorials on LinkedList, Stack, etc. and hope I'll find more built-in stuff to make this work. (I think I've been trying and failing to reinvent the wheel a bit) Based on a few tutorials, I assumed the built-in stuff was much more limited than it is because I didn't have any idea there was such a vast repertoire of commands to manipulate each (I thought calling any one did exactly what the tutorial before me showed and no more). I finally found F2 focus on Eclipse by accident and found I could read up on all the bits pertinent to each one right inside Eclipse! I'll have to tackle learning enums on my second project. It's nice to know enums would work for this project (implementation depending) if I get in a pinch with the route I'm taking now.
5 years ago