Manipulating Arrays from User Input in a Loop

I have the following code, and I would like the user inputs to store, and then the code loop back with the next input storing also, until they hit "Q" which will then quit. I am not sure how to do it. Also, I want my 2d array to be printed blank, instead of the default 0s, after the user sets the size. SO if the user says they want to input SIZE = 4x4 "row =1, column =2, input =7" it would print "These ZEROS would be BLANK

0000
0070
0000
0000

the input "row 2, column 1, input A it would print

0000
0070
0700
0000

My code so far

import java.util.*;

public class MainProg {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("How many rows do you want for your matrix?  ");
        int row = in.nextInt();

        System.out.print("How many columns do you want for your matrix?  ");
        int column = in.nextInt();

        Array2 twoDArray = new Array2(row, column); //calling my class
        //twoDArray.getElement();


        System.out.println("If you would like to set an, element press S: " + "\n" +
                "If you would like to set an element, press G" + "\n" +
                "If you would like to empty an element, press E" + "\n" +
                "If you would like to print an element, press P" + "\n" +
                "If you would like to quit, press Q");
        String userInput = in.next();
        //do {
            if (userInput.equalsIgnoreCase("S")) {
                twoDArray.setElement();
            } else if (userInput.equalsIgnoreCase("G")) {
                twoDArray.getElement();
            } else if (userInput.equalsIgnoreCase("E")) {
                twoDArray.clearElement();
            } else if (userInput.equalsIgnoreCase("P")) {
                twoDArray.printMatrix();
                //you will do you toString here
            } else if (userInput.equalsIgnoreCase("Q")) {
                //this will quit the program
                System.exit(0);
            }
        //} while(!userInput.equalsIgnoreCase("q"));
    }
}
import java.util.*;

public class Array2 {
    MainProg main1 = new MainProg();
    Scanner in = new Scanner(System.in);
    //private ArrayList[][];
    private int row;
    private int column;
    String [][] newArray;



    public Array2(int row, int column) {
        this.row = row;
        this.column = column;
        String[][] newArray = new String[row][column];
    }

    public void getElement() {
        //int[][] newArray = new int[row][column]; (TOOK THIS OUT)
        Scanner in = new Scanner(System.in);
        System.out.println("What row is the element you would like to get in? (Must be under " + row + ")");
        int userRow = in.nextInt();
        System.out.println("What column is the element you like to get in? (Must be under " + column + ")");
        int userCol = in.nextInt();
        System.out.println("You have entered: " + "\n" +
                "Row " + userRow + "\n" +
                "Column " + userCol);
        int getElement = newArray[userRow][userCol];
        //System.out.println(getElement); (TOOK THIS OUT)
    }

    public void setElement() {
        //int[][] newArray = new int[row][column]; (TOOK THIS OUT)
        Scanner in = new Scanner(System.in);
        System.out.println("What row would you like your element in? (Must be under " + row + ")");
        int userRow = in.nextInt();
        System.out.println("What column would you like your element in? (Must be under " + column + ")");
        int userCol = in.nextInt();
        System.out.println("What character would you like the element to be?");
        char userChar = in.next().charAt(0);
        System.out.println("You have entered: " + "\n" +
                "Row " + userRow + "\n" +
                "Column " + userCol + "\n" +
                "Char to be entered: " + userChar);
        newArray[row][column] = String.valueOf(userChar); //CHANGED TO USEROF
    }

    public void clearElement() {
        Scanner in = new Scanner(System.in);
        //int[][] newArray = new int[row][column]; (TOOK THIS OUT)
        System.out.println("What row is the element you would like empty? (Must be under " + row + ")");
        int userRow = in.nextInt();
        System.out.println("What column is the element you like to empty? (Must be under " + column + ")");
        int userCol = in.nextInt();
        System.out.println("You have entered: " + "\n" +
                "Row " + userRow + "\n" +
                "Column " + userCol);

    }

    public void printMatrix() {
        //int[][] newArray = new int[row][column]; (TOOK THIS OUT, THIS CAUSING ERROR)
        //{
        //    for (int[] row : newArray)   //THIS FILLS THE MATRIX WITH 32??????
        //        Arrays.fill(row, ' ');
        //}

        Scanner in = new Scanner(System.in);
        String result = " ";
        System.out.println("The array is: \n");
        for (int i = 0; i < newArray.length; i++) {
            for (int j = 0; j < newArray[i].length; j++) {
                System.out.print(newArray[i][j]);
            }
            System.out.println();
        }
    }

}


Read more here: https://stackoverflow.com/questions/66993048/manipulating-arrays-from-user-input-in-a-loop

Content Attribution

This content was originally published by dreedski at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: