Drahcir
Drahcir

Reputation: 17

Printing a new line every third integer in java

lets say i want to print out odd numbers from 1 to 10. but it will make a new line every 3rd number. How would you control the number of items you can print on each line in java console.

Here's my code so far,

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);


    int z,t;
    System.out.println("Enter two numbers: ");
    z = input.nextInt();
    t = input.nextInt();

    humbala(z,t);

}

public static void humbala ( int x, int y)
{

    for(int a; x < y; x++)
    {
        if(x % 2 !=0 )
        {
            System.out.print(x+ " ");

            for (int i=0; i<y; i++)
            {
                if(i > 3)
                {
                System.out.println();
                }
                i = 0;

            }



        }

    }

}

}

my for loop is a bit weird. i have a counter variable a but never really used it but it's working anyway. the thing is, if I put x , I get an error saying it's a duplicate I'm thinking, I should make an array. so that every 3rd index, I can print out a new line but I can't translate it to code

Upvotes: 0

Views: 2377

Answers (2)

Sash Sinha
Sash Sinha

Reputation: 22275

If you are using Java8 you could do something like this to print the items on the same line Note you could use an ArrayList instead but I am not sure have learnt about them:

import java.util.Arrays;
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter start number: ");
    int start = input.nextInt();
    System.out.print("Enter end number: ");
    int end = input.nextInt();
    humbala(start, end);
  }

  public static void humbala(int start, int end) {
    int count = 0;
    String[] numbersLine = new String[3];
    for (int i = start; i <= end; i++) {
      if(count != 0 && count % 3 == 0) {
        System.out.println(String.join(" ", numbersLine));
        Arrays.fill(numbersLine, ""); //for last line
      }
      numbersLine[count % 3] = String.valueOf(i);
      count++;
    }
    System.out.println(String.join(" ", numbersLine)); //print any remaining numbers
  }
}

Example usage:

Enter start number:  1
Enter end number:  10
1 2 3
4 5 6
7 8 9
10

Try it here!

Upvotes: 1

Darshan Mehta
Darshan Mehta

Reputation: 30809

You don't need multiple if conditions and an unused variable in for loop, if you just want to print all the numbers and print a new line after every 3 numbers, it can be done with a counter, e.g.:

public static void main(String[] args) throws IOException {
    try(Scanner input = new Scanner(System.in);){
        int z, t;
        System.out.println("Enter two numbers: ");
        z = input.nextInt();
        t = input.nextInt();
        humbala(z, t);
    }
}

public static void humbala(int x, int y) {
    int counter = 0;
    for (int i = x; i <= y; i++) {
        if(counter++ % 3 == 0){
            System.out.println();
        }
        System.out.println(i);
    }
}

Upvotes: 0

Related Questions