Results 1 to 18 of 18
Thread: Conditional Statement with null
- 09-18-2011, 03:54 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Conditional Statement with null
Hey, I got a quick (maybe even stupid) question. So i need to write a program for Bingo in class, and got most of the code down. I just need to be able to print "****" right in the middle of the board. Here is my code so far:
Java Code:public class BingoCard { private BingoSquare[][] myBingoCard = new BingoSquare[5][5]; public BingoCard() { for (int row = 0; row < myBingoCard.length; row++) { for (int col = 0; col < myBingoCard[row].length; col++){ myBingoCard[row][col] = (BingoSquare) new BingoSquare ((int) (col * 15 + 1 + Math.random() * 15)); myBingoCard[2][2] = null; } } } public String toString() { String newCard = ""; for (int row = 0; row < 5; row++) { for(int col = 0; col < 5; col++) { newCard += " " + myBingoCard[row][col]; } newCard += "\n"; } return newCard; } }
Now i made the middle square null, but cant seem to get a condition where I have it output the stars. Any help would be nice.
-
Re: Conditional Statement with null
You're right -- an if block would work here. So where do you think it should go in the code above?
- 09-18-2011, 04:00 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
Im thinking in the for loop in the toString method?
-
Re: Conditional Statement with null
- 09-18-2011, 04:54 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
Hmm I tried it a bit, but I just ended up getting to print a bunch of stars before the bingo board because of the for loop. Im most likely putting the code in the wrong place, but I also feel like Im using the wrong statement in the condition.
Pretty much I dont know how to get the stars to replace null.Last edited by Bowsan22; 09-18-2011 at 04:59 AM.
-
Re: Conditional Statement with null
- 09-18-2011, 05:04 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
Woops totally thought I put that in.
output:Java Code:for (int row = 0; row < 5; row++) { for(int col = 0; col < 5; col++) { if (myBingoCard[2][2] == null) { System.out.print("****"); } newCard += " " + myBingoCard[row][col]; }
************************************************** ************************************************** B 07 I 20 N 32 G 57 O 72
B 05 I 20 N 34 G 57 O 73
B 03 I 23 null G 60 O 72
B 11 I 29 N 35 G 57 O 68
B 08 I 27 N 32 G 56 O 61
-
Re: Conditional Statement with null
So, what does this line do?:
How does it effect the eventual output (the newCard String)?Java Code:System.out.print("****");
- 09-18-2011, 05:13 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
It prints the stars when theres a null in that array position. Just made me realize this >.<, return would just give me the stars without the board because the condition is true. So I have an incorrect "then" clause, and I need to somehow replace the null with the stars correct?
-
Re: Conditional Statement with null
- 09-18-2011, 05:25 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
Would it be the newCard string?
-
Re: Conditional Statement with null
Exactly.
So again, inside of your if block you have a System.out.println(...) statement. Will that in any way change the newCard String? If not, perhaps you want to get rid of the printLn statement and replace it with one that somehow updates the newCard String. And yes, you'll need an else block too for your default behavior.
- 09-18-2011, 05:45 AM #13
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
No the printLn statement wont update it, having trouble figuring out how to actually update it though. if i put newCard = "****"; then it will just replace the board. Lost on how to update this (sorry really suck at programming)
-
Re: Conditional Statement with null
You need to think logically to figure this out. You already know how to add things to the newCard String, you're doing it elsewhere in that for loop, and in fact, that's how you're getting the "null" into newCard String. Again, logically, how would you get your "****" String into the already existing newCard String. Again, look at how you use newCard String in your already existing code. And before you come back here, please experiment a little bit. The best way to learn is not to get stuck and immediately post a question, but to get stuck and keep fiddling with your code.
- 09-18-2011, 06:58 AM #15
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
Ya I spent an hour trying to figure it out just to become more flustered than I already was. Gonna take another look at it tomorrow.
-
Re: Conditional Statement with null
hint, to change your newCard String you are using += which appends the String to the right of this operator to the already existing newCard String. If you do this, you don't lose all the information already held by the String.
- 09-19-2011, 03:28 AM #17
Member
- Join Date
- Sep 2011
- Posts
- 17
- Rep Power
- 0
Re: Conditional Statement with null
Just figured it out in less than a minute, guess I was too tired to properly think last night, thanks for your time and help!
-
Similar Threads
-
using the mousedrag in a conditional loop
By trishtren in forum New To JavaReplies: 4Last Post: 04-23-2011, 03:55 PM -
conditional statement (part 2) please checxk my codes!
By blindfolded916 in forum New To JavaReplies: 8Last Post: 07-12-2010, 03:30 AM -
conditional statement
By blindfolded916 in forum New To JavaReplies: 12Last Post: 07-11-2010, 09:09 AM -
how to do conditional looping?
By chennee72 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 09-09-2008, 12:38 PM -
statement null pointer exception
By bbq in forum JDBCReplies: 1Last Post: 07-05-2007, 04:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks