简体   繁体   中英

Print user inputs from 2D array using another class in Java?

I am trying to print user inputs in another class in Java. I have made a chessboard which asks the user to input strings on the board, and then, when these strings are printed on screen, I would like the output to be "You have placed piece [name] at coordinate [coordinate]". I am trying to do this in another class rather in the main method, but what I have tried so far doesn't seem to work. Here's my code.

import java.util.Arrays;
import java.util.Scanner;
public class ChessBoard
{
     public static void main(String[] args)
    {
        char rows = 'a';
        String spot;
        Scanner scanner = new Scanner(System.in);
        String[][] grid = new String [8][8];

        for(int i = 0; i < grid.length; i++, rows++)
        {
            for(int col = 0; col < grid[i].length; col++);
            String input = null;              // will be changed to a valid position
            boolean validCoordinate = false;   // will be true if position is valid
            while ( ! validCoordinate) {
                System.out.println("Enter a coordinate (for example, a5): ");
                input = scanner.next();
                validCoordinate = input.matches("[a-h][1-8]");
            };
            // now we now that the input is valid
            int row = input.charAt(0) - 'a';
            int col = input.charAt(1) - '1';
            String temp = input + " - ";
            System.out.println("Insert your piece:");
            input = scanner.next();
            grid[row][col] = temp + input;
        }
        System.out.println(Arrays.deepToString(grid));
     }
}

So what I'd like to do is have a new class that uses that last print line to instead print the desired output that I mentioned earlier. Any help would be appreciated, thanks!

EDIT:

import java.util.Arrays;
import java.util.Scanner;
public class ChessBoard1
{
    public static  void main(String[] args)
    {
        userInputs input = new userInputs();
        showInput show = new showInput();

        String grid[][] = input.takeInput();
        show.show(grid);
    }
}



public class userInputs
{
    public String[][] takeInput()
    {
        char rows = 'a';
        String spot;
        Scanner scanner = new Scanner(System.in);
        String[][] grid = new String [8][8];

        for(int i = 0; i < grid.length; i++, rows++) {
            for (int col = 0; col < grid[i].length; col++) ;
            String input = null;              // will be changed to a valid position
            boolean validCoordinate = false;   // will be true if position is valid
            while (!validCoordinate) {
                System.out.println("Enter a coordinate (for example, a5): ");
                input = scanner.next();
                validCoordinate = input.matches("[a-h][1-8]");
            }
            ;
            // now we now that the input is valid
            int row = input.charAt(0) - 'a';
            int col = input.charAt(1) - '1';
            String temp = input + " - ";
            System.out.println("Insert your piece:");
            input = scanner.next();
            grid[row][col] = temp + input;
        }
        return  grid;
    }
}



public class showInput {

    public void show(String [][] inputs)
    {
        for(int i=0 ; i<inputs.length ; i++){
            for(int j=0  ; j < inputs[0].length ; j++)
            {
                System.out.println(Arrays.deepToString(grid));
            }
        }
    }
}

I have 2 separate files userInputs and showInput but they it says that they should still be declared in a separate file?

It's wrong to write main Function in every class, the program uses the main function to start from it, So you should write it only in the main project class and call inside it the other classes. Your code should be:

package com.company;

public class ChessBoard
{
    public static  void main(String[] args)
    {
        userInputs input = new userInputs();
        showInput show = new showInput();

        String grid[][] = input.takeInput();
        show.show(grid);
    }
}

and other classes in separate files like:

package com.company;

import java.util.Scanner;

public class userInputs
{
    public String[][] takeInput()
    {
        char rows = 'a';
        String spot;
        Scanner scanner = new Scanner(System.in);
        String[][] grid = new String [8][8];

        for(int i = 0; i < grid.length; i++, rows++) {
            for (int col = 0; col < grid[i].length; col++) ;
            String input = null;              // will be changed to a valid position
            boolean validCoordinate = false;   // will be true if position is valid
            while (!validCoordinate) {
                System.out.println("Enter a coordinate (for example, a5): ");
                input = scanner.next();
                validCoordinate = input.matches("[a-h][1-8]");
            }
            ;
            // now we now that the input is valid
            int row = input.charAt(0) - 'a';
            int col = input.charAt(1) - '1';
            String temp = input + " - ";
            System.out.println("Insert your piece:");
            input = scanner.next();
            grid[row][col] = temp + input;
        }
        return  grid;
    }
}

and another class to output:

package com.company;

public class showInput {

    public void show(String [][] inputs)
    {
        for(int i=0 ; i<inputs.length ; i++){
            for(int j=0  ; j < inputs[0].length ; j++)
            {
                //Print Your Data
            }
        }
    }
}

Like @Atef Magdy said, You should have one class which holds all the data and functions and a main class which executes the functions.

and the explanation for this error ( it states that using public int is an "illegal start" to the expression, and that it needs a ";" after it?) I have seen that you made the 2d Array of Type String?

String[] [] grid = new String [8][8];

and then returning it as a 1D Array of Type int?

public int[] getGrid(){
return grid.clone();
}

I should say that this is the source of this error. You should change the 'int[]' to 'string[][]'

if there is any error please reply to this answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM