RSS Forums RSS

pls help w/ this

Please support our Java advertiser: Programming Forums
Reply
Posts: 8
Reputation: beginner21 is an unknown quantity at this point 
Solved Threads: 0
beginner21 beginner21 is offline Offline
Newbie Poster

pls help w/ this

  #1  
Nov 30th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,143
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 121
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: pls help w/ this

  #2  
Nov 30th, 2008
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 .
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 :-

  1. System.out.print("\nPlease insert the first card row and column seperated by a comma.");
  2. 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.
Last edited by stephen84s : Nov 30th, 2008 at 3:21 pm.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote  
Posts: 8
Reputation: beginner21 is an unknown quantity at this point 
Solved Threads: 0
beginner21 beginner21 is offline Offline
Newbie Poster

Re: pls help w/ this

  #3  
Nov 30th, 2008
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?
Reply With Quote  
Posts: 15,219
Reputation: jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light 
Solved Threads: 455
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: pls help w/ this

  #4  
Nov 30th, 2008
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
Master of puppets Im pulling your strings - blinded by me, you cant see a thing. Master! Master!

If i am helpful, please give me reputation points.
Reply With Quote  
Posts: 8
Reputation: beginner21 is an unknown quantity at this point 
Solved Threads: 0
beginner21 beginner21 is offline Offline
Newbie Poster

Re: pls help w/ this

  #5  
Nov 30th, 2008
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?
Reply With Quote  
Posts: 15,219
Reputation: jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light jbennet is a glorious beacon of light 
Solved Threads: 455
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: pls help w/ this

  #6  
Nov 30th, 2008
thats because you are using print not println
Master of puppets Im pulling your strings - blinded by me, you cant see a thing. Master! Master!

If i am helpful, please give me reputation points.
Reply With Quote  
Posts: 8
Reputation: beginner21 is an unknown quantity at this point 
Solved Threads: 0
beginner21 beginner21 is offline Offline
Newbie Poster

Re: pls help w/ this

  #7  
Nov 30th, 2008
where is the exact location where in i change the System.out.print into System.out.println?
can you post it?
Reply With Quote  
Posts: 8
Reputation: beginner21 is an unknown quantity at this point 
Solved Threads: 0
beginner21 beginner21 is offline Offline
Newbie Poster

Re: pls help w/ this

  #8  
Dec 1st, 2008
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?
Last edited by beginner21 : Dec 1st, 2008 at 12:19 am. Reason: code tags
Reply With Quote  
Posts: 1,143
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 121
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: pls help w/ this

  #9  
Dec 1st, 2008
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 :-
  1. for(int r=0; r<4; r++)
  2. {
  3. for(int c=0; c<4; c++)
  4. {
  5. cards[r][c]=(int) (Math.random() * 8);
  6. // System.out.print(cards[r][c]);
  7. }
  8. // System.out.print("\n");
  9. }

They are the first nested for loops in your code, but honestly you shouldn't be asking us that.
Last edited by stephen84s : Dec 1st, 2008 at 1:07 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote  
Posts: 812
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 72
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: pls help w/ this

  #10  
Dec 1st, 2008
  1. int r1, c1, r2, c2;
  2. int[][]cards;
  3. cards=new int[4][4];
  4. for(int r=0; r<4; r++)
  5. {
  6. for(int c=0; c<4; c++)
  7. {
  8. cards[r][c]=(int) (Math.random() * 8);
  9. // the line below should be commented
  10. System.out.print(cards[r][c]);
  11. }
  12. System.out.print("\n");
  13. }

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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the Java Forum
Views: 524 | Replies: 10 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:03 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC