Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By DarrylBurke
  • 1 Post By JosAH
  • 1 Post By JosAH

Thread: If statement issue in Array List with Random generator

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default If statement issue in Array List with Random generator

    Hi All! I am doing exercise from the book, here is the task in short: Write a program that generates a sequence of 20 random die tosses and that prints the die values, marking the runs by including them in the parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 3 1. here is what I have so far:

    Java Code:
    //PE 7.9
    /**
     * Generates a sequence of 20 random die tosses and prints the 
     * die values, marking the runs by including them in parentheses.
     * @author FOX
     *
     */
    
    import java.util.Random;
    import java.util.ArrayList;
    
    public class Run {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Random generator = new Random();
    		ArrayList<Integer> tosses = new ArrayList<>();
    		for (int i = 0; i < 20; i++)
    		{
    			tosses.add(generator.nextInt(6)+1);
    		}
    		System.out.println(tosses);
    		
    		boolean inRun = false;
    		System.out.print(tosses.get(0) + " ");
    		
    		for (int i = 1; i < tosses.size()-1; i++)
    		{
    			if (inRun)
    			{
    				if (tosses.get(i) !=tosses.get(i)-1)
    				{
    					System.out.print(")");
    					inRun = false;
    				}
    			}
    			if (!inRun)
    			{
    				if(tosses.get(i) == tosses.get(i)+1)
    				{
    					System.out.print("(");
    					inRun = true;
    				}
    			}
    			System.out.print(" " + tosses.get(i) + " ");
    		}
    		System.out.print(" " + tosses.get(tosses.size()-1));
    		if (inRun)
    			System.out.println(")");
    
    	}
    
    }
    The problem is it is not printing parentheses even I have two or more consecutive integers as it should. Somehow it skips the if condition. I could tell it for example by putting System.out.println("I am here");. Maybe I messed up with logic? Have no idea, can someone tell me what I am doing wrong here? Thanks in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: If statement issue in Array List with Random generator

    Never use == with reference types. Use the .equals(...) method instead.

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

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

    Default Re: If statement issue in Array List with Random generator

    Quote Originally Posted by DarrylBurke View Post
    Never use == with reference types. Use the .equals(...) method instead.

    db
    Things work correctly here (by accident, I'm sure ;-) The Integers in that List<Integer> are created by auto boxing with the Integer.valueOf( ... ) method; values 1 ... 6 are all less than 128 so they are cached.

    kind regards,

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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: If statement issue in Array List with Random generator

    That's good for small Integer values, but it's better to adopt good habits :)

    Apart from that, I see two if conditions one of which will always be true and the other always false.

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

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

    Default Re: If statement issue in Array List with Random generator

    Quote Originally Posted by DarrylBurke View Post
    That's good for small Integer values, but it's better to adopt good habits :)

    Apart from that, I see two if conditions one of which will always be true and the other always false.
    Yep, the code is a bit of a mess; @OP: carefully read the conditions at line #34 and line #42; they don't make sense.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: If statement issue in Array List with Random generator

    that is beyond my knowledge :D

  7. #7
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    54
    Rep Power
    0

    Default Re: If statement issue in Array List with Random generator

    had to completely rearrange loops, in addition it should be tosses.get(i+1) instead of tosses.get(i)+1....

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

    Default Re: If statement issue in Array List with Random generator

    Quote Originally Posted by FOX427 View Post
    had to completely rearrange loops, in addition it should be tosses.get(i+1) instead of tosses.get(i)+1....
    Yep, that's what I was hinting at; now also implement Darryl's suggestion (see above) because otherwise your code might work for reasons beyond your knowledge (I'm serious here). w.r.t. your problem: try to find two index values si en ei that are the index values of the start of a 'run' and ei is the index value just one value beyond the end of that run. If ei-si > 1 you have to print parentheses and continue your search at position ei until you're done.

    kind regards,

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

Similar Threads

  1. Adding random number to array list
    By Michael305rodri in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 09:50 AM
  2. Replies: 5
    Last Post: 10-24-2012, 09:17 PM
  3. Issue with filling array full of random values
    By quinnvanorder in forum New To Java
    Replies: 5
    Last Post: 05-20-2012, 01:04 AM
  4. Fill Array with Numbers from Random generator [HELP]
    By s_mxyzptlk in forum New To Java
    Replies: 3
    Last Post: 03-07-2012, 02:06 AM
  5. Replies: 6
    Last Post: 09-05-2011, 01:25 PM

Tags for this Thread

Posting Permissions

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