Results 1 to 7 of 7
  1. #1
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default How to exit the program by typing "exit"?

    How to exit the program by typing "exit"?

    this is the code that i came up with:

    Java Code:
    import java.io.*;
    
    public class GetCharacterMethodX
    {
        public static void main(String Laythe[])
        {
            System.out.println("Enter any character or Enter 'exit' to exit");
            BufferedReader DataIn = new BufferedReader(new InputStreamReader(System.in));
            String StringX = "";
           
            while ( StringX != "you can type anything here, just dont leave it empty" )        
                {
                if (StringX != "exit" )
                {
                    try
                    {
                        StringX = DataIn.readLine();
                    }
                    catch (IOException E)
                    {
                        System.out.println("Error!");
                    }
                    
                    char   charX[] = StringX.toCharArray();
                    
                    try
                    {
                        System.out.println("The First Character you have entered is: " +charX[0]);
                    }
                    catch (Exception E)
                    {   
                    }
                }
                else
                {
                    System.exit(0);
                }
            }
        }
    Last edited by Laythe; 08-19-2009 at 08:34 AM.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    compare strings with .equals() not == or !=
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default

    as angryboy told before using equals(),just trim it and call equals method like this below

    StringX.trim().equals("exit")
    Ramya:cool:

  4. #4
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you angryboy for the idea and thank you RamyaSivakanth for the hint ;)
    I tried all the possibilities using trim(), equals(), == and != but the program exit befor i can type anything do you have any idea?

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    805
    Rep Power
    6

    Default

    Hi,
    Just compare your code with my code and u can find the differnce where u are going wrong.If u ahve any problems ,I will explain you.

    import java.util.Scanner;
    import java.io.*;
    Java Code:
    public class Test
    { 
        public static void main(String[] args) throws Exception
    	{ 
       		// TODO, add your application code 
            System.out.println("Enter any character or Enter 'exit' to exit");
            BufferedReader DataIn = new BufferedReader(new InputStreamReader(System.in));
            String StringX = "";
            System.out.println("you can type anything here, just dont leave it empty");
            while ( !StringX.trim().equalsIgnoreCase("exit"))        
            {
                    
                        StringX = DataIn.readLine();
                    
                    
                    char   charX[] = StringX.toCharArray();
                    System.out.println("The First Character you have entered is: " +charX[0]);
    
                    
            }//while
    	}//main
      } //class
    Ramya:cool:

  6. #6
    Join Date
    Aug 2009
    Posts
    21
    Rep Power
    0

    Default

    BufferedReader DataIn = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("you can type anything here ");
    String StringX = "";
    do{
    StringX = DataIn.readLine();
    }while(!StringX.trim().equalsIgnoreCase("exit"))

    so when u type in the new line it will automatically exit
    but u should type only exit in the new line because it reads entire line
    :) javadeveloper

  7. #7
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you very much guys the problem is solved i realy apreciate your help :D
    Peace

Similar Threads

  1. Replies: 6
    Last Post: 05-18-2010, 05:11 PM
  2. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  3. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  4. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 03:42 AM
  5. How to exit the program..
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

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