Results 1 to 18 of 18
  1. #1
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  3. #3
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Conditional Statement with null

    Im thinking in the for loop in the toString method?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    Quote Originally Posted by Bowsan22 View Post
    Im thinking in the for loop in the toString method?
    Exactly! Check the value held by the array before you append the array contents to the newCard String. Give it a try and let us know what happens.

  5. #5
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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 05:59 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    Quote Originally Posted by Bowsan22 View Post
    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.
    Without seeing your latest code attempt and its output, I'm afraid I don't know how to help you. I'm not going to write this for you of course.

  7. #7
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Conditional Statement with null

    Woops totally thought I put that in.

    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];					
    			}
    output:

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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    So, what does this line do?:
    Java Code:
    System.out.print("****");
    How does it effect the eventual output (the newCard String)?

  9. #9
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    Quote Originally Posted by Bowsan22 View Post
    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?
    Your null is being put into what String? Or put another way, what String when being printed out displays:

    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

  11. #11
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default Re: Conditional Statement with null

    Would it be the newCard string?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    Quote Originally Posted by Bowsan22 View Post
    Would it be the newCard string?
    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.

  13. #13
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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)

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    Quote Originally Posted by Bowsan22 View Post
    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)
    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.

  15. #15
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  17. #17
    Bowsan22 is offline Member
    Join Date
    Sep 2011
    Posts
    17
    Rep Power
    0

    Default 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!

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Conditional Statement with null

    Glad you've got it working, and more importantly, you did it yourself!

Similar Threads

  1. using the mousedrag in a conditional loop
    By trishtren in forum New To Java
    Replies: 4
    Last Post: 04-23-2011, 04:55 PM
  2. conditional statement (part 2) please checxk my codes!
    By blindfolded916 in forum New To Java
    Replies: 8
    Last Post: 07-12-2010, 04:30 AM
  3. conditional statement
    By blindfolded916 in forum New To Java
    Replies: 12
    Last Post: 07-11-2010, 10:09 AM
  4. how to do conditional looping?
    By chennee72 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-09-2008, 01:38 PM
  5. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •