Right now my output is like this:
2, 4, 6, 8, 10,
What I want:
2, 4, 6, 8, 10
PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("numbers.dat"))); for(int i = start; i <= 100; i = i + 2) { fout.print(i+","); }
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Right now my output is like this:
2, 4, 6, 8, 10,
What I want:
2, 4, 6, 8, 10
PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("numbers.dat"))); for(int i = start; i <= 100; i = i + 2) { fout.print(i+","); }
Many ways: a) Print the comma out at the start of every loop iteration except the first (use a conditional), or b) print it out at the end of every loop iteration except for the last (use a conditional) c) limit your loop to miss the first or last iteration (depending upon choice) and print those separately (before or after, respectively).
try thiscount=0; for ..{ if (count<4){ fout.print(i+",");count++;} else {fout.print(i);count=0;} }
Above code will only work for given set of numbers. What if there are only 3 numbers? 20 Numbers? 10000 numbers?
It is easier to determine when you are at the first number than the last number. So print a comma before each number except the first.
Here i will show you the pseudocode.
PRINT array[FIRST_INDEX]; LOOP I=1 TO array.LENGTH STEP 1 PRINT "'"; PRINT array[I]; END LOOP