hi everyone,
can someone help me w/ this
here is my code:

import java.io.*;
public class Num2
{
public static void main(String[]args)throws IOException
{
BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
String comma;
int r1, c1, r2, c2;
int[][]cards;
cards=new int[4][4];
for(int r=0; r<4; r++)
{
for(int c=0; c<4; c++)
{
cards[r][c]=(int) (Math.random() * 8 + 1);
System.out.print(cards[r][c]);
}
System.out.print("\n");
}
for (int r=0; r<4; r++)
{
for (int c=0; c<4; c++)
{
System.out.print("*");
}
System.out.print("\n");
}
System.out.print("\nPlease insert the first card row and column seperated by a comma.");
r1=Integer.parseInt(a.readLine());
comma=a.readLine();
c1=Integer.parseInt(a.readLine());
System.out.print("\nPlease insert the second card row and column seperated by a comma.");
r2=Integer.parseInt(a.readLine());
comma=a.readLine();
c2=Integer.parseInt(a.readLine());
//reveal
for(int r=0; r<4; r++)
{
for (int c=0; c<4; c++)
{
if((r==r1)&&(c==c1))
{
System.out.print(cards[r][c]);
}
else if((r==r2)&&(c==c2))
{
System.out.print(cards[r][c]);
}
else
{
System.out.print("*");
}
System.out.println();
}
//match?
if (cards[r1][c1]==cards[r2][c2])
{
}
else
{
}
//this pushes the next board onto a blank screen
for (int b=0; b<=20; b++)
System.out.println();
}
}
}

my problem here is once i entered the row and columns,there is nothing happens..pls help,
here's the error...

---------- Capture Output ----------
> "C:\jdk1.3.0_02\bin\java.exe" Num2
5373
7563
5732
8555
****
****
****
****

Please insert the first card row and column seperated by a comma.1,2
java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Integer.java:426)
at java.lang.Integer.parseInt(Integer.java:454)
at Num2.main(Num2.java:29)
Exception in thread "main"

and also how to hide the values of the cards,
i mean only the * can only be seen,
once the row and the column is inputted,
the values will only show,
pls help w/ my program

Recommended Answers

All 10 Replies

First of all let me congratulate you on the amazing indentation technique you follow. I think very few programmers in the world survive today who use your indentation technique ... complete left alignment :P.
I suggest you first learn to indent your code. As long as you are doing these kiddy programs you will not have any problems, but when you start writing code even extending to a couple of hundred lines, you will find yourself completely lost trying to figure out which { belongs to which } or where an if block ends etc.
I think you should seriously follow at least some of the coding conventions mentioned by Sun here while programming in Java.
And please give meaningful names to your variables, the sooner you develop these good habits the better it is for your future programmer self.

Now lets see where the problem is :-

System.out.print("\nPlease insert the first card row and column seperated by a comma.");
r1=Integer.parseInt(a.readLine());

Now here you are asking a User to input a row and column number separated by a comma.
But whatever you receive you are directly passing to the Integer.parseInt() method.
Now let me tell you how exactly readLine() works, From your code I get the impression that you think the readLine() method reads character by character which is wrong it actually returns to you the entire LINE of text which was written, so in your case it would be something like 1,2 which is passed to your Integer.parseInt() method.

And since 1,2 is not a valid number, Integer.parseInt() throws a NumberFormatException .

To get over this problem I would suggest you store whatever readLine() returns in a temporary String object and then using the split() method of the String class extract the values you need out of it.

OR
You could always accept the row and column values on separate lines without the comma.

ok,
i get it
here is my modified code:

import java.io.*;
public class Num2
{
	public static void main(String[]args)throws IOException
	{
	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
	
	int r1, c1, r2, c2;
	int[][]cards;
	cards=new int[4][4];
	for(int r=0; r<4; r++)
	{
		for(int c=0; c<4; c++)
		{
			cards[r][c]=(int) (Math.random() * 8);
			System.out.print(cards[r][c]);
		}
		System.out.print("\n");
	}
   	 for (int r=0; r<4; r++)
    	{
        for (int c=0; c<4; c++)
        {
           System.out.print("*");
        }
	System.out.print("\n");
	}
	System.out.print("\nPlease insert the first card row.");
	r1=Integer.parseInt(a.readLine());
	System.out.print("\nPlease insert the first card column.");
	c1=Integer.parseInt(a.readLine());
	System.out.print("\nPlease insert the second card row.");
	r2=Integer.parseInt(a.readLine());
	System.out.print("\nPlease insert the first card column.");
	c2=Integer.parseInt(a.readLine());
	//reveal
	for(int r=0; r<4; r++)
    	{
        for (int c=0; c<4; c++)
        {
	if((r==r1)&&(c==c1))
	{
		System.out.print(cards[r][c]);
	}
	else if((r==r2)&&(c==c2))
	{
		System.out.print(cards[r][c]);
	}
	else
	{
	System.out.print("*");
	}
	}
	//match?
    	if (cards[r1][c1]==cards[r2][c2])
    	{
    	}
    	else
    	{
    	}
	}	
}
}

