Results 1 to 13 of 13
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By JosAH

Thread: if statement with arrayitem[randNumber] not executing

  1. #1
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default if statement with arrayitem[randNumber] not executing

    Hello, sorry to post so often
    Java Code:
    void replaceNum() {// For String
    			String song = "This old man, he played 1. He played knick-knack on my thumb. With a knick-knack, paddy-whack,Give your dog a bone. This old man came rolling home.";
    			String list[] = { "cat", "dog", "mouse", "lion", "tiger", "rat",
    			"elephant" };
    			// replace 1 in line one with one of the animals randomly
    			Random rand = new Random();
              	
    			String lines[] = song.split("(?=H)|(?=W)|(?=G)|(?=T)");
    
    			for (int m = 1; m <= 6; m++) {
    				System.out.println("Running for the "+ m +" time");
    
    				
    				for (int j = 0; j < lines.length; j++) {
    					
    					int randNum=rand.nextInt(list.length);
    					if (j==0)
    					{			
    
    						lines[0]=lines[0].replace("1", list[randNum]);
    						System.out.println(lines[0]);
    						
    					}
    					else if (j!=0)
    					System.out.println(lines[j]);
    					
    					}
    				System.out.println("==================================");
    				}
    				System.out.println("*********************************");
    
    			}
    Here is the output:
    Java Code:
    Running for the 1  time
    
    This old man, he played 1. 
    He played knick-knack on my thumb. 
    With a knick-knack, paddy-whack,
    Give your dog a bone. 
    This old man came rolling home.
    ==================================
    Running for the 2  time
    
    This old man, he played 1. 
    He played knick-knack on my thumb. 
    With a knick-knack, paddy-whack,
    Give your dog a bone. 
    This old man came rolling home.
    ==================================
    Running for the 3  time
    
    This old man, he played 1. 
    He played knick-knack on my thumb. 
    With a knick-knack, paddy-whack,
    Give your dog a bone. 
    This old man came rolling home.
    ==================================
    Running for the 4  time
    
    This old man, he played 1. 
    He played knick-knack on my thumb. 
    With a knick-knack, paddy-whack,
    Give your dog a bone. 
    This old man came rolling home.
    ==================================
    Running for the 5  time
    
    This old man, he played 1. 
    He played knick-knack on my thumb. 
    With a knick-knack, paddy-whack,
    Give your dog a bone. 
    This old man came rolling home.
    ==================================
    Running for the 6  time
    
    This old man, he played 1. 
    He played knick-knack on my thumb. 
    With a knick-knack, paddy-whack,
    Give your dog a bone. 
    This old man came rolling home.
    ==================================
    *********************************
    In line 23,24 I am trying to replace number "1" in the first line with a value from array list assigning the value based on a pseudorandom number.
    I am not sure why it does not work.

    the if statement works (i tested it);
    the .replace method works (tested .replace("val1", "val2")
    Maybe it is the random number? but i tested it and it does generate a number..
    so I 've run out of possible causes

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,530
    Rep Power
    5

    Default Re: if statement with arrayitem[randNumber] not executing

    The problem is with the regex. One of your split parameters (?=T) matches at the beginning of the song. So line[0] = "". Also, keep in mind that once you replace "1" with an animal, then your replace statement won't work for other animals because "1" won't exist in line[0] anymore.

    Regards,
    Jim
    sayswho likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default Re: if statement with arrayitem[randNumber] not executing

    Thank you I was wondering why I was getting that empty line at the top.
    I was going to split it by regex(match dot followed by white space)..which works except for the line that ",Give".

    Here is match dot followed by white space:
    Java Code:
     (?=[\\.][\\s])
    I thought I can use match dot followed by white space and G:

    Java Code:
    (?=[\\.][\\s])(?=G)"
    but then it is searching for dot followed by white space and followed by G - which returns 0 match.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: if statement with arrayitem[randNumber] not executing

    Hmm. Why use split in the first place, instead of just assigning the array right off the bat?

    The other alternative is to do what you did in the OP and just start from array-index 1. Either should get rid of the blank line.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: if statement with arrayitem[randNumber] not executing

    The output is definitely not produced by that method;

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: if statement with arrayitem[randNumber] not executing

    What makes you say that? It looks right to me...

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: if statement with arrayitem[randNumber] not executing

    Quote Originally Posted by Toll View Post
    What makes you say that? It looks right to me...
    This line:

    Java Code:
     System.out.println("Running for the "+ m +" time");
    can never produce this output:

    Java Code:
    Running for the 1  time
    (count the spaces between the number and 'time')

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,199
    Rep Power
    19

    Default Re: if statement with arrayitem[randNumber] not executing

    Sharp eyes, Jos!
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: if statement with arrayitem[randNumber] not executing

    Quote Originally Posted by DarrylBurke View Post
    Sharp eyes, Jos!
    Yep, I got them from my old Cobol programming (all those darn 'PIC XXXXXXXXX' thingies and their misaligned output) I'm extremly good at spotting those spaces, semi colons and other useless stuff ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: if statement with arrayitem[randNumber] not executing

    Sharp eyes indeed, but I don't see why you'd make a point out of it though. Whenever I post code I also tend to give it a once-over to clean it up a little, which may make code and generated output slightly different.

  11. #11
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: if statement with arrayitem[randNumber] not executing

    The problem is that if one thing changed, then something else (seemingly innocent, but perhaps important) might've changed as well. In this case, I agree it's not an important change, but it certainly could've been.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,199
    Rep Power
    19

    Default Re: if statement with arrayitem[randNumber] not executing

    More than anything else, there's the real possibility that the discrepancy arises out of not recompiling after making (any number of) changes.

    There's an interesting page about this on another forum: Post Real Code at JavaRanch

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: if statement with arrayitem[randNumber] not executing

    I am not ignoring you lot, I just choose to delay any kind of response to that to a Friday afternoon off-topic banter thread ;)

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. If statement executing when false...
    By ribbs2521 in forum New To Java
    Replies: 6
    Last Post: 10-26-2009, 05:19 PM
  4. If statement not executing correctly
    By gligor_kot in forum New To Java
    Replies: 5
    Last Post: 08-03-2009, 01:46 AM
  5. Replies: 2
    Last Post: 07-18-2008, 01:13 PM

Posting Permissions

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