I would like to develop a character array output that outputs ONLY a single character in each position. While my programming appear to work fine with filling all spaces with a single character or with 2 characters one each at alternating spaces, its not the same with designated position.

For a 3x3 array[][] with x and x o values respectively I get..
x x x|......|x o x
x x x|......|o x o
x x x|......|x o x

BUT my output for character at designated array position(1,1) is:
00 00 00
00 x 00
00 00 00

I really need to have the following:
0 0 0
0 x 0
0 0 0

Here is the code I have creating the trouble and I suspect the double zeros are coming from my for statements.

// Code to use scanner to select character and designated position
//the character is selected and inserted at the position I
//designate with these code segments so no problem there...
.
.
.
// Insert character into array
         char square[][];
         square = new char[size][size];
         for (int row = 0; row < size; row++) {    // allocate array of rows
            for (int col = 0; col < size; col++) { // allocate array of columns

               System.out.print(" " + square[row][col]);
            }
            System.out.println(" ");
         }

Thanks,
Jems

Recommended Answers

All 6 Replies

The comment above your loop says you are inserting a character into the array, but inside the loop all you are doing is printing out the array. Where does the character get inserted? Your code to print out the array looks fine by the way.

The comment above your loop says you are inserting a character into the array, but inside the loop all you are doing is printing out the array. Where does the character get inserted? Your code to print out the array looks fine by the way.

Here it is, I figured more ore less the problem lied within the output (printout).

// Method takes a symbol and places it in a designated cells of board
      public void placeSymbolDsg(char symb4, int rowPos, int colPos) {
      // Generate symbol for use in designated board space
         Board in=new Board(size);      
         String input4;
         Scanner inSymb4 = new Scanner(System.in);
         System.out.println("\nEnter a symbol: ");
         input4 = inSymb4.nextLine();
         symb4 = input4.charAt(0);
         int input5;
         Scanner inRow = new Scanner(System.in);
         System.out.println("Enter a row array position: ");
         rowPos = inRow.nextInt();
         int input6;
         Scanner inCol = new Scanner(System.in);
         System.out.println("Enter a column array position: ");
         colPos = inCol.nextInt();
         System.out.println(desigSym);
      // Insert character into array
         in.getSizeSqr();
         for (int row = 0; row < size; row++) {    // allocate array of rows
            for (int col = 0; col < size; col++) { // allocate array of columns
               try {
                  square[rowPos][colPos] = symb4;
                  if (square[rowPos][colPos] > square[row][col]) {
                  }
               }
                  catch (ArrayIndexOutOfBoundsException OBErr) {
                     System.out.println("Out of bounds entry - please try again!!!");
                     System.exit(0);
                  }
               finally { }
               System.out.print(" " + square[row][col]);
            }
            System.out.println(" ");
         }
      }

I ran your code with a couple modifications because I don't have the context, but it looks to me like the code is printing a single character at each location, so I'm not sure what the problem is that you are seeing without more of your code. I do have a few suggestions though.

1. You really only need one Scanner object. There's no need to create multiple scanners.
2. You read a symbol (symb4), a rowPos & a colPos from the user, so to set the symbol at the appropriate place in the square, you only need line 24 above. It doesn't need to be inside 2 nested for loops.
3. You do need those nested for loops to print out your 2d array though.
4. Line 25 doesn't do anything. You need to check that 0 >= rowPos < size and
0 >= colPos < size. Note that this is not correct Java syntax, but I'm just trying to show you that rowPos and colPos need to be within the correct range so you don't go out of bounds of the array.

Here's the program I adapted from your code.

import java.util.Scanner;

public class Test2DCharArray {
   public static void main(String[] args) {
      final int size = 10;
      char square[][] = new char[size][size];
      for (int row = 0; row < size; row++) {
         for (int col = 0; col < size; col++) {
            square[row][col] = '0';
         }
      }      
      String input4;
      Scanner in = new Scanner(System.in);
      System.out.println("\nEnter a symbol: ");
      input4 = in.nextLine();
      char symb4 = input4.charAt(0);
      System.out.println("Enter a row array position: ");
      int rowPos = in.nextInt();
      System.out.println("Enter a column array position: ");
      int colPos = in.nextInt();
      
      // assign the symbol
      /* you could check here that rowPos and colPos are both
       * greater than or equal to zero and less than size if
       * you want to make sure you don't go out of bounds.
       */
      square[rowPos][colPos] = symb4;
      
      for (int row = 0; row < size; row++) {
         for (int col = 0; col < size; col++) {
            System.out.print(" " + square[row][col]);
         }
         System.out.println(" ");
      }
   }
}

I ran your code with a couple modifications because I don't have the context, but it looks to me like the code is printing a single character at each location, so I'm not sure what the problem is that you are seeing without more of your code. I do have a few suggestions though.

1. You really only need one Scanner object. There's no need to create multiple scanners.
2. You read a symbol (symb4), a rowPos & a colPos from the user, so to set the symbol at the appropriate place in the square, you only need line 24 above. It doesn't need to be inside 2 nested for loops.
3. You do need those nested for loops to print out your 2d array though.
4. Line 25 doesn't do anything. You need to check that 0 >= rowPos < size and
0 >= colPos < size. Note that this is not correct Java syntax, but I'm just trying to show you that rowPos and colPos need to be within the correct range so you don't go out of bounds of the array.

Here's the program I adapted from your code.

import java.util.Scanner;

public class Test2DCharArray {
   public static void main(String[] args) {
      final int size = 10;
      char square[][] = new char[size][size];
      for (int row = 0; row < size; row++) {
         for (int col = 0; col < size; col++) {
            square[row][col] = '0';
         }
      }      
      String input4;
      Scanner in = new Scanner(System.in);
      System.out.println("\nEnter a symbol: ");
      input4 = in.nextLine();
      char symb4 = input4.charAt(0);
      System.out.println("Enter a row array position: ");
      int rowPos = in.nextInt();
      System.out.println("Enter a column array position: ");
      int colPos = in.nextInt();
      
      // assign the symbol
      /* you could check here that rowPos and colPos are both
       * greater than or equal to zero and less than size if
       * you want to make sure you don't go out of bounds.
       */
      square[rowPos][colPos] = symb4;
      
      for (int row = 0; row < size; row++) {
         for (int col = 0; col < size; col++) {
            System.out.print(" " + square[row][col]);
         }
         System.out.println(" ");
      }
   }
}

Thanks Kramered,

The program in essence is doing exactly what I want it to do and that is to place a symbol in a designated space of my choosing. If I print an empty two dimensional array, each position is filled with 00; likewise a one dimensional array 0 in all position. When I place an X in a designated position of the two dimensional array, the space if filled while the others have the 00. My instructor tells me that for this assignment, there must only be one symbol/character in each place. I can see how I can get rid of the 00. This is where I need help - why the double 00??? A 3x3 array prints for example:

00 00 00
00 x 00
00 00 00

I really need:
0 0 0
0 x 0
0 0 0

Thanks,
Jems

As I said, I am not seeing the double zeros print out. Maybe the problem is that the array is empty except for the place where you put your symbol. If you notice in the code I posted (lines 7-11), I am initializing the array to all '0' characters. Maybe this is what you need to do?

As I said, I am not seeing the double zeros print out. Maybe the problem is that the array is empty except for the place where you put your symbol. If you notice in the code I posted (lines 7-11), I am initializing the array to all '0' characters. Maybe this is what you need to do?

Eureka - Kramered!!!!!
I added the code line square[row][col] = '0'; immediately after the for statements.

Thank you, thank you, thank you very much - my problem is solved. I am pretty excited. I am pretty close to giving up on this particular course because access to a tutor is very limited with 48hr+ response turn around.

PS: I have another stumper I will post soon.

Thanks against,
Jems

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.