• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Flowcontrole with an assertion

 
Madhu repala
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nigel please Dont Wake The Zombies, this thread is 2 years old, start a new topic for your question...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Similar Threads