943,779 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 822
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 30th, 2008
0

pls help w/ this

Expand Post »
hi everyone,
can someone help me w/ this
here is my code:
Java Syntax (Toggle Plain Text)
  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...
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beginner21 is offline Offline
16 posts
since Nov 2008
Nov 30th, 2008
1

Re: pls help w/ this

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 :-

java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Nov 30th, 2008
0

Re: pls help w/ this

ok,
i get it
here is my modified code:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beginner21 is offline Offline
16 posts
since Nov 2008
Dec 1st, 2008
0

Re: pls help w/ this

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
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,505 posts
since Apr 2005
Dec 1st, 2008
0

Re: pls help w/ this

my problem here is i want the output to be print like this:
example:
Java Syntax (Toggle Plain Text)
  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
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beginner21 is offline Offline
16 posts
since Nov 2008
Dec 1st, 2008
0

Re: pls help w/ this

thats because you are using print not println
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 574
Moderator
jbennet is offline Offline
16,505 posts
since Apr 2005
Dec 1st, 2008
0

Re: pls help w/ this

where is the exact location where in i change the System.out.print into System.out.println?
can you post it?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beginner21 is offline Offline
16 posts
since Nov 2008
Dec 1st, 2008
0

Re: pls help w/ this

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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beginner21 is offline Offline
16 posts
since Nov 2008
Dec 1st, 2008
0

Re: pls help w/ this

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

Java Syntax (Toggle Plain Text)
  1. 5264
  2. 7021
  3. 1741
  4. 1556

That would be simple, just comment out or remove the System.out.print() in the following :-
java Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Dec 1st, 2008
0

Re: pls help w/ this

Quote ...
java Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to send (java code)sms from tomcatserver to mobile using some gateway
Next Thread in Java Forum Timeline: Tree interface for ArrayList





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC