• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Flowcontrole with an assertion

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nigel please Dont Wake The Zombies, this thread is 2 years old, start a new topic for your question...
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic