Results 1 to 8 of 8
  1. #1
    bradoz is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Problem with program exiting code.

    just trying to see where i have gone wrong with my code.

    Java Code:
    		System.out.println("If you would like to choose option 1 PRESS 1. ");
    		System.out.println("If you would like to choose option 2  PRESS 2. ");
    		System.out.println("If you would like to choose option 3  PRESS 3. ");
    		System.out.println("If you would like to choose option 4 PRESS 4. ");
    		System.out.println("If you would like to choose option 5  PRESS 5. ");
    		System.out.println("If you would like to choose option 6  PRESS 6. ");
    		System.out.println("To QUIT program PRESS q. ");
    		Scanner in = new Scanner(System.in);
    
    		System.out.print("Enter Option: "); // Prompts for selection choice
    		int option = in.nextInt(); // Stores input in option
    		char quitProgram = in.next(). charAt(0); //stores input if options is a character
    
    		if (option < 1 || option > 6) {
    			// Display Message to say wrong input entered
    			System.out.println();
    			System.out.println();
    			System.out.println("WRONG INTEGER PRESSED!!");
    			System.out.println();
    			System.out.println();
    			return;
    		}
    
    		if (quitProgram != 'q' || quitProgram != 'Q' ) {
    			// Display Message to say wrong character entered
    			System.out.println();
    			System.out.println();
    			System.out.println("WRONG CHARACTER PRESSED!!");
    			System.out.println();
    			System.out.println();
    			return;
    		}
    
    		if (quitProgram == 'q' || quitProgram == 'Q' ) {
    			// Display Message to say wrong character entered
    			System.out.println();
    			System.out.println();
    			System.out.println("EXITING PROGRAM!!");
    			looping = false;
    			System.out.println();
    			System.out.println();
    			return;
    		}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Problem with program exiting code.

    You tell us. What does this code do? What do you expect it to do? Have you stepped through it with a debugger, or at least added some print statements, to figure out what this program actually does?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    bradoz is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Problem with program exiting code.

    Can this be done like i am trying to do it?
    when my program runs, where it prompts me for an option, i put in an interger that works fine, but then the program waits till i enter a characters after that.
    if i enter a character straight away it crashes my program (because im guessing its expecting an integer) if i enter an interger say 8 or 9 and then enter a character, eitehr q or any other character, the program just processes the integer (and returns the "wrong integer entered" message) and loops through the program, ignoring the character code.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Problem with program exiting code.

    Like I said, have you stepped through this with a debugger oadded some print statements? What values are stored in option and quitProgram?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    bradoz is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Problem with program exiting code.

    yeh i worked out the character reading wasnt being processed, would just read the character and then jump to the next part of the integer reading part of the program.
    got a work around to put the character reading first, not sure if it is the best or cleanest way to do what i was trying to do
    (which was to display a list of options the majority of options being integers with values 1-6 or whatever and an error message returns if press a wrong integer, then also on same menu have the option if user pressed character q then it would quit the program, but also if they pressed another character it would return a message to the user. If the user chose a valid integer option the program would perform that option then loop back to the menu. so thus while selecting valid inputs the program was in a continuous loops. thus the need to put in an option to quit the program lol)

  6. #6
    bradoz is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Problem with program exiting code.

    System.out.println("If you would like to choose option 1 PRESS 1. ");
    System.out.println("If you would like to choose option 2 PRESS 2. ");
    System.out.println("If you would like to choose option 3 PRESS 3. ");
    System.out.println("If you would like to choose option 4 PRESS 4. ");
    System.out.println("If you would like to choose option 5 PRESS 5. ");
    System.out.println("If you would like to choose option 6 PRESS 6. ");

    Scanner in = new Scanner(System.in);
    System.out.println();

    System.out.print("Do you want to QUIT program? (Press Y to QUIT or any other character to continue): "); // Prompts for selection choice
    char quitProgram = in.next(). charAt(0); //stores input if options is a character

    if (quitProgram == 'y' || quitProgram == 'Y' ) {

    System.out.println();
    System.out.println();
    System.out.println("EXITING PROGRAM!!");
    looping = false;
    System.out.println();
    System.out.println();
    return;
    }

    System.out.print("Enter Option: "); // Prompts for selection choice
    int option = in.nextInt(); // Stores input in option

    if (option < 1 || option > 6) {
    // Display Message to say wrong input entered
    System.out.println();
    System.out.println();
    System.out.println("WRONG INTEGER PRESSED!!");
    System.out.println();
    System.out.println();
    return;
    }
    Last edited by bradoz; 05-22-2012 at 03:34 PM.

  7. #7
    bradoz is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: Problem with program exiting code.

    What it does now is what i want it to, except it requires the user to input a character during each loop to continue, instead of not coming up with the prompt and just waiting until the user inputted a character when they were ready to exit

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Problem with program exiting code.

    Can you post the console contents from when you execute the program and add some comments to it saying what is wrong with what it is doing and how you want to change what it does?

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Problems with exiting application
    By JohnPringle83 in forum New To Java
    Replies: 14
    Last Post: 05-06-2011, 05:35 PM
  2. Cancel button for exiting app
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-13-2011, 07:20 PM
  3. Exiting a while loop using a String?
    By Isshin in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 11:33 PM
  4. exiting a while loop with a string
    By Aldius in forum New To Java
    Replies: 3
    Last Post: 12-10-2009, 10:38 PM
  5. Exiting on menuSelected action
    By benra in forum New To Java
    Replies: 8
    Last Post: 10-13-2009, 04:58 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
  •