"Column" cannot be resolved to a variable - java

i am trying some of the practice New Boston videos and can't get this to work! I am very new to java, this is my first class. I have an assignment to do a 2d array and I can't figure out how to get it to display on the screen. This practice is from Thenewboston's Java tutorials, video # 34 and it works for him!
public class inventory {
public static void main (String[] args) {
int firstarray[][]= {{8,9,10,11},{12,13,14,15}};
int secondarray[][]={{30,31,32,33},{43},{4,5,6}};
System.out.println("This is the first array");
display(firstarray);
System.out.println("This is the second array");
display(secondarray);
}
public static void display (int x[][])
{
for(int row=0;row<x.length;row++)
{
for(int column=0;column<x.length;column++);
System.out.print(x[row][column]+"\t");
}
{
System.out.println();
}
}
}

You've put a ; after the for loop, negating what you thought was its body. Get rid of
for(int column=0;column<x.length;column++); // <--- this ;
In this situation, the body of the for loop, where column variable is declared and has scope, is everything after the ) and before the ;. In other words, nothing. You actually need to replace the ; with a {.
Correct indentation would go a long way in helping you write syntactically correct code.

You have semicolon at the end of for cycle and not good formating. There are also two brackets, which are absolutely useless :). The right code can look like this :
public static void display(int x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x.length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}
Still the display function is not correct, it fails at the end, because the difference of length of rows and columns.
If you want to make this functional, at the second for cycle, you should consider the length of the actual ROW, not how many rows (which is x.length).
You only need change column < x.length to column < x[row].length
So the working code is this :
public static void display(int x[][]) {
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
}
}

Related

Why are my arrays returned blank from a method but the code works fine when running in the main-Method?

I'm having following problem with my program.
It's a "connect four" Java console application.
When starting my program, I reset 3 things.
A multi-dimensional char array
public final int rows = 8, columns = 8;
public char[][] board = new char[rows][columns];
I reset it with a for-loop, overwriting every array field with the character '.', which is my default board texture.
public char[][] resetBoard() {
// for loops cycle through every array field
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = '.';
}
}
return board;
}
An integer-array
public int[] nums = new int[columns];
I reset it with a for-loop using the variable i, since I just need an array with the length of columns, which just counts up from 1. It is used so the user know which column he's choosing. Like in chess "A6" e.g., except without letters.
public int[] resetNums() {
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
return nums;
}
An integer
public int roundCounter = 0;
The integers keeps track of how many rounds there have been in the current game. I want it to be printed while playing.
public int resetRoundCounter() {
// Resetting Round Counter, by initializing it to 0
return roundCounter = 0;
}
I reset these looking like this:
gameMain main = new gameMain();
gameMode mode = new gameMode();
gameCheck check = new gameCheck();
main.board = main.resetBoard();
main.nums = main.resetNums();
check.roundCounter = check.resetRoundCounter();
My problem is when printing the game board, the nums-array and the round counter none seem to work.
The game board is just completely blank. The nums-array is only 0's and the round counter stays at 0.
When running the code in the main-method it worked better than running it through classes etc.
My print method:
public void printBoard() {
gameMain main = new gameMain();
gameCheck check = new gameCheck();
// Printing number array with space in between the elements
for (int i = 0; i < main.nums.length; i++) {
System.out.print(main.nums[i] + " ");
}
// Printing the round count next to the number array
System.out.println(" Round " + check.getRoundCounter());
for (int i = 0; i < main.rows; i++) {
for (int j = 0; j < main.columns; j++) {
System.out.print(main.board[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < main.nums.length; i++) {
System.out.print(main.nums[i] + " ");
}
System.out.println("\n");
}
I could really use some help, since I've been up all night. Originally due to how much fun I was having programming this, now it has become frustrating.
Thanks for reading and thanks in advance!
I think what is happening to you is related to the references of the objects you are using. You are mixing two different ways of working, I give you an example:
You can use the reference of 'roundCounter' and work on it:
public void resetRoundCounter() {
// Resetting Round Counter, by initializing it to 0
roundCounter = 0;
}
or you can return it, like this:
public int resetRoundCounter() {
// Resetting Round Counter, by initializing it to 0
return 0;
}
In the first case, you will have to call the function like this:
resetRoundCounter(); //this function changes the value of your variable.
In the second case, you will have to call the function like this:
roundCounter = resetRoundCounter();
You can choose the way you like to work but I recomend you not working with global variables especially working with methods. I hope it helps you.
Do it as
public void resetBoard() {
// for loops cycle through every array field
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = '.';
}
}
}
public void resetNums() {
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
}
public void resetRoundCounter() {
// Resetting Round Counter, by initializing it to 0
roundCounter = 0;
}
Finally, call them as follows:
gameMain main = new gameMain();
gameCheck check = new gameCheck();
main.resetBoard();
main.resetNums();
check.resetRoundCounter();
I also recommend you follow Java Naming Convention e.g. gameMain should be GameMain and gameCheck should be GameCheck.

Java, need help making a for loop to make a image like stairs with single letters

I need help to make program that will accept a number and print that number of rows to make a image, and it would use letters for the image like this. for example i will input the number 5 and then the program would print this.....
x
xx
xxx
xxxx
xxxxx
|
public class Stairs
{
public static void Stairs1 (int height)
{
for (int row = 0; row < height; row = row + 1)
{
for (int col = 0; col <= row; col = col + 1)
{
System.out.print("x");
}
System.out.println();
}
}
}
This is to make that. I'm trying to make the a simpler thing but it would be flipped over from the left side to the right side
like this.....
x
xx
xxx
xxxx
xxxxx
Try something like below; it should give you the desired output:
// Function to demonstrate printing pattern
public static void printStars(int n)
{
int i, j;
// outer loop to handle number of rows
// n in this case
for(i=0; i<n; i++)
{
// inner loop to handle number spaces
// values changing acc. to requirement
for(j=2*(n-i); j>=0; j--)
{
// printing spaces
System.out.print(" ");
}
// inner loop to handle number of columns
// values changing acc. to outer loop
for(j=0; j<=i; j++)
{
// printing stars
System.out.print("* ");
}
// ending line after each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 5;
printStars(n);
}
}

need a method to print a 2d array that works for this program

So like the title says, im trying to make a method that prints a 2d array that i have already populated with another method. so basically this is what i have for my populate method.
public void populate() {
int num = 1;
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
this.values[i][j] = num;
++num;
}
}
}
that is in a class called Array_two now right below this in that same class i have this.
static void printMatrix(int[][] grid) {
for(int r=0; r<grid.length; r++) {
for(int c=0; c<grid[r].length; c++)
System.out.print(grid[r][c] + " ");
System.out.println();
}
}
in my main i have this
Array_two my2dArray = new Array_two(10,10);
my2dArray.populate();
Array_two.printMatrix(my2dArray);
everything works except for that print statement, ive been playing around with it because i know its a problem with the types and im probably making a really dumb mistake i just for some reason cant figure out what im doing at the moment.
Instead of
Array_two.printMatrix(my2dArray);
do
Array_two.printMatrix(my2dArray.values);
Because my2dArray is an object not a 2d array.

Why won't my array fill with '?'s?

public class MakeQuilt {
public static void main (String[] args){
char [][] myBlock = new char [4][5];
char [][] myQuilt = new char [12][4];
for(int row = 0; row < myBlock.length; row++){
for(int column = 0; column < myBlock[row].length; column++){
if(column == 0 || row == 3)
myBlock[row][column]='X';
else if(row == column || (row == 2 && column == 1))
myBlock[row][column]='+';
else
myBlock[row][column]='.';
}
}
displayPattern(myBlock);
displayPattern(myQuilt);
}
public static void displayPattern(char[][] myBlock){
for(int row = 0; row < myBlock.length; row++){
for(int column = 0; column < myBlock[row].length; column++){
System.out.print(myBlock[row][column]);
}
System.out.println();
}
System.out.println();
}
public static void fillQuilt(char[][] myQuilt){
for(int row = 0; row < myQuilt.length; row++){
for(int column = 0; column < myQuilt[row].length; column++){
myQuilt[row][column] =('?');
}
}
}
}
Can't seem to figure out why my char array myQuilt won't fill with question marks but instead is filled with nothing? (output shows a bunch of 0's). Not sure how to change the displayPattern method to output ?'s in the myQuilt array.
Before calling displayPattern, you have to fill quilt somewhere. i.e.
displayPattern(myBlock);
fillQuilt(myQuilt);
displayPattern(myQuilt);
Question: You define the fillQuilt(...) method where you would fill an array with question mark characters, but where do you ever call this method?
Answer: You don't (at least you don't show it), and if it's never called, it will never do its thing. The solution is to call the fillQuilt method, passing in myQuilt where you need it to do its actions: fillQuilt(myQuilt);. Understand that programming takes things very literally: they only do what you explicitly program them to do, nothing less, nothing more.
I can't see any call to your fillQuilt() method in your main.
Don't you have to call fillQuilt() somewhere before printing?

little java help with loops

Hi I am doing some practice problems and trying to print a diagonal line like the example below. I have writen the program you see below and I honestly dont understand what I am doing wrong. I m a java beginner and I cant see how to find the error.
Example:
*
*
*
*
*
code:
class Diagonal{
public static void main(String args[]) {
int row, col;
for(row = 1; row < 6; row++) {
for(col = 1; col <= row; col++) {
if(col==row){
System.out.print("*");
} else{
System.out.print("");
}
System.out.println();
}
}
}
}
I am trying to learn for loops because they really confuse me. Another practice is to print a similar diagonal line but this time from right to left. I cant do that without getting this right however :( I believe they will be pretty similar?
Above my reasining is this: As long as the column # is the same as the row number the print the line or otherwise leave a blank....what's wrong with how i did it?
THANK YOU!
You never print any space character. You print an empty String. Replace
System.out.print("");
with
System.out.print(" ");
Also, you write a newline after each column rather than writing one after each row.
String spaces = "";
for(int row = 1; row < 6; row++) {
System.out.println(spaces+"*");
spaces += " ";
}
Print new lines when entering a star: System.out.println("*");
Add spaces: System.out.println(" ");
remove the line where you print new lines between columns.
as said before replace black with space and move the end line to the end of the FIRST for() like this:
class Diagonal{
public static void main(String args[]) {
int row, col;
for(row = 1; row < 6; row++) {
for(col = 1; col <= row; col++) {
if(col==row){
System.out.print("*");
} else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

Categories

Resources