Results 1 to 7 of 7
Thread: Fence Post problem
- 01-20-2011, 03:10 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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
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:int num = 1; while( num <= 10) { System.out.print(num); if ( num < 10 ) System.out.print(","); num++; } System.out.println();
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]); } }
- 01-20-2011, 03:17 AM #2
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.
- 01-20-2011, 03:21 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
Sorry, a fence post problem is a problem that prints the comma after the last digit.
that last line of code that reads
should actually readJava 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.Java Code:for(int index = 0; index < rolls.length; index++) System.out.println(rolls[index] + ", ");
- 01-20-2011, 03:27 AM #4
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.
- 01-20-2011, 03:34 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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]); }
- 01-20-2011, 03:40 AM #6
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; }
- 01-20-2011, 03:53 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
first post, array problem
By jaremah in forum New To JavaReplies: 19Last Post: 10-05-2010, 02:27 AM -
DOnt know if 1st post if did, I am VERY sorry for duplicate post. I have error messg
By afisher300 in forum New To JavaReplies: 3Last Post: 05-04-2009, 03:15 AM -
Problem With POST command to control the network camera
By sri_reddy523 in forum NetworkingReplies: 3Last Post: 03-16-2009, 06:53 AM -
First post out of the way..
By sirwiggles in forum IntroductionsReplies: 0Last Post: 02-06-2009, 10:44 PM -
Problem creating a Post script file
By krishnan.1000 in forum New To JavaReplies: 0Last Post: 02-14-2008, 07:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks