Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: System exit command is not working properly

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    England
    Posts
    2
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default System exit command is not working properly

    I have been staring at this program for a while and can't get it to work. I would appreciate any assistance that you can provide.

    Problem: System.exit(0); is not working. IDE gives me an error that I don't understand. System should exit when mouse is pressed inside of rectangle.

    Program description: Program is supposed to open drawing window and then draws rectangle. Program loops until mouse is pressed inside of rectangle (Mouse press marked by red hash). System is supposed to close when condition is true.

     
    import element.*;  // opens element library
    import java.awt.Color;  // opens color library
     
    public class DrawHashMarks {  // opens class
     
    	/**
    	 * This class draws a set of hash marks.  
    	* @Walton
    	*/
     
    	public static void main(String[] args) { //opens main method
     
    		DrawingWindow d = new DrawingWindow();   //opens drawing window
    		Rect stop = new Rect (50,50,30,30);  // draws rectangle
    		Pt pressPoint;  // sets variable for presspoint
     
    		d.draw(stop); //a button for stopping 
    		d.setForeground(Color.red);  // set color for hash mark 
     
    		while (true) { // potentially infinite
     
    			pressPoint = d.awaitMousePress(); // mouse is now down
    			if (stop.contains(pressPoint));  // leave loop 
    			drawHashAt(d,pressPoint); // calls drawHashAt method
    			d.awaitMouseRelease();  // mouse is now up
    		} // close loop	
     
    		System.exit(0);
     
    	} // close main method	
     
    	public static void drawHashAt (DrawingWindow d, Pt center) { // opens method
    	// pre: d is a valid drawing window, center is a valid point 
    	// post: draw a hash mark centered at center in drawing window d
     
    		d.moveTo(center);  //move to center of hash mark 
    		d.move(-10, 0);  //move left 
    		d.line(20, 0); //draw horizontal axis 
    		d.move(-10, -10); //move up 
    		d.line(0, 20); //draw vertical axis
     
    	} // opens drawHashAt method 	
     
    }  // close class

    Thanks for your help.
    LR Walton


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: System exit command is not working properly

    Just because you don't understand an error doesn't mean you shouldn't post it here.

    Anyway, two immediate problems to spot are:

    while (true) -- Your loop is an infinite loop as you have nothing inside which could break out of it
    if (stop.contains(pressPoint)); // leave loop -- even if that did what it was meant to, it wouldn't work because you've closed it down with a semi-colon instead of a curly brace.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    LRWalton (March 21st, 2012)

Similar Threads

  1. Applet not working properly
    By kookevk in forum AWT / Java Swing
    Replies: 1
    Last Post: February 3rd, 2012, 12:29 AM
  2. Scanner not working properly.
    By kookevk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 25th, 2012, 09:14 PM
  3. Java Child process cannot execute properly for telnet command
    By mamunbu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 25th, 2011, 07:43 AM
  4. Problem with directly and indirectly access system command
    By silentbang in forum Java Theory & Questions
    Replies: 0
    Last Post: January 29th, 2011, 10:34 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM