• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Flowcontrole with an assertion

 
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
 
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
 
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.
 
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!
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
Space pants. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic