# Parameters with two integers.

• 10-25-2011, 02:53 PM
Hello
Parameters with two integers.
Hello there, im stuck at something here. I'm new to Java, and i have this issue. I'll try to explain as good as i can.

I have written a method called printNumbers, in this method it has a paramater including two integers ( public static int printNumbers (int x, int y) ), i want it to print all the numbers from 1 up to (and including) the value of the first parameter. The second parameter detemines how many numbers to be printed on each line. And in the main method i use scanner as user input. Could anyone help me out here?
Here is an "psudo" picture i made to illustrate the problem.
Attachment 1911
• 10-25-2011, 05:49 PM
joeyvitoro
Re: Parameters with two integers.
If I understand your problem correctly...

int total = 0;

for(int i=1;i<x+1;i++)
{
total+=x;
}

You are probably already using a loop to print so just add this in there. Remember that you are starting at 1, not 0.
• 10-25-2011, 08:24 PM
Hello
Re: Parameters with two integers.
Hello joey, thanks for reply. I finaly got it to work. Here you can see how i solved this.
Code:

```import java.util.Scanner; public class Boka {         public static void main (String args[]){                                 Scanner input = new Scanner(System.in);                                 int number, line;                                 System.out.print("Max number: ");                 number = input.nextInt();                                 System.out.print("Numbers each line: ");                 line = input.nextInt();                                 System.out.print( printNumbers(number, line) );                                         }         public static int printNumbers(int x, int y){                                                                 for (int i = 1; i < x; i++){                                                                         if (i % y == 0){                                 System.out.printf("%3s \n", i );                                                 }                         else {                                 System.out.printf("%3s", i );                         }                 }                 System.out.print(" ");                 return x;                                                                                 }         }```
Is there a better way / less code etc.?

Kind Regards Danny
• 10-25-2011, 08:31 PM
joeyvitoro
Re: Parameters with two integers.
There is ALWAYS a leaner way. Check out ternary operators if you want to have some fun. For readability, though, yours is fine.
• 10-25-2011, 08:35 PM
JosAH
Re: Parameters with two integers.
Change line #26 to:

Code:

`    for (int i = 1; i <= x; i++){`
Your version forgot that last number (and you don't have to print it in your main( ... ) method as an afterthought)

kind regards,

Jos
• 10-25-2011, 08:38 PM
joeyvitoro
Re: Parameters with two integers.
Interestingly, I think he got it by accident. He is returning x for some reason, which is being printed out in main.