Results 1 to 7 of 7
- 08-19-2009, 08:25 AM #1
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.
- 08-19-2009, 09:01 AM #2
- 08-19-2009, 10:08 AM #3
as angryboy told before using equals(),just trim it and call equals method like this below
StringX.trim().equals("exit")Ramya:cool:
- 08-19-2009, 12:23 PM #4
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?
- 08-19-2009, 01:09 PM #5
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 } //classRamya:cool:
- 08-19-2009, 06:04 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 21
- Rep Power
- 0
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
- 08-19-2009, 08:32 PM #7
Similar Threads
-
"Could not find the main class: comparisonDemo.class. Program will exit."
By ziisrick in forum New To JavaReplies: 6Last Post: 05-18-2010, 05:11 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
"Jumble" or "Scramble" Program
By Shadow22202 in forum Java AppletsReplies: 8Last Post: 04-30-2008, 03:42 AM -
How to exit the program..
By coco in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks