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: Help with loop problem

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Help with loop problem

    I have made a game which works fine, 1 hiccup though, at the start of the game a player is asked whether he wants to be a 0 or a X ( noughts and crosses game)..

    my problem is, if i get the player to enter 0 he is automatically set to player 2 ( and X is always player 1)....

    then it asked player 2 to input there name... but then it doesnt ask player 1 for there name....

    and vis versa if i input X it will go to player 1, i enter the name but it skips player 2 and goes in to the game calling player 2 "null"

    my code is

    if (userinput.equals ("x") {

    bufferReader

    Print please enter player 1's name....

    and so on

    with a try and catch in here...

    }

    else if (userinput != "X"){

    print please enter player 2's name

    and so on..

    with a try and catch in here...

    }

    im sure there is an easy solution to this but my mind is frazzled at the moment and i need some guidance...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Help with loop problem

    Should the code ask for both players' names? Why not have separate questions for each player's name?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with loop problem

    yes, basically i want it to ask for both players name, just that i need to ask whether the player wants to be a 0 or a X

    my code currently asks for both names, but when i add in the code for adding a 0 or being a X im unsure which loop to use, if its a loop, ive done a if, statement but thats just 1 or the other, therefore i only get 1 name as the result and the other is always set to "null" which i dont want....

    System.out.println("Do you want to be a 0 or a X???");
    String userinput = input.nextLine();

    if (userinput.equals ("X")){




    BufferedReader reader = new BufferedReader(new InputStreamReader(
    System.in));
    System.out.println("Please enter Player 1's name: ");

    try {
    player1 = reader.readLine();
    } catch (IOException e1) {
    e1.printStackTrace();
    }

    System.out.println("You entered : " + player1 + " as Player 1");
    }

    else if (userinput != "X"){

    BufferedReader reader1 = new BufferedReader(new InputStreamReader(
    System.in));
    System.out.println("Please enter Player 2's name");

    try {
    player2 = reader1.readLine();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    thats the code i tried using but it doesnt work....

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Help with loop problem

    the other is always set to "null" which i dont want....
    Ask for the player's name if the value of the name variable is null.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: Help with loop problem

    Ah nice one thanks for the tag help.....

    i dont understand, why would i ask for the variable if its null?

    player 2 is always 0
    player 1 is always X

    so shouldnt there be some code that asks

    print do you want to be a nought or a cross

    if nought assign player 2

    else assign player 1

    but then i also want it to ask both players for names, this is the part where im struggling, i can get both to work separately, but asking for which 0 or X and what name u want im having real trouble with,

    thanks for your assistance! means a lot!


     
    System.out.println("Do you want to be a 0 or a X???");
    String userinput = input.nextLine();
     
    if (userinput.equals ("X")){
     
     
     
     
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    System.in));
    System.out.println("Please enter Player 1's name: ");
     
    try {
    player1 = reader.readLine();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
     
    System.out.println("You entered : " + player1 + " as Player 1");
    }
     
    else if (userinput != "X"){
     
    BufferedReader reader1 = new BufferedReader(new InputStreamReader(
    System.in));
    System.out.println("Please enter Player 2's name");
     
    try {
    player2 = reader1.readLine();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,310
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Help with loop problem

    why would i ask for the variable if its null?
    Because of this:
    the other is always set to "null" which i dont want....
    The code in post#5 needs formatting. All statements should NOT start in the first column. Add some indentations to nested statements.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Problem with loop?
    By Rilstin81 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 23rd, 2011, 07:48 PM
  2. Do while loop problem
    By kratos75 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 27th, 2011, 07:04 AM
  3. [SOLVED] for loop problem
    By javaneedhelp in forum Loops & Control Statements
    Replies: 3
    Last Post: October 9th, 2011, 11:25 AM
  4. While loop problem
    By ak120691 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:09 PM
  5. Little Loop Problem
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 17th, 2009, 05:40 AM