Help me please....

I have here my code to output a Five Per Line ...try to run this... but my question is...how can i make this the beginning number 1000, not 1001.(because it starts with 1001)

code:

import javax.swing.*;

public class FivePerLine {

public static void main( String [] args) {

int b;

for (b = 1000; b <= 2500; b++) {

if (b % 5 == 1)

System.out.println();

System.out.print(b + " ");

}

System.exit(0);

}

}

[494 byte] By [kimberlystara] at [2007-11-27 10:27:37]
# 1

Change this: "if (b % 5 == 1) "

Hint: 1001 % 5 == 1

~

yawmarka at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 2

Please use a more informative title in future. You wouldn't be posting if you didn't need help.

You probably need to move the print statment before the if statement and you will probably also need an independent variable to keep track of when to print the new line.

floundera at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 3

> You probably need to move the print statment before

> the if statement and you will probably also need an

> independent variable to keep track of when to print

> the new line.

I don't think that helps. It's my understanding the OP wants to print the numbers from 1000 to 2500 in increments of 5. Simply changing the modulus operand will fix the problem.

[EDIT] Oh -- I think I completely misunderstood. Upon re-reading it looks like the OP wants to print the numbers from 1000 to 2500, with a new line every 5 numbers. My bad!

P.S. The order of the statements needs to be changed, but it can still be accomplished with the % operator:#! /usr/bin/groovy

1000.upto(2500) { number ->

print "$number "

if (number % 5 == 4) print '\n'

}

~

yawmarka at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 4

> [EDIT] Oh -- I think I completely

> misunderstood. Upon re-reading it looks like the OP

> wants to print the numbers from 1000 to 2500, with a

> new line every 5 numbers. My bad!

The op is new here and is just learning both java and how to post a well-formed question. Both will get better in time. But one thing I'd like to pass on to her is for her to remember in future posts to post an example of desired output. This can increase the chances that we'll understand what she wants, and increase her odds of getting a correct and helpful answer.

petes1234a at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 5

> The op is new here and is just learning both java and

> how to post a well-formed question. Both will get

> better in time. But one thing I'd like to pass on to

> her is for her to remember in future posts to post an

> example of desired output. This can increase the

> chances that we'll understand what she wants, and

> increase her odds of getting a correct and helpful

> answer.

Great point!

~

yawmarka at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 6

> but it can still be accomplished with the % operator:

That may be true for the given situation but it wouldn't work for all sets of numbers.

floundera at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 7

> > but it can still be accomplished with the %

> operator:

>

> That may be true for the given situation but it

> wouldn't work for all sets of numbers.

What were you suggesting as an alternative? a counter? (sorry, I didn't read all of your posts yet, but am going to do this right now..... I have too much blood in my alcohol system an it is currently slowing down my thought processes........).

petes1234a at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 8

> That may be true for the given situation...

That's the situation I'm addressing. My response was not intended to be a robust, scalable, generic solution. Sorry if it came across that way. ;o)

~

yawmarka at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 9

Yep, a counter and whenever it reaches a multiple of five add a new line.

floundera at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 10

Of course to make it completely dynamic, you write a method that takes three parameters: starting value, ending value and how many numbers per line.

floundera at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 11

> Of course to make it completely dynamic, you write a

> method that takes three parameters: starting value,

> ending value and how many numbers per line.

One could also expose it as a web service. Don't forget about clustering.

:o)

~

yawmarka at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 12

> > That may be true for the given situation...

>

> That's the situation I'm addressing. My response was

> not intended to be a robust, scalable, generic

> solution. Sorry if it came across that way. ;o)

>

> ~

In that case:

System.out.println("1000 1001 1002 1003 1004");

System.out.println("1005 1006 1007 1008 1009");

etc

floundera at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 13

> In that case:

Brillant!

:o)

~

yawmarka at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...
# 14

Of course if you are worried about writing over 250 lines of code:

int num = 1000;

for(int x = 0; x < linesToPrint; x++) {

System.out.println(num + " " + (num + 1) + " " + (num + 2) + " " + (num + 3) + " " + (num + 4));

num += 5;

}

floundera at 2007-7-28 17:46:48 > top of Java-index,Java Essentials,Java Programming...