the output is this:

5446
5611
4143
4601
****
****
****
****

Please insert the first card row.1

Please insert the first card column.2

Please insert the second card row.3

Please insert the second card column.3
******1********1

how can i fix it?

add some more whitespace for visibility while you are at it
and you have virtually no comments! its no wonder you cant see where you are going wrong

my problem here is i want the output to be print like this:
example:

Please insert the first card row: 1
Please insert the first card column: 2
Please insert the second card row: 3
Please insert the second card column: 3
****
**0*
****
***2

but i came up with this

Please insert the first card row: 1
Please insert the first card column: 2
Please insert the second card row: 3
Please insert the second card column: 3
******0********2

how can i fix it?

thats because you are using print not println

where is the exact location where in i change the System.out.print into System.out.println?
can you post it?

alright..
i print now the output that i wanted..
i have a question again..
i want to hide the values of the cards in my output.
here is my modified code:

import java.io.*;
public class Num2
{
	public static void main(String[]args)throws IOException
	{
	BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
	
	int r1, c1, r2, c2;
	int[][]cards;
	cards=new int[4][4];
	for(int r=0; r<4; r++)
	{
		for(int c=0; c<4; c++)
		{
			cards[r][c]=(int) (Math.random() * 8);
			System.out.print(cards[r][c]);
		}
		System.out.print("\n");
	}
   	 for (int r=0; r<4; r++)
    	{
        for (int c=0; c<4; c++)
        {
           System.out.print("*");
        }
	System.out.print("\n");
	}
	System.out.print("\nPlease insert the first card row: ");
	r1=Integer.parseInt(a.readLine());
	System.out.print("\nPlease insert the first card column: ");
	c1=Integer.parseInt(a.readLine());
	System.out.print("\nPlease insert the second card row: ");
	r2=Integer.parseInt(a.readLine());
	System.out.print("\nPlease insert the first card column: ");
	c2=Integer.parseInt(a.readLine());
	//reveal
	for(int r=0; r<4; r++)
    	{
        for (int c=0; c<4; c++)
        {
	if((r==r1)&&(c==c1))
	{
		System.out.print(cards[r][c]);
	}
	else if((r==r2)&&(c==c2))
	{
		System.out.print(cards[r][c]);
	}
	else
	{
	System.out.print("*");
	}
	}
	//match?
    	if (cards[r1][c1]==cards[r2][c2])
    	{
    	}
    	else
    	{
	System.out.println();
    	}
	}	
}
}

the output is:

5264
7021
1741
1556
****
****
****
****
Please insert the first card row: 1
Please insert the first card column: 2
Please insert the second card row: 3
Please insert the second card column: 3
****
**2*
****
***6

the values in my output are printed also..
before the 4x4 *;
what should i modify?

Now from your post I get the impression that you want to hide the following part of the output:-

5264
7021
1741
1556

That would be simple, just comment out or remove the System.out.print() in the following :-

for(int r=0; r<4; r++)
{
	for(int c=0; c<4; c++)
	{
		cards[r][c]=(int) (Math.random() * 8);
		// System.out.print(cards[r][c]);
	}
	// System.out.print("\n");
}

They are the first nested for loops in your code, but honestly you shouldn't be asking us that.

int r1, c1, r2, c2;
	int[][]cards;
	cards=new int[4][4];
	for(int r=0; r<4; r++)
	{
		for(int c=0; c<4; c++)
		{
			cards[r][c]=(int) (Math.random() * 8);
                        // the line below should be commented
			System.out.print(cards[r][c]);
		}
		System.out.print("\n");
	}

I have mentioned the changes in the code itself (check the comment)
As I see here you have not been taking note of some of the conventions mentioned to you. What you have rather done is just for the sake of doing it have indented the code in question and have kept all the other code part as it is. The conventions sited as for your convenience and not for ours. If you want to plainly discard the guidelines mentioned to you while keep on asking for help I don't think this is the right place for you.

alright,
i figure it out,
sorry..
my problem now is the condition,
the game stop after the user inputted the row and the column and display the value in that row and column,
what should i do to fix that condition?

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.