# Thread: Parameters with two integers.

1. Member
Join Date
Oct 2011
Posts
4
Rep Power
0

## 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.
Last edited by Hello; 10-25-2011 at 04:16 PM.

2. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## 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.

3. Member
Join Date
Oct 2011
Posts
4
Rep Power
0

## Re: Parameters with two integers.

Hello joey, thanks for reply. I finaly got it to work. Here you can see how i solved this.
Java 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

4. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## 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.

5. ## Re: Parameters with two integers.

Change line #26 to:

Java 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

6. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•