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

Thread: Help figuring out why my method isn't executing as expected.

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Help figuring out why my method isn't executing as expected.

    Hey everyone, thanks in advance for your help. I am giving a method here that is a portion of a game I am writing. If you can figure out the issue with it, I would be greatly appreciative.
    For some reason, when prompted the question about whether player is 1 or 2, the user must enter 1 or 2 three times before the program reacts. I have initiated my player type variables elsewhere. I know I don't have a catch block yet.
     
    	public void setPlayerType() throws InputMismatchException{
    	while(firstPlayerType == -1){
    	System.out.println("Will player 1 be human or computer contolled? enter 1 for human, 2 for computer");	
    	if(console.nextInt() < 1 || console.nextInt() > 2)
    		System.out.println("That is not a valid selection");
    	else
    		this.firstPlayerType = console.nextInt();
    	}// end while loop
     
    	if(firstPlayerType == 1)
    		System.out.println("Player 1 will be human controlled");
    	else
    		System.out.println("Player 1 will be computer controlled.");
     
     
    	while(secondPlayerType == -1){
    	System.out.println("Will player 2 be human or computer contolled? enter 1 for human, 2 for computer");
    	if(console.nextInt() < 1 || console.nextInt() > 2)
    		System.out.println("That is not a valid selection");
    	else
    		this.secondPlayerType = console.nextInt();
    		}
    	if(secondPlayerType == 1)
    		System.out.println("Player 2 will be human controlled");
    	else
    		System.out.println("Player 2 will be computer controlled.");
    	} //end set player type
     
    	public int getPlayerType(int playerNumber){
    		if (playerNumber == 1)
    			return firstPlayerType;
    		else 
    			return secondPlayerType;
    	}
    Last edited by Herah; October 8th, 2011 at 05:06 PM. Reason: readability


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help figuring out why my method isn't executing as expected.

    It's hard to read your code when It's not surrounded by code tags, so If you could sort that out, it would be great.
    Not quite sure what your problem is, but I'm going to guess It's because of the following:

    if(console.nextInt() < 1 || console.nextInt() > 2)
    System.out.println("That is not a valid selection");
    else
    this.firstPlayerType = console.nextInt();

    every call to console.nextInt() prompts the user for a reply,
    so when you say;
    if (console.nextInt() < 1 || console.nextInt() > 2)
    It's essentially saying:
    if(//get user input   < 1 || //get another user input > 2) //do something
    else //get another user input.

    How about you prompt the user for an input, save that input in a variable and then compare the variable with < 1 etc.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    Herah (October 8th, 2011)

  4. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Help figuring out why my method isn't executing as expected.

    Thank you! I thought that might be the problem so changed my if statement, but forgot to change the set statement. It's good now. You're awesome

Similar Threads

  1. Having a hard time figuring out how to make my code work
    By iainnitro in forum Loops & Control Statements
    Replies: 2
    Last Post: September 6th, 2011, 07:48 AM
  2. Need help figuring out the problem (GUI)
    By Prescott in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 8th, 2011, 12:01 AM
  3. Figuring Out Object Arrays
    By bengregg in forum Collections and Generics
    Replies: 2
    Last Post: April 5th, 2011, 05:55 AM
  4. Executing Oracle Procedure
    By java_mein in forum JDBC & Databases
    Replies: 0
    Last Post: May 14th, 2010, 02:41 PM
  5. Replies: 0
    Last Post: March 28th, 2010, 01:27 PM