Why is my array printing out differently on different computers - java

For our assignment. We have to create an Absolon game. But first we have to get the display board right. I programmed the display using a 2D array, 11 by 21. Using this code. And i got the following answer and it printed out correctly. (IDE Neatbeans)
char [][] board = new char [11][21];
for(int column =6; column <= 14; column=column+2 ) {
int row = 0;
board[row][column] = '=';
}
for(int column =6; column <= 14; column=column+2 ) {
int row = 1;
board[row][column] = 'o';
}
for(int column =6; column <= 14; column=column+2 ) {
int row = 10;
board[row][column] = '=';
}
for(int column =6; column <= 14; column=column+2 ) {
int row = 9;
board[row][column] = 'x';
}
for(int column =5; column <= 15; column=column+2 ) {
int row = 2;
board[row][column] = 'o';
}
for(int column =5; column <= 15; column=column+2 ) {
int row = 8;
board[row][column] = 'x';
}
for(int column =5; column <= 15; column=column+2 ) {
int row = 8;
board[row][column] = 'x';
}
for(int column =8; column <= 12; column=column+2 ) {
int row = 3;
board[row][column] = 'o';
}
for(int column =8; column <= 12; column=column+2 ) {
int row = 7;
board[row][column] = 'x';
}
int j=1;
for(int column =4; column >= 0; column-- ) {
board[j][column] = '"';
j = j+1;
}
int l=1;
for(int column =16; column <= 20; column++ ) {
board[l][column] = '"';
l = l+1;
}
int m=6;
for(int column =1; column <= 4; column++ ) {
board[m][column] = '"';
m = m+1;
}
int n=6;
for(int column =19; column >= 16; column-- ) {
board[n][column] = '"';
n = n+1;
}
//Print Out Board
for(int row =0; row < board.length; row++) {
for(int column =0; column < board[row].length; column++) {
System.out.print(board[row][column] + "");
}
System.out.println();
}
}
}
This Is The Link to my sample output.
http://postimg.org/image/dx656twrf/
But when i went to campus working on the same problem also using neatbeans. It gave me a completely new output.
=====
"ooooo"
"oooooo"
"ooo"
""
""
""
"xxx"
"xxxxxx"
"xxxxx"
=====
Whats wrong. Is it the IDE or my code. please help.

You have not initialized the characters in board to anything so they default to zero which prints differently on various systems. Initialize board with blanks and it will work.

The memory of your multi-dimensional char array is initialized with null characters and different systems/fonts may render it differently.
Why does \0 print different output in different system in java

Related

Need assistance with creating a table for java project

