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?
Advertisement
Answer
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 } } } }