Can someone explain to me what this is?

I have been asked to code a tic-tac-toe board using an array which I have done, but the brief specifies using standard input, and I have no idea what that is, but I'm guessing does not include changing the values of the array manually in the code view!

import javax.swing.JOptionPane;

class nx {
     public static void main(String[] args) {
          int[][] area;              // declares  array of integers

          final int O = 1; //denotes a nought
          final int X = 2; //denotes a cross
          
          area = new int[3][3];      // allocates memory for 9 integers
            
          area[0][0] = O; // places first figure
          area[0][1] = O; // places second figure
          area[0][2] = X; // etc.
          area[1][0] = X;
          area[1][1] = O;
          area[1][2] = X;
          area[2][0] = O;
          area[2][1] = X;
          area[2][2] = O;
          
          //read each line individually, printing 'true' if noughts (1) have won, 'false' if crosses (2) have won, or nothing otherwise
          
          if (area[0][0] == 1 && area [0][1] == 1 && area [0][2] == 1){
            System.out.println("true");}
          else if (area[0][0] == 2 && area [0][1] == 2 && area [0][2] == 2){
            System.out.println("false");}
          
          if (area[1][0] == 1 && area [1][1] == 1 && area [1][2] == 1){
            System.out.println("true");}
          else if (area[1][0] == 2 && area [1][1] == 2 && area [1][2] == 2){
            System.out.println("false");}
          
          if (area[2][0] == 1 && area [2][1] == 1 && area [2][2] == 1){
            System.out.println("true");}
          else if (area[2][0] == 2 && area [2][1] == 2 && area [2][2] == 2){
            System.out.println("false");}
          
          if (area[0][0] == 1 && area [1][0] == 1 && area [2][0] == 1){
            System.out.println("true");}
         else if (area[0][0] == 2 && area [1][0] == 2 && area [2][0] == 2){
            System.out.println("false");}
          
          if (area[0][1] == 1 && area [1][1] == 1 && area [2][1] == 1){
            System.out.println("true");}
          else if (area[0][1] == 2 && area [1][1] == 2 && area [2][1] == 2){
            System.out.println("false");}
          
          if (area[0][2] == 1 && area [1][2] == 1 && area [2][2] == 1){
            System.out.println("true");}
          else if (area[0][2] == 2 && area [1][2] == 2 && area [2][2] == 2){
            System.out.println("false");}
          
          if (area[0][0] == 1 && area [1][1] == 1 && area [2][2] == 1){
            System.out.println("true");}
          else if (area[0][0] == 2 && area [1][1] == 2 && area [2][2] == 2){
            System.out.println("false");}
          
          if (area[0][2] == 1 && area [1][1] == 1 && area [2][0] == 1){
            System.out.println("true");}
          else if (area[0][2] == 2 && area [1][1] == 2 && area [2][0] == 2){
            System.out.println("false");}
          
         
     }
}

That is the code I have at the moment, but what I want to do is use standard input to enter the values into the array, rather than typing them in myself

Can anyone assist?

Can someone explain to me what this is?

I have been asked to code a tic-tac-toe board using an array which I have done, but the brief specifies using standard input, and I have no idea what that is, but I'm guessing does not include changing the values of the array manually in the code view!

That is the code I have at the moment, but what I want to do is use standard input to enter the values into the array, rather than typing them in myself

Can anyone assist?

Standard input means prompting the user to enter in the data and storing it in the array. This would not be hard coded in the program:

area[0][0] = O; // places first figure
          area[0][1] = O; // places second figure
          area[0][2] = X; // etc.
          area[1][0] = X;
          area[1][1] = O;
          area[1][2] = X;
          area[2][0] = O;
          area[2][1] = X;
          area[2][2] = O;

Instead, this array would be filled in by asking the user a question and filling in the array, based on his/her response. For example, ask the user for a row, a column, and whether they'd like an 'X' or an 'O'. Or set up a loop that goes through all the rows and columns and ask for an 'X' or 'O' for that row/column.

RE: the scanner

I have never used that function before, but having read through the link you provided, am I right in thinking it's possible to read data from a Notepad file in this way?

I ask because I have been provided with a txt file to use 'if I so wish' - the contents are below

XOX
XXO
XOO

So I could use the scan function to input these values into the array? Or am I barking up the wrong tree here

The only other way I could think of doing it is to use the JOptionPane, but in my view, that is a bit of a long winded method and I'd prefer to avoid it if possible

Input From File:

Scanner sc = new Scanner(new File("PATHTOFILE"));

Input From Console (On Runtime):

Scanner sc = new Scanner(System.in);

OK thanks, I'll give that a bash and if I encounter any problems I'll post back here

OK thanks, I'll give that a bash and if I encounter any problems I'll post back here

Best of luck buddy! and don't forget to set this thread as solved :D

OK, I managed to screw that one up royally!

I need more help with the scanner and/or file reader functions, both throw out this error at me

File: C:\Users\Jamie\Documents\Napier Stuff\Dr Java\nx.java [line: 16]
Error: C:\Users\Jamie\Documents\Napier Stuff\Dr Java\nx.java:16: cannot find symbol
symbol : method File(java.lang.String)
location: class nx

I can only guess I'm putting it in the wrong place, or I need to add something that I haven't thought about?

The code I entered for the FileReader, then the Scanner:

FileReader ox = new FileReader(File("nx.txt"));

Scanner sc = new Scanner(new File("nx.txt"));

I know that by entering either of those codes, the functions aren't really doing anything, but I wanted to see if they would actually read the file before I went any further

OK, I managed to screw that one up royally!

I need more help with the scanner and/or file reader functions, both throw out this error at me

File: C:\Users\Jamie\Documents\Napier Stuff\Dr Java\nx.java [line: 16]
Error: C:\Users\Jamie\Documents\Napier Stuff\Dr Java\nx.java:16: cannot find symbol
symbol : method File(java.lang.String)
location: class nx

These functions aren't in your nx class. Java provides them. You need to add the appropriate import.

import java.util.Scanner;
import java.io.File;
import java.io.FileReader;

Thanks, that solved that one, but now I'm going to show my complete lack of knowledge and ask how to get that information into the array?

I've found a lot of tutorials that explain 1D arrays and 2D arrays from code, but nothing about scanning into 2D from text. If you're all fed up of answering my questions, pointing me in the direction of a relevant tutorial works for me too

Thanks, that solved that one, but now I'm going to show my complete lack of knowledge and ask how to get that information into the array?

I've found a lot of tutorials that explain 1D arrays and 2D arrays from code, but nothing about scanning into 2D from text. If you're all fed up of answering my questions, pointing me in the direction of a relevant tutorial works for me too

The fact that it's going to be stored in an array is irrelevant to the Scanner. You already know how to use an array from your first post. Use the nextInt () function from Scanner . Assign it to an array element:

Scanner scanner;
// code to initialize scanner

area[1][2] = scanner.nextInt();

The above will read the next number from the file and store it in area[1][2] . Presumably you'll have a nested for-loop rather than hard coding area[1][2] into the code.

From what I understood I presume that each line in the file will have many values (probably comma separated) that need to be put in each row of a 2D array.

If this is the case then when you read the line use the split method of the String class:

String s = "a,b,c,d";
String [] array = a.split(",");

Then the array will have elements:
array[0] = a
array[1] = b
array[2] = c
array[3] = d

Afterward you can take these values and put them wherever you want

commented: Thanks for telling abt this method! :D +2

You could have easily found out about this method: split() if you looked at the java API.

You should be always consulting and searching the APIs for something that you might need.

Useful packages:

java.lang
java.util
java.text
javax.swing
java.io

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.