So, I'm super new to programming in general. I've been learning Java through a course and one of our projects is to make a program that reads a text file and prints the number of vowels in a little table. (See below example.)
A|##
E|##
I|####
O|#
U|
I've gotten some work done. For instance getting the total number of vowels. (See below.)
String words = input.nextLine();
System.out.println(words);
for (int i=0;i<words.length(); i++)
{
char car = words.charAt(i);
if (car =='a'|| car == 'i' || car == 'e' || car=='o' || car == 'u')
{
count ++ ;
}
This prints the total number ^
Output: 9
After that I worked out a way to print the table. (See below.)
//Process for creating the chart (NOT FINAL)
System.out.println(count);
System.out.print("A|");
final int MAX_ROWS = 1 ;
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int a =5; a >= row; a--)
System.out.print("#");
}
System.out.println("");
System.out.print("E|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int e =5; e >= row; e--)
System.out.print("#");
}
System.out.println("");
System.out.print("I|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int ii =5; ii >= row; ii--)
System.out.print("#");
}
System.out.println("");
System.out.print("O|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int o =5; o >= row; o--)
System.out.print("#");
}
System.out.println("");
System.out.print("U|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int u =5; u >= row; u--)
System.out.print("#");
}
Output:
A|#####
E|#####
I|#####
O|#####
U|#####
However, I can't figure out how to get the number of each vowel into the table. I've provided the whole thing below to look through for broader context. I apologize for how rough it is, I'm just a bit lost. And I couldn't find anything with my specific table requirement on the Web. I hope one of you can help. Thanks for any answers.
P.S. Since this is a class were I'm trying to learn I want to figure this out on my own. Pointers with explanations would be great. A push in the right direction if you will.
Edit: I want to clarify something that I explained poorly. The ints a-u are placeholders and the goal was to use vowels from a text doc. The example from the test doc is "orange tree apple hope". Hope that clarifies.
Full Code:
import java.util.Scanner ;
import java.io.File ;
public class VowelCounter
{
public static void main(String[] args) throws Exception
{
File text = new File ("Doc.txt") ;
Scanner input = new Scanner(text) ;
//File Doc.txt is scanned and is used
int count = 0 ;
while (input.hasNext())
{
String words = input.nextLine();
System.out.println(words);
for (int i=0;i<words.length(); i++)
{
char car = words.charAt(i);
if (car =='a'|| car == 'i' || car == 'e' || car=='o' || car == 'u')
{
count ++ ;
}
//Test Values (NOT FINAL)
int a = 5 ;
int e = 5;
int ii = 5;
int o = 5;
int u = 5;
}
//Process for creating the chart (NOT FINAL)
System.out.println(count);
System.out.print("A|");
final int MAX_ROWS = 1 ;
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int a =5; a >= row; a--)
System.out.print("#");
}
System.out.println("");
System.out.print("E|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int e =5; e >= row; e--)
System.out.print("#");
}
System.out.println("");
System.out.print("I|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int ii =5; ii >= row; ii--)
System.out.print("#");
}
System.out.println("");
System.out.print("O|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int o =5; o >= row; o--)
System.out.print("#");
}
System.out.println("");
System.out.print("U|");
for (int row = 1; row <= MAX_ROWS; row++)
{
for(int u =5; u >= row; u--)
System.out.print("#");
}
}
//(ERROR) prints 15 # instead of 4
// a= 2 e=5 i=0 o=2 u=0 (FOR CHECK)
// 9 toatl vowels
}
}
Based on my comments above I remove the outer for loops and replaced your inner for loops with occurrences of each vowel shows up
//Test Values (NOT FINAL)
int aCount = 5 ;
int eCount = 5;
int iCount = 5;
int oCount = 5;
int uCount = 5;
//Process for creating the chart (NOT FINAL)
System.out.println(count);
System.out.print("A|");
final int MAX_ROWS = 1 ;
for(int i =0; i < aCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("E|");
for(int i =0; i < eCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("I|");
for(int i =0; i < iCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("O|");
for(int i =0; i < oCount; i++)
System.out.print("#");
System.out.println("");
System.out.print("U|");
for(int i =0; i < uCount; i++)
System.out.print("#");
}
and this is what #Just another Java programmer suggested
public static void main(String[] args){
Map<Character, Integer> vowelMap = new HashMap<Character, Integer>() {{
put('A', 0);
put('E', 0);
put('I', 0);
put('O', 0);
put('U', 0);
}};
String words = "oiwjroiajfdojasoijfalkdfoiejrf";
for(int i=0;i<words.length();i++){
char c = Character.toUpperCase(words.charAt(i));
if(vowelMap.containsKey(c))
vowelMap.put(c, vowelMap.get(c)+1);;
}
vowelMap.forEach((k,v) -> System.out.println( k+ "|"+ "#".repeat(v)));
}

Same Java code to print triangle using asterisk showing different results

The following codeparts are exactly the same, however, it prints a different result and I am unable to understand why.
//First Part
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.println("*");
System.out.println();
}
System.out.println();
//Second Part
for (int row = 1; row <= MAX_ROWS; row++)
{
for (int star = 1; star <= row; star++)
System.out.print ("*");
System.out.println();
}
Since you are using print() in the second part of your code you should insert \n to wrap text, Because println() will do it by default.
Your code to work should be like :
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++){
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
System.out.println();
for (int row = 1; row <= MAX_ROWS; row++){
for (int star = 1; star <= row; star++) {
System.out.print("*\n");
}
System.out.println();
}
Or like this:
final int MAX_ROWS = 10;
for (int row = 1; row <= MAX_ROWS; row++) {
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}
System.out.println();
for (int row = 1; row <= MAX_ROWS; row++) {
for (int star = 1; star <= row; star++) {
System.out.println("*");
}
System.out.println();
}

How do I change the numbers on an array of buttons?

My buttons are currently displaying numbers 1-9, but I don't know how to display the numbers 9-1.
I already using different numbers in my for loops, but it still did not work for me.
for (int row=0; row<3; row++) {
for (int col = 1; col<4; col++) {
int pieces = row*3 + col;
String count = Integer.toString(pieces);
Button button = new Button(count);
GridPane.setRowIndex(button, row);
GridPane.setColumnIndex(button, col);
keypad.getChildren().add(button);
button.setMinSize(80, 80);
}
}
Just subtract the calculated number from the maximum number to count backwards:
int rows = 3;
int cols = 3;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int pieces = rows * cols - (row * 3 + col);
String count = Integer.toString(pieces);
// ...
}
}
Alternatively you can reverse the both for loops:
for (int row = 2; row >= 0; row--) {
for (int col = 3; col > 0; col--) {
int pieces = row * 3 + col;
String count = Integer.toString(pieces);
// ...
}
}

Java Tetris Game: Having trouble moving a block down the grid

I am making Tetris in Java. I am having difficulty having my blocks shift down, once a row has been removed (because it filled up). My code is throwing an arrayIndexOutOfBounds error. The rest of the code works, where it finds which row is full and sets that row to 0. But in the if statement near the bottom, that's where it doesn't work. Thanks ! :)
// Removes any rows that are filled
public static int removeRows()
{
// Variables
int numOfRows = tetrisGrid.getNumOfRows();
int numOfCols = tetrisGrid.getNumOfCols();
int numRowsRemoved = 0;
int [][] grid = tetrisGrid.getGrid();
// Finds the number of rows that are filled
for (int row = 0; row < numOfRows; row++)
{
for (int col = 0; col < numOfCols; col++)
{
int blockValue = tetrisGrid.getBlockValue(row, col);
if (blockValue == 0)
{
break;
}
if (col == numOfCols - 1)
{
numRowsRemoved++;
// Sets the rows filled to zero
for (int change = 0; change < numOfCols; change++)
{
grid [row][change] = 0;
}
// Shifts everything down until it hits blocks below
for (int shiftRow = 0; shiftRow < 2; shiftRow++)
{
for (int shiftCol = 0; shiftCol < numOfCols; shiftCol++)
{
if (blockValue == 1)
{
grid [row][col] = 0;
row++;
col++;
grid [row][col] = 1;
row--;
col--;
}
else if (blockValue == 0)
break;
}
}
// TAKE OUT -------------------------
System.out.println ("--------");
tetrisGrid.printGrid();
System.out.println ("--------");
// -------------------------
}
}
}
tetrisGrid.setGrid(grid);
return numRowsRemoved;
}

Inputs for column length is returning unintended length

While working on a code for making a irregular 2D-array, I discovered a weird error while messing with different inputted values for the column. While the row works, the column length input returns either the wrong amount or a null pointer error occurs. I'm not sure what might be causing this since inputs such as ( 1 , 2 , 3) returns the correct table but (2 , 1, 3) will not. Also in a row of 4 with column inputs of (2, 3, 4, 5) returns "index out of bounds exception: 5" when there shouldn't be able to be out of bounds because of the while loop that should keep it with in reasonable range. Neither the main nor the display method seems to be saving the intended column length correctly and I can't seem to spot why.
It seems the array is set to 3 rows with column length of 1 , 2 , 3.
The output for row(3) and column(2,3,1) gives:
A:2.0
B:2.0 2.0
C:2.0 2.0 2.0
when I want is:
A:2.0 2.0
B:2.0 2.0 2.0
C:2.0
The code:
import java.util.Scanner;
//================================================================
public class ArrayIrreg {
//----------------------------------------------------------------
private static Scanner Keyboard = new Scanner(System.in);
public static void main(String[] args) {
//----------------------------------------------------------------
char group, rLetter,letter;
String choice;
int sum = 0;
int num = 10; // for test
int rows = 10;
int columns = 8;
//greetings
System.out.println("");
System.out.println("Welcome to the Band of the Hour");
System.out.println("-------------------------------");
// creating 2d array
System.out.print("Please enter number of rows : ");
rows = Keyboard.nextInt();
Keyboard.nextLine();
while (rows < 0 || rows >= 10) {
System.out.print("ERROR:Out of range, try again : ");
rows = Keyboard.nextInt();
Keyboard.nextLine();
}
double[][] figures = new double[rows + 1][num];
for(int t = 0; t < rows; t++) {
rLetter = (char)((t)+(int)'A');
System.out.print("Please enter number of positions in row " + rLetter + " : ");
columns = Keyboard.nextInt();
Keyboard.nextLine();
while(columns < 0 || columns >= 8) {
System.out.print("ERROR:Out of range, try again : ");
columns = Keyboard.nextInt();
Keyboard.nextLine();
}
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
}
// filling the array
for(int row = 0; row < figures.length; ++row) {
for(int col = 0; col < figures[row].length; ++col) {
figures[row][col] = 2.0;
}
}
// printing the array
for(int row = 1; row < figures.length; ++row) {
// printing data row
group = (char)((row-1)+(int)'A');
System.out.print(group + " : ");
for(int col = 0; col < figures[row].length; ++col) {
System.out.print(figures[row][col] + " ");
System.out.print(" ");
}
System.out.print("["+","+avg(figures)+"]");
System.out.println();
}
//----------MENU
System.out.print("(A)dd, (R)emove, (P)rint, e(X)it : ");
choice = Keyboard.next();
letter = choice.charAt(0);
letter = Character.toUpperCase(letter);
if(letter == 'P') {
display(figures);
}
}
public static void display(double x[][]) {
int average, total;
char group;
System.out.println(" ");
for(int row=1;row<x.length;row++) {
group = (char)((row-1)+(int)'A');
System.out.print(group+" : ");
for(int column=0;column<x[row].length;column++){
System.out.print(x[row][column]+" ");
}
System.out.print("["+","+avg(x)+"]");
System.out.println();
}
}
public static int avg(double[][] temp) {
int sum = 0;
int avg = 0;
for (int col = 0; col < temp[0].length; col++) {
sum = 0;
for (int row = 0; row < temp.length; row++)
sum += temp[row][col];
System.out.println(sum);
}
avg = sum / temp.length;
return avg;
}
}
In Creating 2D Array,
Change this
double[][] figures = new double[rows + 1][num];
to
double[][] figures = new double[rows][num];
and
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
to
figures[t] = new double[columns] ;
In Printing Array
Change this
for(int row = 1; row < figures.length; ++row) {
group = (char)((row-1)+(int)'A');
to
for(int row = 0; row < figures.length; ++row) {
group = (char)((row)+(int)'A');
In display function
Change this
for(int row = 1; row < x.length; ++row) {
group = (char)((row-1)+(int)'A');
to
for(int row = 0; row < x.length; ++row) {
group = (char)((row)+(int)'A');
It's this
for(int j = 0; j <= columns; j++) {
figures[j] = new double[j] ;
}
I think you meant actually
figures[t] = new double[columns];

Categories