Simple debugging

J

js_dev

Simple debugging

How do I see the values of variables in a Java Program?

(a) System.out.print(), System.out.println() etc.

(b) JOptionPane.showMessageDialog(null,"a message");

(c) If you are using an Integrated Development Environment, enter the
expression or variable name in the "expressions" or "evaluate"
box.

(d) Write the values of the variables to a file and open the file later
for inspection


(a)
public class Foo{
String s;
int i;
.....
public void somefunc(){
.....
i = (p*n*r/100);
System.out.print(p);
System.out.print(i); //prints on same line as p
System.out.println(i);//print i on a new line
.....
}
.....
}

(b)

import javax.swing.JOptionPane;//import needed

public class Foo{
String s;
int i;
.....
public void somefunc(){
.....
i = (p*n*r/100);
JoptionPane.showMessageDialog(null,i);
//will show a simple message box with the
//value of i and an OK button
JoptionPane.showMessageDialog(null,"the value of i is:" + i);
//will show message box with the
//text "the value of i is:" and the value of i
.....
}
.....
}


(c)
It is advisable to spend at least two full days coding Java programs
using notepad and javac, the Java compiler to get used to the
code-build-run sequence.

However, it is advisable to have a good look at at least one of a few
IDEs(Integrated Development Environment) on the third day and see their
capabilities so that if you are stuck you know where to look for help.

Every IDE has a debugging mode and in that mode, there is usually a box
provided for entering expressions to watch while debugging. Put your
variable name there. To find out how to get into debugging mode you
will have to consult the help of the IDE.

Search for 'debug' or 'add watch' or 'watch' in the find/search box of
the help to get the help information.


(d)
import java.io.FileWriter;//import needed

public class Foo{
.....
public void appendlog(String s){
FileWriter fw =
new FileWriter("c:\\temp\\debug.txt",true);
// true tells that strings must be appended,
// i.e. open the file in append mode.
fw.append("|".charAt(0));
for (int i=0; i <s.length();++i){
fw.append(s.charAt(i));
}
fw.flush();
fw.close();
}
.....
}

then, in your method, wherever you feel that the value of a variable
must be observed, before and after an operation performed on the
variable put an appendlog().
For example,

appendlog("myvar before Myobj.myfunc():" + myvar);
myvar = MyObj.Myfunc(i,"A");
// sample function which returns a value
// which you want to put in myvar
appendlog("myvar after Myobj.myfunc():" + myvar);

You can then open the file after your program run to analyse what went
wrong by seeing the values of the variables at different places (
assuming, of course, that you have put calls to the appendlog()
fucntion intelligently in your program).

Note: you will have to add a try/catch block to the code that accesses
the files as follows

import java.io.FileWriter;//import needed

public class Foo{
.....
public void appendlog(String s){
try{
FileWriter fw =
new FileWriter("c:\\temp\\debug.txt",true);
// true tells that strings must be appended,
// i.e. open the file in append mode.
fw.append("|".charAt(0));
for (int i=0; i <s.length();++i){
fw.append(s.charAt(i));
}
fw.flush();
fw.close();
}catch(Exception e){System.out.println("an Exception
occurred,text:" + e);}
}
.....
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top