pls help w/ this

Reply

Join Date: Nov 2008
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

 
0
  #1
Nov 30th, 2008
hi everyone,
can someone help me w/ this
here is my code:
  1. import java.io.*;
  2. public class Num2
  3. {
  4. public static void main(String[]args)throws IOException
  5. {
  6. BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
  7. String comma;
  8. int r1, c1, r2, c2;
  9. int[][]cards;
  10. cards=new int[4][4];
  11. for(int r=0; r<4; r++)
  12. {
  13. for(int c=0; c<4; c++)
  14. {
  15. cards[r][c]=(int) (Math.random() * 8 + 1);
  16. System.out.print(cards[r][c]);
  17. }
  18. System.out.print("\n");
  19. }
  20. for (int r=0; r<4; r++)
  21. {
  22. for (int c=0; c<4; c++)
  23. {
  24. System.out.print("*");
  25. }
  26. System.out.print("\n");
  27. }
  28. System.out.print("\nPlease insert the first card row and column seperated by a comma.");
  29. r1=Integer.parseInt(a.readLine());
  30. comma=a.readLine();
  31. c1=Integer.parseInt(a.readLine());
  32. System.out.print("\nPlease insert the second card row and column seperated by a comma.");
  33. r2=Integer.parseInt(a.readLine());
  34. comma=a.readLine();
  35. c2=Integer.parseInt(a.readLine());
  36. //reveal
  37. for(int r=0; r<4; r++)
  38. {
  39. for (int c=0; c<4; c++)
  40. {
  41. if((r==r1)&&(c==c1))
  42. {
  43. System.out.print(cards[r][c]);
  44. }
  45. else if((r==r2)&&(c==c2))
  46. {
  47. System.out.print(cards[r][c]);
  48. }
  49. else
  50. {
  51. System.out.print("*");
  52. }
  53. System.out.println();
  54. }
  55. //match?
  56. if (cards[r1][c1]==cards[r2][c2])
  57. {
  58. }
  59. else
  60. {
  61. }
  62. //this pushes the next board onto a blank screen
  63. for (int b=0; b<=20; b++)
  64. System.out.println();
  65. }
  66. }
  67. }
my problem here is once i entered the row and columns,there is nothing happens..pls help,
here's the error...
  1. ---------- Capture Output ----------
  2. > "C:\jdk1.3.0_02\bin\java.exe" Num2
  3. 5373
  4. 7563
  5. 5732
  6. 8555
  7. ****
  8. ****
  9. ****
  10. ****
  11.  
  12. Please insert the first card row and column seperated by a comma.1,2
  13. java.lang.NumberFormatException:
  14. at java.lang.Integer.parseInt(Integer.java:426)
  15. at java.lang.Integer.parseInt(Integer.java:454)
  16. at Num2.main(Num2.java:29)
  17. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
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: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: pls help w/ this

 
1
  #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 4: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 Quick reply to this message  
Join Date: Nov 2008
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

 
0
  #3
Nov 30th, 2008
ok,
i get it
here is my modified code:
  1. import java.io.*;
  2. public class Num2
  3. {
  4. public static void main(String[]args)throws IOException
  5. {
  6. BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
  7.  
  8. int r1, c1, r2, c2;
  9. int[][]cards;
  10. cards=new int[4][4];
  11. for(int r=0; r<4; r++)
  12. {
  13. for(int c=0; c<4; c++)
  14. {
  15. cards[r][c]=(int) (Math.random() * 8);
  16. System.out.print(cards[r][c]);
  17. }
  18. System.out.print("\n");
  19. }
  20. for (int r=0; r<4; r++)
  21. {
  22. for (int c=0; c<4; c++)
  23. {
  24. System.out.print("*");
  25. }
  26. System.out.print("\n");
  27. }
  28. System.out.print("\nPlease insert the first card row.");
  29. r1=Integer.parseInt(a.readLine());
  30. System.out.print("\nPlease insert the first card column.");
  31. c1=Integer.parseInt(a.readLine());
  32. System.out.print("\nPlease insert the second card row.");
  33. r2=Integer.parseInt(a.readLine());
  34. System.out.print("\nPlease insert the first card column.");
  35. c2=Integer.parseInt(a.readLine());
  36. //reveal
  37. for(int r=0; r<4; r++)
  38. {
  39. for (int c=0; c<4; c++)
  40. {
  41. if((r==r1)&&(c==c1))
  42. {
  43. System.out.print(cards[r][c]);
  44. }
  45. else if((r==r2)&&(c==c2))
  46. {
  47. System.out.print(cards[r][c]);
  48. }
  49. else
  50. {
  51. System.out.print("*");
  52. }
  53. }
  54. //match?
  55. if (cards[r1][c1]==cards[r2][c2])
  56. {
  57. }
  58. else
  59. {
  60. }
  61. }
  62. }
  63. }
the output is this:
  1. 5446
  2. 5611
  3. 4143
  4. 4601
  5. ****
  6. ****
  7. ****
  8. ****
  9.  
  10. Please insert the first card row.1
  11.  
  12. Please insert the first card column.2
  13.  
  14. Please insert the second card row.3
  15.  
  16. Please insert the second card column.3
  17. ******1********1
how can i fix it?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,144
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: pls help w/ this

 
0
  #4
Dec 1st, 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
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
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

 
0
  #5
Dec 1st, 2008
my problem here is i want the output to be print like this:
example:
  1. Please insert the first card row: 1
  2. Please insert the first card column: 2
  3. Please insert the second card row: 3
  4. Please insert the second card column: 3
  5. ****
  6. **0*
  7. ****
  8. ***2
but i came up with this
  1. Please insert the first card row: 1
  2. Please insert the first card column: 2
  3. Please insert the second card row: 3
  4. Please insert the second card column: 3
  5. ******0********2
how can i fix it?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,144
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 531
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: pls help w/ this

 
0
  #6
Dec 1st, 2008
thats because you are using print not println
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
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

 
0
  #7
Dec 1st, 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 Quick reply to this message  
Join Date: Nov 2008
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

 
0
  #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:
  1. import java.io.*;
  2. public class Num2
  3. {
  4. public static void main(String[]args)throws IOException
  5. {
  6. BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
  7.  
  8. int r1, c1, r2, c2;
  9. int[][]cards;
  10. cards=new int[4][4];
  11. for(int r=0; r<4; r++)
  12. {
  13. for(int c=0; c<4; c++)
  14. {
  15. cards[r][c]=(int) (Math.random() * 8);
  16. System.out.print(cards[r][c]);
  17. }
  18. System.out.print("\n");
  19. }
  20. for (int r=0; r<4; r++)
  21. {
  22. for (int c=0; c<4; c++)
  23. {
  24. System.out.print("*");
  25. }
  26. System.out.print("\n");
  27. }
  28. System.out.print("\nPlease insert the first card row: ");
  29. r1=Integer.parseInt(a.readLine());
  30. System.out.print("\nPlease insert the first card column: ");
  31. c1=Integer.parseInt(a.readLine());
  32. System.out.print("\nPlease insert the second card row: ");
  33. r2=Integer.parseInt(a.readLine());
  34. System.out.print("\nPlease insert the first card column: ");
  35. c2=Integer.parseInt(a.readLine());
  36. //reveal
  37. for(int r=0; r<4; r++)
  38. {
  39. for (int c=0; c<4; c++)
  40. {
  41. if((r==r1)&&(c==c1))
  42. {
  43. System.out.print(cards[r][c]);
  44. }
  45. else if((r==r2)&&(c==c2))
  46. {
  47. System.out.print(cards[r][c]);
  48. }
  49. else
  50. {
  51. System.out.print("*");
  52. }
  53. }
  54. //match?
  55. if (cards[r1][c1]==cards[r2][c2])
  56. {
  57. }
  58. else
  59. {
  60. System.out.println();
  61. }
  62. }
  63. }
  64. }

the output is:
  1. 5264
  2. 7021
  3. 1741
  4. 1556
  5. ****
  6. ****
  7. ****
  8. ****
  9. Please insert the first card row: 1
  10. Please insert the first card column: 2
  11. Please insert the second card row: 3
  12. Please insert the second card column: 3
  13. ****
  14. **2*
  15. ****
  16. ***6
the values in my output are printed also..
before the 4x4 *;
what should i modify?
Last edited by beginner21; Dec 1st, 2008 at 1:19 am. Reason: code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
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: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: pls help w/ this

 
0
  #9
Dec 1st, 2008
Now from your post I get the impression that you want to hide the following part of the output:-

  1. 5264
  2. 7021
  3. 1741
  4. 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 2: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 Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: pls help w/ this

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC