2

Below I have a copy of my entire code for a tic tac toe program. I know its not much yet, but I'm stuck at the getting input part. I've managed to get 1 input from the user and then print it out (column), but then when I try to enter something different for row, it gives me whatever I was using for column. Any thoughts on how to fix it?

I'm just learning java, please be gentle.

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {

        System.out.println ("Please make your first move by entering a column and then a row, like this: c r \n");

        int columnGotten = 0;
        int rowGotten = 0;

        //gets your column number choice

        BufferedReader columnInput = new BufferedReader(new InputStreamReader (System.in));

        try {
            columnGotten = Integer.parseInt(columnInput.readLine());
        } catch (NumberFormatException nfe) {
            System.out.println ("If you're not going to play fair, I'm going to leave. Bye.");
            return;         
        }

        System.out.print ("Your column is " + columnGotten + "\n");

        //gets your row number choice

        BufferedReader rowInput = new BufferedReader(new InputStreamReader (System.in));

        try {
            rowGotten = Integer.parseInt(rowInput.readLine());
        } catch (NumberFormatException nfe) {
            System.out.println ("If you're not going to play fair, I'm going to leave. Bye.");
            return;         
        }

        System.out.print ("Your row is " + columnGotten);           

    }

}
2
  • You shall not create a new Reader, just use the previous reader. Commented Mar 11, 2012 at 22:59
  • I tried to do that, but it still gave me the same error (displaying the old input). Commented Mar 11, 2012 at 23:01

2 Answers 2

4

Change

System.out.print ("Your row is " + columnGotten);

to

System.out.print ("Your row is " + rowGotten);

Sign up to request clarification or add additional context in comments.

2 Comments

Don't worry. Even the most advanced devs do such mistakes. Just keep in mind whenever you copy/paste to double-check the changes. ;)
I still need to wait 5 mins, there's some kind of timeout.
2

Try input using Scanner.

Scanner sc = new Scanner();
int x = sc.nextInt();
String s = sc.nextLine();

And so on. Hope it helps.

3 Comments

I'll try this - it seems to be less complicated than using BufferedReader - do they have the same functionality?
Yes. It is more neat and less error prone. javap java.util.Scanner for more info.
Thank you both, I'll be checking this out later tonight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.