How to line up numbers correctly [duplicate] - java

This question already has answers here:
Align printf output in Java
(5 answers)
Closed 23 days ago.
I am currently working on a program which displays the multiplication table, but I'm not sure how to line them up properly, as the spacing varied, making the table not correct.
void Multiply_Table(){//open method
int i = 0, ii = 0, iii = 0, iv = 0, v = 0, vi = 0, vii = 0, viii = 0, ix = 0, x = 0; //VARIABLES
System.out.println("");
System.out.println("--------------- MULTIPLICATION TABLE ---------------");
System.out.println("");
System.out.println(" | 1 2 3 4 5 6 7 8 9 10 ");
System.out.println("---|------------------------------------------------");
System.out.print("1 | "); //NUMBER 1
while ( i < 10 ){
i+=1;
System.out.print( i + " " );
}
System.out.print("\n2 | "); //NUMBER 2
while ( ii < 20 ){
ii+=2;
System.out.print( ii + " " );
}
System.out.print("\n3 | "); //NUMBER 3
while ( iii < 30 ){
iii+=3;
System.out.print( iii + " " );
}
System.out.print("\n4 | "); //NUMBER 4
while ( iv < 40 ){
iv+=4;
System.out.print( iv + " " );
}
System.out.print("\n5 | "); //NUMBER 5
while ( v < 50 ){
v+=5;
System.out.print( v + " " );
}
System.out.print("\n6 | "); //NUMBER 6
while ( vi < 60 ){
vi+=6;
System.out.print( vi + " " );
}
System.out.print("\n7 | "); //NUMBER 7
while ( vii < 70 ){
vii+=7;
System.out.print( vii + " " );
}
System.out.print("\n8 | "); //NUMBER 8
while ( viii < 80 ){
viii+=8;
System.out.print( viii + " " );
}
System.out.print("\n9 | "); //NUMBER 9
while ( ix < 90 ){
ix+=9;
System.out.print( ix + " " );
}
System.out.print("\n10 | "); //NUMBER 10
while ( x < 100 ){
x+=10;
System.out.print( x + " " );
}
}//close method
I tried different spacing, but that didn't work.

I guess the format you need like this, having a try:
void Another_Multiply_Table() {
int rowCount = 10; int columnCount = 10;
System.out.println();
System.out.println("--------------- MULTIPLICATION TABLE ---------------");
System.out.println();
System.out.print(format("", 2) + " | ");
for (int i = 1; i <= columnCount; i++) {
System.out.print(format(i, 4));
}
System.out.println();
System.out.println("---|------------------------------------------------");
for (int row = 1; row <= rowCount; row++) {
System.out.print(format(row, 2) + " | ");
for (int column = row; column <= columnCount * row; column += row) {
System.out.print(format(column, 4));
}
System.out.println();
}
}
String format(Object num, int maxLength) {
String numStr = num + " ";
return numStr.substring(0, maxLength);
}

Related

How do I print the output in a straight vertical line from for loop

I used a for-loop in my code and my output prints vertically, but what I want is it to print horizontally. For example
for (int i = 0; i < 10; i++ ) {
System.out.println("i is " + (i+1) + " | ");
}
For this the output is:
i is 1 |
i is 2 |
i is 3 |
and so on...
The output I want is:
i is 1 | i is 2 | i is 3 | ... and so on
Change it to System.out.print as below:
for (int i = 0; i < 10; i++ ) {
System.out.print("i is " + (i+1) + " | ");
}
That should do.
Use System.out.print instead of System.out.println because println prints at new line.
for (int i = 0; i < 10; i++) {
System.out.print("i is " + (i + 1) + " | ");
}
Println prints a new line while print does not
Use only print() function instead of println()
Follow below code:
for (int i = 0; i < 10; i++ ) {
System.out.print("i is " + (i+1) + " | ");
}

How can I use 'System.out.printf' to make my code look need

