I am trying to get a program to work that asks for a user input to count by that number and display 10 numbers per line. I can not get it to display 10 numbers per line. If anyone can explain to me what I am doing wrong?

public static void main(String[] args) 
{
      //final int START = 5;
      final int STOP = 500;
      int NUMBER_PER_LINE = 10;

      Scanner val = new Scanner(System.in);
      System.out.println("Enter your value ");
      int START =val.nextInt();

      for(int i = START; i <= STOP; i +=START)    
      {
          System.out.print(i + "  ");
          if(i % NUMBER_PER_LINE == 0)
             System.out.println();
      val.close();

          }
}

Line 14
Suppose i is counting up in fives, i%10 is 0 every other pass ( 5, 10, 15, 20 etc) , so you get a new line every 2 passes.
You need a separate counter for the 10 items per line.

That is where I am having problems. I have tried adding a second loop as a counter, but it makes it print out in a single column with 10 spaces between each number. Here is that code.

          public static void main(String[] args) 
    {
      //final int START = 5;
      final int STOP = 500;
      //int NUMBER_PER_LINE = 10;

      Scanner val = new Scanner(System.in);
      System.out.println("Enter your value ");
      int START =val.nextInt();

      for(int i = START; i <= STOP; i +=START)    
      {
          System.out.print(i + "  ");
          for (int j = 1; j <10; j++) {
             System.out.println(); 
          }
          val.close();

          }
}
}

It's hard to answer this without giving the actual solution (which we prefer not to do - you learn more by working it out for yourself), but here's some pseudo-code to show how to do this

declare and initialise a counter before the main loop
inside the loop:
        each time you print a value increment the counter
        if the counter modulo 10 is zero print a newline

I have tried with a modulus as well. When I add by 2 it prints out 5 numbers per line, add by 5 and it prints out 2 numbers per line. If i count by 10 its 1 number per line. I really don't know where to go with this.

public static void main(String[] args) 
{
     final int STOP = 500;
      //int NUMBER_PER_LINE = 10;

      Scanner val = new Scanner(System.in);
      System.out.println("Enter your value ");
      int START =val.nextInt();

      int counter = 10;
      for(int i = START; i <= STOP; i +=START)    
      {
          System.out.print(i + "  ");
          if(i % counter == 0)
             System.out.println();
          }
          val.close();
          }
}

Line 11 looks odd to me. I used the following to test and it appears to do exactly as you coded, not as you asked above. Why did you i += START?

import java.util.Scanner;  // Import the Scanner class
public class MyClass
{
    public static void main(String[] args) 
    {
        final int STOP = 500;
        //int NUMBER_PER_LINE = 10;
        Scanner val = new Scanner(System.in);
        System.out.println("Enter your value ");
        int START =val.nextInt();
        int counter = 10;
        for(int i = START; i <= STOP; i +=START)    
        {
          System.out.print(i + "  ");
          if(i % counter == 0)
             System.out.println();
        }
        val.close();
    }
}

Tested at https://www.jdoodle.com/online-java-compiler/

Again, I didn't fix your code. Just wanted to test what you had and well, what's the thinking about your line with the += statement?

The +=START was to start the count by the user input number. I have fixed the code so that it works.

import java.util.Scanner;
public class CountByAnything
{

public static void main(String[] args) 
{

      final int STOP = 500;

      Scanner val = new Scanner(System.in);
      System.out.println("Enter your value ");
      int START =val.nextInt();

      int j = 0;
      for(int i = START; i <= STOP; i +=START)    
      {
          System.out.print(i + "  ");

          j++;
          if (j % 10 == 0)
          {
          System.out.println("");
          }
          val.close();
          }
}
}

The name START is confusing.... it would be better named step, or increment or somesuch. (And all caps implies a constant in Java.)
And while we're at it... why is the Scanner called val? And what doesj mean to the reader?

Naming variables in a program isn't just about reducing typing - it's about making the meaning of the code obvious. Most of the confusion about this code would have been avoided by better naming.

Finally closing the Scanner inside the loop is horriby redundant. Once is enough.

The Java code should look like this:

import java.util.Scanner;  
public class MyClass {
  public static void main(String[] args) 
  {
        //final int START = 5;
        final int STOP = 500;
        int NUMBER_PER_LINE = 10;
        Scanner val = new Scanner(System.in);
        System.out.println("Enter your value ");
        int START =val.nextInt();
        for(int i = START; i <= STOP; i +=START)    
        {
            System.out.print(i + "  ");
            if(i % NUMBER_PER_LINE == 0)
               System.out.println();
        val.close();
            }
  }
}

To learn more about programming language, I would also suggest visiting: Learn Java

commented: Rubbish. Still has the original bug! -3
commented: Spam much? +0

Santosh:

Your code contains exactly the same bug as the original. Didn't you read the whole topic before replying?

Maybe you should visit a good leaning site (eg Oracle's own Java tutorials) before makinga fool of yourself like this.

commented: Checked their posts. Looks like a spammer to me. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.