• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use the debugger in Java?  RSS feed

 
Ry Shu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I added a Breakpoint here:

public class JavaApplication8 {

I added another here:

public static void main(String[] args) {

Finally, I added one more here:

Scanner input = new Scanner(System.in);


Ctrl + F5 is supposed to start the debugger, right. Mine doesn't work. When I hit Ctrl+F5, another window opens and it's called 'Class.java'. If I just try to run the code by hitting F6, the code simply runs and none of the break points are hit. This is so weird. I've never seen anything like it. Does anyone have any idea what could be wrong here?

Here is my code.

package javaapplication8;
import java.util.Scanner;
public class JavaApplication8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Celsius of your input is Fahrenheit degree ");
double myinput = input.nextInt();
System.out.println(myinput + 32 * (9/5));
}
}


I want to debug this, so I can learn how the debugger works. Also there really is something wrong with this code. It just adds 32 to whatever number I enter. The 9/5 is never hit, which is very weird.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. It just adds 32 to whatever number I enter.
That is because (9/5) is equal to 1. Because 9 is an integer and 5 is an integer, the result of (9/5) is converted to an integer before multiplying by 32. So 9/5 = the integer values of 1.8 which is 1

Try changing it to (myinput + 32 * (1.8)) and it should work better in that regard.


As for you your break points, which IDE are you using? NetBeans? Eclipse?
 
Ry Shu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just realized it's actually this:
(myinput * (1.8) + 32 )

Why is 9/5 = 1?
I don't get that part.

Sorry for not mentioning it before, I am using NetBeans IDE 8.0.2.

I still can't debug anything. If I right-click the project and click 'Debug File' it opens a new window named 'Class.java'. What the heck? Why can't I step through the code like a normal debugger? No matter how many Breakpoints I put in, when I hit F6, it just runs the code. Any idea what's going on here?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ry Shu wrote:Why is 9/5 = 1?
I don't get that part.


That's the rule in Java. If you divide two integer values -- such as 9 and 5 here -- the result is truncated to the next lower integer -- in this case 1. (Actually it might be the next integer closer to zero; try some examples with negative numbers if you want something better than my loose description.)

Next try 9.0/5.0 -- there are different rules for dividing two double values, which is what that is.
 
Ry Shu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! I've got a lot to learn. Any idea why the debugger doesn't work? Is there a setting that I can change? Or, am I just doing it the wrong way? I watched 2 videos on YouTube about the NetBeans IDE Debugger, and mine doesn't work anything like those in the videos. So weird.
 
Derik Davenport
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the debugger, I don't use the hot keys I use a Mac, so it would be different anyway. I just use the "Play" button. There are 2 of them. one is a Green Triangle pointed right. The other is a smaller green tirangle placed on top of a white and red field.

How are you enabling the break points? by clicking outside the line numbers? Do you see the red box and the pink highlighting on the line you have the break point on?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As Paul C says, integer arithmetic is rounded to the number nearer to 0. It is all in the Java® Language Specification (=JLS), but that is notoriously difficult to read.
JLS wrote:Integer division rounds toward 0.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the debugger... here's a link to the Netbeans debugger tutorial: https://netbeans.org/kb/docs/java/debug-visual.html -- I'm assuming that you didn't already find that in your web search, because it uses a different technique to start the debugger than what you said you did.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!