Java Thread

Troncoso

VIP Member
Ahh. Okay. Well, it's actually very simple. Once you've found a set of data that sends it into an infinite loop, hard code those numbers.

after you do that, on the line where your while loop starts, simply click in the white space to the left. It should create a little circle. That's your breakpoint.
bluej01_05.png


After that you simply run your program. When it gets to that while statement, it will stop, bring up the debugger window.
bluej117.gif


here, it'll show you the values of each variable as they are. From here, you press the "Step" button to go to the next line, after you press it each variable will update, if they have been changed. If you keeping doing this, you'll see why the values never meet the while loop condition.
 

Cromewell

Administrator
Staff member
Ah, well, I never use the Math class for random, there is actually a Random class you could use.

I don't know how well you know your way around Bluj, but it has a pretty extensive debugging mode. You need to find a set of data that gives you an infinite loop and hard code those numbers into your program. After that, you can set a break point at your while loop and keep stepping through it to see why it's not exiting the loop.
Math.random() is actually creating an instance of the random class, it's just short cutting it and allows you to call nextInt, nextByte, nextXXXX...
I meant teach me how to set up BlueJ so I can figure out why it's looping.
BlueJ is an option for a debugger, if you are using Eclipse as an IDE, it has a built in debugger you can use.
 

ScottALot

Active Member
Math.random() is actually creating an instance of the random class, it's just short cutting it and allows you to call nextInt, nextByte, nextXXXX...

BlueJ is an option for a debugger, if you are using Eclipse as an IDE, it has a built in debugger you can use.

I don't know what an IDE is... we're just learning how to create programs, so terms that might be common for people who learned to code outside of school.

EDIT: I know it means Integrated Development Environment... but that still means very little to me. It's like how the CIA is the Central Intelligence Agency; so, what, do they store all of the intelligence they can find into one center?
 
Last edited:

Cromewell

Administrator
Staff member
Think of using notepad and command line to build your program or Visual Studio (an IDE). Sure you can use notepad and enter commands into a prompt window, but having an IDE is easier and faster. They do other things as well, such as code completion (i.e. type ahead), syntax highlighing. And they typically have a built in debugger so you can step through your code and watch what it's doing :)
 

ScottALot

Active Member
Think of using notepad and command line to build your program or Visual Studio (an IDE). Sure you can use notepad and enter commands into a prompt window, but having an IDE is easier and faster. They do other things as well, such as code completion (i.e. type ahead), syntax highlighing. And they typically have a built in debugger so you can step through your code and watch what it's doing :)

Oh, so Eclipse, BlueJ, Notepad and more are different IDEs... got it!
 

Cromewell

Administrator
Staff member
The only thing I'd say about BlueJ is it's intended for use as a learning tool for object oriented programming in java to beginners. There's nothing wrong with it but you won't see it used outside of the education system.
 

Troncoso

VIP Member
The only thing I'd say about BlueJ is it's intended for use as a learning tool for object oriented programming in java to beginners. There's nothing wrong with it but you won't see it used outside of the education system.

That's 100% true. That's all BlueJ is designed for. It really is excellent, though, for seeing how programs are working. The inspector, especially.
 

Cromewell

Administrator
Staff member
Some projects will provide eclipse plugins, but if you just want to write code it has a lot of built in functionality that will help. Learning how to use the debugger will help a lot.

One thing to remember with eclipse is by default it will build your code automatically when you hit save so you don't have to search for how to compile.
 

Troncoso

VIP Member
I'm having some casting issues. I'm getting mixed results as to whether I can cast a super class to a sub class. Some say yeah you can, some say Java won't let you.

well, my code is simply:

Code:
public void addGame(Game game, String typeGame){
        if (typeGame.equals("sugGame")){
            if (game instanceof SuggestionGame)
                SuggestionGame gameToAdd = (SuggestionGame) game;

But, when I try to compile, it tells me that the last line is not a complete statement....

Edit: Nevermind. I figured out a better way to do this that actually worked.
 
Last edited:

Troncoso

VIP Member
Got another question:

Is there some function in Java that would allow me to trigger a method when the respective object is returned by another method call?

Something like this:

Code:
public void method() {
    Test test = sample.getSomething();

where getSomething() will return a Test object.

When this happens I would like the test object to execute a method. Can I do that?
 

TrainTrackHack

VIP Member
When this happens I would like the test object to execute a method. Can I do that?
Not the exact way you're thinking, no. What exactly are you trying to achieve? There's probably a better way to do whatever you're trying to do.
 

Troncoso

VIP Member
Not the exact way you're thinking, no. What exactly are you trying to achieve? There's probably a better way to do whatever you're trying to do.

Well...that's what I'm trying to achieve...whenever I execute a method and a certain object is returned, I want that object to execute it's own method.

What's going on, is, the user picks from a list of options, when they enter their choice, the object that is specified by that choice is returned, and printed on the screen via the toString() method.

Well, not only do I want that option to print it's toString method, but I also want to call another method. The object is created before hand, so putting it in the constructor wouldn't help.
 

TrainTrackHack

VIP Member
What's going on, is, the user picks from a list of options, when they enter their choice, the object that is specified by that choice is returned, and printed on the screen via the toString() method.

Well, not only do I want that option to print it's toString method, but I also want to call another method. The object is created before hand, so putting it in the constructor wouldn't help.
Well you could just call the methods in the code where you're returning the object. I don' know how exactly you've set up your code (if you could post some, that would be helpful) but you could have something like

Code:
Object methodThatReturnsObject() {
     Object object = theOneObjectYouWant;
     System.out.println(object);
     object.someOtheMethodYouWantToCall();
     return object;
}
So in this example methodThatReturnsObject would be triggered when the user enters a choice, and of course you'll need some code to make sure that theOneObjectYouWant corresponds to the choice the user made.
 

Troncoso

VIP Member
I'm a failure. Haha. What I did, was simply create a method within my superclass and then did an override in my subclasses so they all called what they were suppose to.

Instead of just printing the object, I set it equal to a variable, then printed it, then called that method.

Sorry to take you around like that, but at the very least, I got the idea from you.
 

mihir

VIP Member
I am planning to start android development. I need to get a book. Do you have any suggestions.I do not expect you to have used your suggested books, but if you know that the publisher/author is a good one that will do, also if some one else you know has used that book, that will also do.
 

mihir

VIP Member
Couldn't wait that long. Just drove out and got this for $7. Need to start with it this night.

db2kp.jpg
 
Top