Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


how do i print only 10 values per line? using java

I am trying to get numbers print 10 values at a time. Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 so on and so on.... I am stuck I have tried and tried I cannot seem to figure it out. Can one help me please? below is my current code

package project4;

public class lab {
    public static void main(String args[]){

        int lowerBound = 1; int upperBound = 20; int i = lowerBound; final int STARTINGNUMBER = 1; final int ENDINGNUMBER  = 100; 
        int counter = 10; String output ="";
        //i>=lowerbound && i<=upperbound                           

    while ( i >= lowerBound && i<=upperBound )  {
        System.out.print("\t" + i);
    }//while loop close

    for(i = lowerBound; i >= lowerBound && i <= upperBound; i++){
            System.out.print("\t" + i);

        }//for loop >>>> if <<< close
    }//for loop main close

    for (int x = STARTINGNUMBER; x <= ENDINGNUMBER; x++){
        int number = x;
        int squareRoot = (int) Math.sqrt(number);
        if (squareRoot*squareRoot == number){

            System.out.print("\t" + x);

        }//if loop sqrt close
    }//for loop sqrt Main

2 Answers

Java looks like c++, maybe I can help.

You mean 10 values each line ? or prin 10 values at a time inside the loop ?

If you want to print 10 values each line, you should try this, not sure if it works as I have no idea about java

for(int i = 1; i <= 20; i++) {
   System.out.print(" " + i);
   if(i % 10 == 0)

it works

Java seems fun :), but I'll stick to Objective C for now

Nice yeah that's what I was trying to do :D. Thanks. I did exactly that before I read this :D