| | |
pls help w/ this
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
hi everyone,
can someone help me w/ this
here is my code:
my problem here is once i entered the row and columns,there is nothing happens..pls help,
here's the error...
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
can someone help me w/ this
here is my code:
Java Syntax (Toggle Plain Text)
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(); } } }
here's the error...
Java Syntax (Toggle Plain Text)
---------- 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"
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
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
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 :-
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
Now let me tell you how exactly
And since
To get over this problem I would suggest you store whatever
OR
You could always accept the row and column values on separate lines without the comma.
.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)
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.
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 ?"
"How to ask questions the smart way ?"
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
ok,
i get it
here is my modified code:
the output is this:
how can i fix it?
i get it
here is my modified code:
Java Syntax (Toggle Plain Text)
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 { } } } }
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
my problem here is i want the output to be print like this:
example:
but i came up with this
how can i fix it?
example:
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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:
the output is:
the values in my output are printed also..
before the 4x4 *;
what should i modify?
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)
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:
Java Syntax (Toggle Plain Text)
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
before the 4x4 *;
what should i modify?
Last edited by beginner21; Dec 1st, 2008 at 1:19 am. Reason: code tags
Now from your post I get the impression that you want to hide the following part of the output:-
That would be simple, just comment out or remove the
They are the first nested
Java Syntax (Toggle Plain Text)
5264 7021 1741 1556
That would be simple, just comment out or remove the
System.out.print() in the following :- java Syntax (Toggle Plain Text)
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. 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 ?"
"How to ask questions the smart way ?"
•
•
•
•
java Syntax (Toggle Plain Text)
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"); }
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.
![]() |
Similar Threads
- windows xp installation error!! pls help! (Troubleshooting Dead Machines)
- Help me remove Online Poker Pop up pls (Viruses, Spyware and other Nasties)
- hi friends pls help me out (C++)
- pls.help me im begging you!!! (C)
- pls... i need help!!! (C)
- Pls Hlp me with Hijackthis log file. (Viruses, Spyware and other Nasties)
- Pls help with my HJT log (Viruses, Spyware and other Nasties)
- can somebody pls. help me out with my HJT log.. (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: How to send (java code)sms from tomcatserver to mobile using some gateway
- Next Thread: Final Year Project (Chat System)
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health hql html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