How can I use 'System.out.printf' to make my code look need.
I want my calculations to look like this:
not like this:
I have to use the printf function.
public static void main(String[] args) {
//Maken van een scanner met de naam input
Scanner input = new Scanner(System.in);
System.out.print("Welke tafel wilt u printen? ");
int tafelGetal = input.nextInt();
int nummer = 0;
int maxGetal = 10;
System.out.println("De tafel van " + tafelGetal + ":");
do {
int nummer2;
nummer2 = ++nummer;
nummer = nummer2;
int berekening = tafelGetal * nummer;
if (nummer >= 5) {
System.out.print("\t" + berekening + "\t");
}
if (nummer == 5) {
System.out.println(" ");
}
if (nummer < 5) {
System.out.print("\t" + berekening + "\t");
}
} while (nummer != maxGetal);
System.out.println("");
}
reusing some of your var names
int tafelGetal = 3;
int maxGetal = 10;
int numberPerLine = 5;
int columnWidth = 4;
for (int i = 1; i <= maxGetal; i += numberPerLine) { //iterate over output lines
for (int j = 0; j < numberPerLine && i + j <= maxGetal; j++) { //iterate over columns
int berekening = (i + j) * tafelGetal;
System.out.printf("%" + columnWidth + "d", berekening);//format as number with specified width
}
System.out.println();
}
For further details regarding available formats just go here :
https://docs.oracle.com/javase/9/docs/api/java/util/Formatter.html
You can also use String.format() to achieve that:
Example:
for(int i=3; i<=15; i+=3)
System.out.print( p(i, 4) );
System.out.println();
for(int i=18; i<=30; i+=3)
System.out.print( p(i, 4) );
//for padding the string
public static String p(int val, int spaces) {
return String.format("%1$" + spaces + "s", val);
}
OUTPUT:
3 6 9 12 15
18 21 24 27 30

Fill a 2-D array with all the factors of the numbers 1 to 1000 - java

I'm attempting to fill a 2D array with the numbers 1 to 1000 and then show all the factors of those numbers. I then need to find all the prime numbers in the same array and output them. Here's what I have so far, keep in mind that I was hoping to do every step in its own method then return them but have not got that far yet
int i = 0;
//int x = 0;
String primeNumber = "";
int[] [] factorArray = new int [1000] [];
for (int x = 0 ; x < 1000 ; x++)
{
int remainder;
int y;
remainder = x % 2;
y = x / 2;
if (remainder != 0)
System.out.println (x + ": " + "1, " + x);
else if (remainder == 0)
System.out.println (x + ": " + (y) + " , " + (y / 2) + " , " + " 1, " + x);
}
for (i = 1 ; i <= 1000 ; i++)
{
int ctr = 0;
for (int x = i ; x >= 1 ; x--)
{
if (i % x == 0){
ctr = ctr + 1;
}
}
if (ctr == 2)
{
primeNumber = primeNumber + i + " ";
}
}
System.out.print ("Prime numbers from 1 - 1000 are : \n" + primeNumber);

How to break out of a while loop in java?

// Setup dummy array
ArrayList<Integer> list = dateArray;
int counter = 1;
while (list.size() != 0) {
for (int j = 1; j < list.size(); j++)
{
//System.out.println(list.get(0) + " and " + list.get(j));
int difference = list.get(0) - list.get(j);
if (difference <6){
System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size() );
counter= counter +1;
System.out.println ("Counter is " + counter);
if (counter >= 4){
System.out.println ("j = " + j + " Counter =" + counter);
if (j ==list.size()-1) {
System.out.println ("here " + counter);
break;
}
}
}
}
list.remove(0);
};
Output:
1 and 2 and size is 4
Counter is 2
1 and 3 and size is 4
Counter is 3
1 and 4 and size is 4
Counter is 4
here 4
2 and 3 and size is 3
Counter is 5
3 and 4 and size is 2
Counter is 6
here 6
Ideally, i want it to stop when Counter is 4 and don't go on to execute "2 and 3 and size is 3 "
Much appreciated!
You need to use a label for the loop . Please try below snippet :
// Setup dummy array
ArrayList<Integer> list = dateArray;
int counter = 1;
outerwhileloop:
while (list.size() != 0) {
for (int j = 1; j < list.size(); j++)
{
//System.out.println(list.get(0) + " and " + list.get(j));
int difference = list.get(0) - list.get(j);
if (difference <6){
System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size() );
counter= counter +1;
System.out.println ("Counter is " + counter);
if (counter >= 4){
System.out.println ("j = " + j + " Counter =" + counter);
if (j ==list.size()-1) {
System.out.println ("here " + counter);
break outerwhileloop;
}
}
}
}
list.remove(0);
};
Just move the code to a method and call return. Or else, you can use something called labeled break.
search:
for (i = 0; i < arrayOfInts.length; i++) {
for (j = 0; j < arrayOfInts[i].length;
j++) {
if (arrayOfInts[i][j] == searchfor) {
foundIt = true;
break search;
}
}
}
You can use java label for that as:
int x = 1;
outerLoopLabel:
while(x != 10) {
int fact = 0;
for(int i=0;i <=x; i++) {
fact +=i;
if(fact > 25) {
break outerLoopLabel;
}
}
System.out.println("Number: "+ x + " and fact: " + fact);
++x;
}
Output without label :
Number: 1 and fact: 1
Number: 2 and fact: 3
Number: 3 and fact: 6
Number: 4 and fact: 10
Number: 5 and fact: 15
Number: 6 and fact: 21
Number: 7 and fact: 28
Number: 8 and fact: 36
Number: 9 and fact: 45
Output with label:
Number: 1 and fact: 1
Number: 2 and fact: 3
Number: 3 and fact: 6
Number: 4 and fact: 10
Number: 5 and fact: 15
Number: 6 and fact: 21
You can add a boolean variable like this:
// Setup dummy array
ArrayList<Integer> list = dateArray;
int counter = 1;
boolean isBreak = false;
while (list.size() != 0 && !isBreak) {
for (int j = 1; j < list.size(); j++)
{
//System.out.println(list.get(0) + " and " + list.get(j));
int difference = list.get(0) - list.get(j);
if (difference <6){
System.out.println(list.get(0) + " and " + list.get(j) + " and size is " +list.size() );
counter= counter +1;
System.out.println ("Counter is " + counter);
if (counter >= 4){
System.out.println ("j = " + j + " Counter =" + counter);
if (j ==list.size()-1) {
System.out.println ("here " + counter);
isBreak = true;
break;
}
}
}
}
list.remove(0);
};

Tidying code and fixing errors (java)

I need to modify the code so that,
its structure is better
it is more readable
it uses methods and parameters
only one statement needs to be changed if a different number of integers is to be input
My new code is at the bottom but it's not working yet, can't quite figure out what else I need to do to it. If anyone could help that would be fab.
Original Code
import java.util.Scanner;
public class MakeMeBetter
{
public static void main(String[] args)
{
int[] a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Scanner b = new Scanner(System.in);
System.out.println("Please input 10 numbers in the range 1-19 :> ");
int c = b.nextInt();
while (c < 1 || c > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[0] = c;
int d = b.nextInt();
while (d < 1 || d > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[1] = d;
int e = b.nextInt();
while (e < 1 || e > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[2] = e;
int f = b.nextInt();
while (f < 1 || f > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[3] = f;
int g = b.nextInt();
while (g < 1 || g > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[4] = g;
int h = b.nextInt();
while (h < 1 || h > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[5] = h;
int i = b.nextInt();
while (i < 1 || i > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[6] = i;
int j = b.nextInt();
while (j < 1 || j > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[7] = j;
int k = b.nextInt();
while (k < 1 || k > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[8] = k;
int l = b.nextInt();
while (l < 1 || l > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[9] = l;
System.out.println("\nArray contents");
System.out.print((a[0] < 10 ? " " : "") + a[0] + " ");
System.out.print((a[1] < 10 ? " " : "") + a[1] + " ");
System.out.print((a[2] < 10 ? " " : "") + a[2] + " ");
System.out.print((a[3] < 10 ? " " : "") + a[3] + " ");
System.out.print((a[4] < 10 ? " " : "") + a[4] + " ");
System.out.print((a[5] < 10 ? " " : "") + a[5] + " ");
System.out.print((a[6] < 10 ? " " : "") + a[6] + " ");
System.out.print((a[7] < 10 ? " " : "") + a[7] + " ");
System.out.print((a[8] < 10 ? " " : "") + a[8] + " ");
System.out.print((a[9] < 10 ? " " : "") + a[9] + " ");
System.out.println();
boolean noSwap = false;
int startAt = 0;
int stopAt = 9;
while (startAt < stopAt && noSwap == false)
{
noSwap = true;
for (int m=startAt; m<stopAt; m++)
{
if (a[m] > a[m+1])
{
int t = a[m];
a[m] = a[m+1];
a[m+1] = t;
noSwap = false;
}
}
stopAt = stopAt - 1;
}
System.out.println("\nArray contents");
System.out.print((a[0] < 10 ? " " : "") + a[0] + " ");
System.out.print((a[1] < 10 ? " " : "") + a[1] + " ");
System.out.print((a[2] < 10 ? " " : "") + a[2] + " ");
System.out.print((a[3] < 10 ? " " : "") + a[3] + " ");
System.out.print((a[4] < 10 ? " " : "") + a[4] + " ");
System.out.print((a[5] < 10 ? " " : "") + a[5] + " ");
System.out.print((a[6] < 10 ? " " : "") + a[6] + " ");
System.out.print((a[7] < 10 ? " " : "") + a[7] + " ");
System.out.print((a[8] < 10 ? " " : "") + a[8] + " ");
System.out.print((a[9] < 10 ? " " : "") + a[9] + " ");
System.out.println();
double n = (a[0] + a[1] + a[2] + a[3] +
a[4] + a[5] + a[6] + a[7] +
a[8] + a[9]) / 10;
System.out.println("The minimum number is: " + a[0]);
System.out.println("The maximum number is: " + a[9]);
System.out.println("The average value is: " + n);
System.out.println("The median is: " + a[4]);
}
}
New Code
import java.util.Scanner;
import java.math.*;
public class MakeMeBetterImproved {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
int[] numbers = new int[10];
int array = 0;
System.out.println("Enter 10 integers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = kybd.nextInt();
int MyIntArray = array + i;
}
System.out.println("The minimum number is: " + math.min);
System.out.println("The maximum number is: " + math.max);
System.out.println("The average value is: " + math.average);
System.out.println("The median is: " + math.median);
}
}
Consider using a more dynamic structure than a primitive array.
I like ArrayLists: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Reorganize your long code into while or for loops.
An example:
ArrayList<Integer> a = new ArrayList<Integer>();
Scanner b = new Scanner(System.in);
Integer c = null;
while (a.size() != 10) {
System.out.println("Please input a number in the range 1-19 :> ");
c = b.nextInt();
while (c < 1 || c > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
c = b.nextInt();
}
a.add(c);
}
System.out.println("\nArray contents");
for (int i=0; i<10; i++) {
System.out.print((a.get(i) < 10 ? " " : "") + a.get(i) + " ");
}
(And so on...)
Could you try this? I made a few changes.
package p2;
import java.util.Arrays;
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
int[] numbers = new int[10];
int sum = 0;
System.out.println("Enter 10 integers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = kybd.nextInt();
sum += numbers[i];
}
Arrays.sort(numbers);
System.out.println("The minimum number is: " + numbers[0]);
System.out.println("The maximum number is: " + numbers[9]);
System.out.println("The average value is: " + sum / numbers.length);
System.out.println("The median is: " + median(numbers));
}
public static double median(int[] m) {
int middle = m.length / 2;
if (m.length % 2 == 1) {
return m[middle];
} else {
return (m[middle - 1] + m[middle]) / 2.0;
}
}
}

Categories