Would someone please helpe me out and tell me where i'm missing up with this code.? I want the result(output) in order to be table of row and column. but i was trying and trying but i couldn't.

import java.util.Scanner;

public class MultiTable {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); // here the program will start.

    int num = -1;
    while(num <=0 || num > 10){   // while loop and collecting numbers between 1 and 10.
    System.out.print("Enter an integer between 1 and 10:");

    num = input.nextInt();
   }
    System.out.println();

    for(int i = 1; i<=num; i++)

    System.out.println("\t"+ i);
    System.out.println("\n");

    for(int row=1; row<=num; row++){
    System.out.println(row + "\t");

    for(int tab=1; tab < row; tab++)
    System.out.println("\t");

    for(int col=row; col<=num; col++){
    System.out.println(row*col + "\t");

    }  //end inter for loop

        System.out.println();

    } //end outer for loop
 }
}

Recommended Answers

All 5 Replies

println always adds a new line at the end of whatever you are printing. If you don't want every print on its own line just use print, or printf if you want more control over formatting.

ps your lack of indentation, and random use/non use of {} for single-line ifs makes your code almost unreadable. If you need to post again please indent correctly and use {} consistently for all if statements.

import java.util.Scanner;

public class Multiable {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); // here the program will start.

    int num = -1;
    while(num <=0 || num > 10){   // while loop and collecting numbers between 1 and 10.
       System.out.print("Enter an integer between 1 and 10:");
       num = input.nextInt();
       System.out.println();
      }

      for(int i = 1; i<=num; i++){

      for(int row = 1; row <=num; row++){
      System.out.print(" " + row);


      for(int col=row; col<=num; col++)
      System.out.print(" " + col*row);


      }//end inter for loop

      System.out.println();

      }//end outer for loop
    }
 }

I have changed certain things in this code and i am still cannot get an arrange table for the output. and sorry i didn't get it james. shall i change one of the for statment to if.? or what exactly .

I'm not going to waste my time trying to understand code that is incorrectly indented and has inconsistent use of {}. But after a quick scan you seem to have 3 nested loops where you only need two (row and col).

sorry man, I know that i have lots of{} and they are not in there actual place. because i am looking to have my output comes as a TABLE of see how it should be
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16

and yes it's a nested loop that i am having in the code. but again the problem is (row and col) are not coming quite as i want them. i can post the last try that i did yesterday. but i posted the first when i said it has to be a solution somehow.
I apologize for posting things incorrect and have mistake on them.

Yup, there's no diifculty understanding what the output should look like. This isn't a difficult program, and it looks like you have it nearly right - the obvious problem being that you still have 3 loops where you only need 2.
The problem is that you are not helping us to help you fix your code! You say "... not coming quite as i want them", and post some out-of-date and unreadable code. How is anyone supposed to help you with that?

There are plenty of people here who will help, but here's what you have to do first:
1. Post a complete, correctly indented listing of the current version of your code with all the {} corrected.
2. Post a sample of the output that code produces so we can see how that differs from what it should be.

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.