Results 1 to 7 of 7
  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Fence Post problem

    So I'm having a fence post problem. Unfortunately I have a major exam tomorrow on fence post problems and other problems. My issue is I can actually defeat this problems with simple code like

    Java Code:
     int num = 1; 
       while( num <= 10) {
          System.out.print(num); 
          if ( num < 10 ) 
            System.out.print(","); 
          num++; 
       } 
       System.out.println();
    But in MY code I can't seem to beat this issue any help would be greatly appreciated. Ill be up coding all night to prepare for this exam. Thanks so much. Here is what I'm working with.

    Java Code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class DiceRoll {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		
    	Scanner input = new Scanner(System.in);
    	Random rolling = new Random();
    	
    	System.out.println("How many rolls do you want? ");
    	int roll_number = input.nextInt();
    	
    	int[] rolls = new int[roll_number];
    	
    	for(int index = 0; index < rolls.length; index++){
    		rolls[index] = rolling.nextInt(6) + 1;
    	}
    	
    	for(int index = 0; index < rolls.length; index++)
    		System.out.println(rolls[index]);
    		
    		
    		
    		
    		
    	}
    
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    It would help if you explained what your problem is other than "it doesn't work" and expect others to examine your code line by line to a) determine what you are trying to do and b) where it is going wrong.

  3. #3
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Sorry, a fence post problem is a problem that prints the comma after the last digit.

    that last line of code that reads

    Java Code:
    for(int index = 0; index < rolls.length; index++)
    		System.out.println(rolls[index]);
    should actually read

    Java Code:
    for(int index = 0; index < rolls.length; index++)
    		System.out.println(rolls[index] + ", ");
    But if it reads that it will throw an extra comma on the end. Sorry I was working on it before I posted it and I thought that line was in there, my mistake.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Yeah, I know what a fence post problem is. what I don't know is what your problem is.

    Are you saying that you are unable to modify your DiceRoll class to perform like the code snippet you posted at the top? Surely it is a simple matter of adding an if statement to determine when/when not to add a comma.

  5. #5
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default

    Ok I'm very sorry to waste your time. I believe I figured it out. I appreciate such quick response to the issue.

    Java Code:
    for(int index = 0; index < rolls.length; index++)
    		if (index < rolls.length-1){
    			System.out.println(rolls[index] + ", ");
    		} else {
    			System.out.println(rolls[index]);
    		}

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Glad you got it to work.

    Just a bit of advice. You won't always know when you get to the last value but you will always know when you are at the first (or not at the first value).
    Java Code:
    loop {
        if(not first value) {
            print comma;
        }
        print value;
    }

  7. #7
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

Similar Threads

  1. first post, array problem
    By jaremah in forum New To Java
    Replies: 19
    Last Post: 10-05-2010, 03:27 AM
  2. Replies: 3
    Last Post: 05-04-2009, 04:15 AM
  3. Problem With POST command to control the network camera
    By sri_reddy523 in forum Networking
    Replies: 3
    Last Post: 03-16-2009, 07:53 AM
  4. First post out of the way..
    By sirwiggles in forum Introductions
    Replies: 0
    Last Post: 02-06-2009, 11:44 PM
  5. Problem creating a Post script file
    By krishnan.1000 in forum New To Java
    Replies: 0
    Last Post: 02-14-2008, 08:15 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
  •