Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-19-2009, 09:25 AM
Laythe's Avatar
Member
 
Join Date: Mar 2009
Posts: 28
Rep Power: 0
Laythe is on a distinguished road
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:

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 09:34 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-19-2009, 10:01 AM
angryboy's Avatar
Senior Member
 
Join Date: Jan 2009
Location: Javaland
Posts: 742
Rep Power: 2
angryboy is on a distinguished road
Default
compare strings with .equals() not == or !=
__________________
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-19-2009, 11:08 AM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 589
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
as angryboy told before using equals(),just trim it and call equals method like this below

StringX.trim().equals("exit")
__________________
Ramya
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-19-2009, 01:23 PM
Laythe's Avatar
Member
 
Join Date: Mar 2009
Posts: 28
Rep Power: 0
Laythe is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-19-2009, 02:09 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 589
Rep Power: 1
RamyaSivakanth is on a distinguished road
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.*;
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-19-2009, 07:04 PM
Member
 
Join Date: Aug 2009
Posts: 21
Rep Power: 0
britto_bicsjohn is an unknown quantity at this point
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-19-2009, 09:32 PM
Laythe's Avatar
Member
 
Join Date: Mar 2009
Posts: 28
Rep Power: 0
Laythe is on a distinguished road
Default
Thank you very much guys the problem is solved i realy apreciate your help
Peace
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Could not find the main class: comparisonDemo.class. Program will exit." ziisrick New To Java 4 02-10-2009 01:32 PM
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan") soc86 New To Java 2 01-24-2009 07:56 PM
the dollar sign "$", prints like any other normal char in java like "a" or "*" ? lse123 New To Java 1 10-20-2008 08:35 AM
"Jumble" or "Scramble" Program Shadow22202 Java Applets 8 04-30-2008 04:42 AM
How to exit the program.. coco New To Java 1 08-01-2007 06:56 PM


All times are GMT +2. The time now is 10:39 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org