This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Flowcontrole with an assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Flowcontrole with an assertion" Watch "Flowcontrole with an assertion" New topic
Author

Flowcontrole with an assertion

Madhu repala
Greenhorn

Joined: Jun 10, 2007
Posts: 16
Hi ranchers,

Whats happening in the following program, why my assert stmt is not working.......

public class Threads implements Runnable {
private int x;
private int y;

public static void main(String[] args) {
Threads that = new Threads();
(new Thread(that, "first")).start();
(new Thread(that, "second")).start();
}

public void run() {
for (int i=0;i<10;i++) {
x++;
y++;
assert(x!=y): "x=" + x + ", y = " + y;
System.out.println(Thread.currentThread().getName() + "x=" + x + ", y = " + y);

}
}
}


//If the assert condition false then it should throw an AssertError am i correct


Please try this and give reply
thanks in advance
Charandeep Singh
Ranch Hand

Joined: May 06, 2007
Posts: 57
Hi Madhu repala

Make sure that when you run the program you have your assertions enabled
java -ea Threads

This will work fine then
by default assertions are not enabled you have to enable them

Thanks
Charandeep
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi charandeep!!!


I am working in Eclipse please
can you say me in order to enable Assertions
where i must write java-ea....
please show me the exact way ie in which window
and all!!!It will be realy kinda of you.


Thanks in advance!!!
Charandeep Singh
Ranch Hand

Joined: May 06, 2007
Posts: 57
Hi Dhwani

An Advice
If you are really looking forward to SCJP certification then use more of command line for compiling your java progs and notepad as your editor.
This will clear a lot of your fundas and help you with some basic issues.

How ever if you still want to compile using Eclipse which I would not recommend Right click on the java file.
Click on Run->Run->Arguments Tab
Over there in VM arguments place -ea and it should work

Cheers
Charandeep
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks Charandeep,
first for showing me how to use assertions in Eclipse,and
for your advice
i will definately try to work using
notepad as editor.
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 140

How ever if you still want to compile using Eclipse which I would not recommend Right click on the java file.
Click on Run->Run->Arguments Tab
Over there in VM arguments place -ea and it should work

Cheers
Charandeep



Hi Charandeep,

I am using MyEclipse 7.5 and the run menu or button has no "Arguments" option under it?
I can get the assertion error message at commandline but not in MyEclipse?

Thanks!

Nigel
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Nigel please Dont Wake The Zombies, this thread is 2 years old, start a new topic for your question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
Consider Paul's rocket mass heater.
 
subject: Flowcontrole with an assertion
 
Similar Threads
Thread help please!