Results 1 to 11 of 11
  1. #1
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default How To Get Java To "Die" [Console App]

    Hi Guys,

    How do I get Java to "die" - in other words simply close the console and stop - don't trigger any of the code underneath it.

    Thanks,

    EdenC

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    Stopping java is one thing. Closing the command prompt console is something else.
    You can exit a java application by calling: System.exit(0);

    don't trigger any of the code underneath it.
    Can you explain what you mean by this?

  3. #3
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks - seems so easy now you've said it!

    Don't worry about what I said about not getting any of the code underneath to run - I meant the same thing but explained it in a way that isn't applicable (am used to scripting, not programming).

    Thanks,

    EdenC

  4. #4
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    On a related note:

    I have an if statement where if the user says that they want to play the game (by inputing "yes") then the game will run, and an else statement that will then close the game (using system.exit) if they type anything else.

    The problem is, the else statement always seems to run:

    Java Code:
    /**
     * Guess Your OS!
     * @author Eden Crow
     *
     */
    import java.io.*;
    public class MainClass{
    	public static void main(String[] args) throws IOException {
    		String input;
    		String OS;
    		
    		System.out.println("Welcome to Guess Your OS! - Read to play?");
    		InputStreamReader isrPlayGame = new InputStreamReader(System.in);
    		BufferedReader brPlayGame = new BufferedReader(isrPlayGame);
    		input = brPlayGame.readLine();
    		input = input.replaceAll("\\\\n", "");
    		if(!(input.equals("Yes") || input.equals("yes"))){
    			OS = System.getProperty("os.name");
    			InputStreamReader isr = new InputStreamReader(System.in);
    			BufferedReader br = new BufferedReader(isr);
    			System.out.println("Your OS has been calcualted - now have a guess what it is!");
    			input = br.readLine();
    			input = input.replaceAll("\\\\n", "");
    			if(input == OS){System.out.println("Thank god - you know what your operating system is (" + OS + ")");
    				if(OS == "Windows Vista"){System.out.println("What a second... you have Windows Vista? (Laughs in your face)");}}
    			else if(input != OS){System.out.println("Wrong! - You thought your OS is " + input + " but is actually " + OS + "!");}
    			else{System.out.println("Error");};
    		}
    		else {System.exit(0);};
    
    		
    		
    		}
    }
    There may be some mistakes in there that aren't related to the question at hand as it is still a work in progress - I've skimmed a tutorial on input and output and then just searched the web for things that I wasn't sure about, hence why it may be terrible and utterly wrong.

    Thanks,

    EdenC

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by EdenC View Post
    Java Code:
    		if(!(input.equals("Yes") || input.equals("yes"))){
    This test is incorrect.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    This test is incorrect.

    kind regards,

    Jos
    I guessed this, but what should it be instead?

    Thanks,

    EdenC

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,534
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by EdenC View Post
    I guessed this, but what should it be instead?
    Think about it: if the input is "yes" or if the input is "Yes" your program should do something, else it should quit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Think about it: if the input is "yes" or if the input is "Yes" your program should do something, else it should quit.

    kind regards,

    Jos
    I'm sorry... I don't think I understand what your trying to get at...
    I've tried changing the program slightly, but now it only runs and doesn't exit at that stage.

    Here is the new code:

    Java Code:
    /**
     * Guess Your OS!
     * @author Eden Crow
     *
     */
    import java.io.*;
    public class MainClass{
    	public static void main(String[] args) throws IOException {
    		String input;
    		String OS;
    		
    		System.out.println("Welcome to Guess Your OS! - Read to play?");
    		InputStreamReader isrPlayGame = new InputStreamReader(System.in);
    		BufferedReader brPlayGame = new BufferedReader(isrPlayGame);
    		input = brPlayGame.readLine();
    		input = input.replaceAll("\\\\n", "");
    		if(!(input.equals("Yes") || input.equals("yes"))){
    			OS = System.getProperty("os.name");
    			InputStreamReader isr = new InputStreamReader(System.in);
    			BufferedReader br = new BufferedReader(isr);
    			System.out.println("Your OS has been calcualted - now have a guess what it is!");
    			input = br.readLine();
    			input = input.replaceAll("\\\\n", "");
    			if(input == OS){System.out.println("Thank god - you know what your operating system is (" + OS + ")");
    				if(OS == "Windows Vista"){System.out.println("What a second... you have Windows Vista? (Laughs in your face)");}}
    			else if(input != OS){System.out.println("Wrong! - You thought your OS is " + input + " but is actually " + OS + "!");}
    			else{System.out.println("Error");};
    			try {Thread.sleep(1000);} catch(InterruptedException e){};
    			System.out.println("Thanks for playing Guess Your OS! Press enter to close the game...");
    			InputStreamReader isrEndGame = new InputStreamReader(System.in);
    			BufferedReader brEndGame = new BufferedReader(isrEndGame);
    			input = brEndGame.readLine();
    			System.exit(0);}
    			else {System.exit(0);};
    			
    		}
    
    		
    		
    	}

  9. #9
    abhilasha is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    while comparing two strings use the method equals rather than using ==,
    since the operator == checks the whether u r refereing to the same object,
    whereas equals compares the actual strings.

    in your program
    if(input == OS) should be if(input.equals(OS))

    if(input != OS) should be if(!input.equals(OS))

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,453
    Rep Power
    25

    Default

    A comment on you coding style: Place the closing } on its own line in vertical alignment beneath the line with the opening {. Do NOT hide the } at the end of line following a statement.
    Java Code:
    System.exit(0);}
    The String class has a method that will compare Strings ignoring their case. That would save you having to compare the String against different spellings.

  11. #11
    EdenC is offline Member
    Join Date
    Aug 2011
    Posts
    19
    Rep Power
    0

    Default

    Thanks all!

    I think I have now fixed all the problems (thanks to your help).

    Really appreciate it!

    Thanks,

    EdenC

    EDIT: Forget that, getting some more problems now: Java Exception Has Occured (should really have read a tutorial before trying to make this)
    Last edited by EdenC; 08-24-2011 at 07:26 PM.

Similar Threads

  1. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  2. Java Console's "Install Shortcuts" grayed Out
    By a_gunslinger in forum New To Java
    Replies: 0
    Last Post: 03-01-2011, 04:21 PM
  3. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 03:03 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •