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 6 of 6

Thread: How can I create a global or local script terminater?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How can I create a global or local script terminater?

    I tried this,
                    System.out.println("Enter your number: ");
    		nr1 = scn.nextDouble();
    		if(nr1.equals("stop") || nr1.equals("Stop")){
    			String nr1 = nr1(double);
    			System.exit(0);
    		}
    but I need to define the String nr1 before the if-statement. How can I do this?

    Also, is there any other way to do this? As in if I write stop in any place it will shutdown the program?

    EDIT:

    I want the nr1 to be converted from a double to a string in the if-statement. I do not want it to be converted so it will work outside the if-statement, as it will only ruin the rest of my code.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I create a global or local script terminater?

    I need to define the String nr1 before the if-statement
    There is already a variable named nr1 defined that looks like it is type double.
    You will have to use a different name for the String.

    I don't know of any general way to convert a double to a String like "stop".

    Can you explain what you are trying to do?
    Why don't you use the Scanner class's next() method to read a String from the user that you could compare to "stop"?
    The Scanner class has methods that will test the datatype of what the user entered. See the methods that begin with has
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How can I create a global or local script terminater?

    Hello YourCrazyFriend !
    I want the nr1 to be converted from a double to a string in the if-statement. I do not want it to be converted so it will work outside the if-statement, as it will only ruin the rest of my code.
    There is a toString(double d) method in the Double class.
    But since you want to compare the String you will create with another("stop") in the if statement, you have to define it at the same level and before the if statement.
    Hope it helps.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How can I create a global or local script terminater?

    I don't know if I did it the correct way, but this is where I have gotten so far. This code is not working yet.
                    Scanner scn = new Scanner(System.in);
    		double nr1, nr2, answer;
    		String str;
     
    		System.out.println("Enter your number: ");
    		nr1 = scn.nextDouble();
     
    		toString(double nr1);
     
    		if(nr1.equalsIgnoreCase("stop")){
     
    			System.exit(0);
    		}

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How can I create a global or local script terminater?

    Quote Originally Posted by YourCrazyFriend View Post
    I don't know if I did it the correct way, but this is where I have gotten so far. This code is not working yet.
    No, this isn't the correct way. You should assign what the toString() method returns to the String you created (str). You can find many examples if you Google it. Here is one of them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can I create a global or local script terminater?

    This code is not working yet.
    I don't think it will ever work. You won't get the String "stop" from a double value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need Java code to be used in Selenium Script
    By anujbatta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 10:50 AM
  2. Hybrix impex script developer
    By ITpraca in forum Paid Java Projects
    Replies: 1
    Last Post: May 25th, 2011, 10:47 AM
  3. Related to Html and Java Script?
    By JackyRock in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: February 17th, 2010, 03:10 AM
  4. Problem with a waitFor() executing a script
    By Baldurian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 29th, 2010, 10:45 AM
  5. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM