aspose file tools*
The moose likes Beginning Java and the fly likes Problems with 2nd Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with 2nd Class" Watch "Problems with 2nd Class" New topic
Author

Problems with 2nd Class

Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
The problem I am having is once the code compiles down to where I call my second class. It does not quite call ... and I am not sure why because I am not having any compiling errors. Thank all



Then here is the second class

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

What do you mean, "It does not quite call"?

Keep in mind that ItDoesntWorkIsUseless(⇐click) so you need to TellTheDetails(⇐click).
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
Once the compiler gets down to line 38 on the first class. it just stops. nothing else happens.

It does not call the method that is listed on line 38 ... so nothing happens besides line 35 on the first class
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It's not the compiler. The compiler compiles source code to bytecode. It doesn't execute code. It's the runtime that executes it.

You claim that nothing happens after line 35. I claim that is false. Add println() calls after line 38 and at various places inside Calc2.loop(), so that you can see what's happening.

As a side note, "class1" is a really lousy variable name for a variable of type Calc2. I know, "oh, this is just a small test program, etc. etc.," but it's already confusing, and if the program gets bigger or you get into the habit, you're just going to be setting yourself up for lots of frustration.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
No I did nothing happens after 35. I say the only thing that shows up lastly is like 35.

When it hits like 38 ... which is suppose to be Class Calc2.

And the class1 is just to see if its going to work. Variable can be changed after the code is working. I have been trying just to get this done for 6 straight hours, and my nerves are a little on edge :/



[Thumbnail for ouput.PNG]

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johnathan Conkle wrote:No I did nothing happens after 35.


I maintain that that is false. The JVM continues to execute code. What you are telling me is that it is not creating a new Calc2. And you are telling me that it is not invoking the loop() method in that new Calc2 object.

You are telling me it is not executing this line in the loop() method:

nor this one:

nor these:


And so on.

I maintain that these claims that you make are false. But I'm not asking you to just believe me. I'm asking you to add print statements so you can observe for yourself what is happening.

This is how we debug. We make predictions on how the code will behave, and when it doesn't match our expectations, we sometimes have to observe the actual behavior at a detailed level to see where our assumptions are wrong. If you're not willing to check your assumptions, you'll be beating your head against the wall forever.

And the class1 is just to see if its going to work. Variable can be changed after the code is working.


I know this. And yet I made the suggestion anyway. Better to get into the habit of taking a few seconds to come up with meaningful identifiers in the first place.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I have been trying just to get this done for 6 straight hours, and my nerves are a little on edge :/


I understand that, believe me. And I'm not trying to jerk you around here.

I'm trying to lead you down the path toward finding out what the problem is, and also teach you some tools to help you solve these problems on your own in the future.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
Indeed.

I tried adding a print statement after the

Calc2 class1 = new Calc2();
class1.loop();


No Change.

When I placed the print statement on line 11 of the class Calc2 it did print .. but anything after that would not.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Take a look at these lines, and then take a look at your other uses of Scanner:



I see one definite problem, and one potential problem. The potential problem is that you should only ever use one Scanner in a given application. I'm not sure if that will cause you any problems here, but it could.

The actual problem has to do with your apparent assumption that it should just continue on after nextDouble() without any help from the user? (Although if the other one is indeed causing a problem, then even if the user does his part, it may look like nothing is happening.)

Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
Okay I was able to get the loop to go through and everything to work thank you for helping me out. But now for some reason. When the loop hits it returns 0 no matter what the user inputs.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johnathan Conkle wrote:Okay I was able to get the loop to go through and everything to work thank you for helping me out. But now for some reason. When the loop hits it returns 0 no matter what the user inputs.


Cool. Glad you worked it out!

As for the "always 0" problem, the same suggestion applies: Add some println() calls so you can see which code paths are being executed and what the various values are along the way.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
But the problem is its a loop I can't just throw a random print line ... because theres not gonna be a value. The whole loop has no value. Cant you look over the code and check where it may be ...? cause if I could of found it lol, I think I would of already.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
this is the problem I had before the loop was not working.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johnathan Conkle wrote:But the problem is its a loop I can't just throw a random print line


Yes you can.

... because theres not gonna be a value. The whole loop has no value.


Who said anything about a "value of the loop"?

Your loop is executing various steps. So you can print out whatever you want to indicate which of those steps are being executed. You can see how many times the loop body is being executed, and which if/else paths are being taken.

And it's performing computations and setting variables, so you can see what the results of those computations are along the way, what values are being assigned to the variables.

Cant you look over the code and check where it may be ...?


I could, but if it were my code and I was confused about why I wasn't seeing the expected behavior, I wouldn't waste much time on that before I started printing stuff out or using a debugger, and I'm certainly not going to expend more effort on somebody else's code than I would on my own.

However, if you add the print statements and still can't figure out what's going on, then post the latest code, and tell us what you saw vs. what you expected to see, and somebody will help you from there.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
Well if I knew what the problem was ... I would have fixed it is the problem.

Its okay. Ill figure it out sooner or later.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
Its frustrating I have been trying to fix this problem for days.
Johnathan Conkle
Greenhorn

Joined: Mar 25, 2013
Posts: 10
Lol can we not edit posts? .... Well anywho.

I was able to get the loop to work if the user puts in any from 120,000+ or 96000+ or higher .. but anything lower then 95999 will not work and I am looking at the line but it seems no different from the rest. I guess thats why I am getting so frustrated lol.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

put in a println at the beginning and end of each method - specifically the loop method if that is the one that isn't working. I would literally have them print "entering loop method of Calc2" and "Leaving loop method of Calc2"

Assuming the problem is the loop on line 18 of the Calc2 class, put in a line that prints the value of i right after line 18, before your "if (num >= 120000.00)" line. I'd also print out what num is. I would also add a a println after you update num with what the new value is.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11311
    
  16

Johnathan Conkle wrote:Lol can we not edit posts?.
after some period of time, posts are locked from editing. If people go back and edit things, it can make the rest of the thread even more confusing, as people are answering questions that are gone or referring to lines of code that aren't there.

If there is some kind of problem with a post, contact a moderator (bartender, sheriff, etc) and they will review your request. If you have new, updated source code, just make a new post with the current code and ask your question in relation to that.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Johnathan Conkle wrote:Well if I knew what the problem was ... I would have fixed it is the problem.


That's the whole point. You don't know what the problem is. And neither do I. So you need to do some work to find out what the problem is. If it were my code, the way I would do that is, as I suggested many times, to add a bunch of print statements so we can observe what's actually happening.

We write the code. We make assumptions and expect it to behave a certain way. The code behaves differently than we expect. We see step 1, and then there's a black box of steps 2 through 10, and then where step 11 should be we see something else, or nothing at all. So obviously our assumptions about what 2-10 does was wrong. At least one of those steps is doing something different than we expected. How do we figure out which steps?

We could just stare at the code and hope we figure it out. That's a valid approach, up to a point. But at some point the same assumptions that led us to write the wrong code in the first place are going to continue to lead us to mis-predict its behavior when reading it. So if a little starting and mental or pen-and-paper walk-through doesn't work, we need to watch what the code is doing. One simple way to do that is to print out which steps are executing and what their results are.

So my question to you is: Why are you so resistant to following my suggestion? What will you do when you get stuck on your next program? You could just throw your hands up and ask somebody else to find it, like you're doing here, or you could use the technique I'm trying to teach you, so that you can find the problem yourself.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with 2nd Class