How to format an array of integers in java - java

Currently I have the following method:
public static void printDoubleIntArray(int[][] doubleIntArray) {
for (int x = 0; x < doubleIntArray.length; x++) {
System.out.println();
for (int y = 0; y < doubleIntArray[x].length; y++) {
System.out.print(doubleIntArray[x][y]);
}
}
}
It works perfectly when the parameter "doubleIntArray" is only numbers from 0 - 9 like in the following print out:
0000000
0000300
0033332
0023323
0022223
0023233
0003332
However, if the integers in each element of the array become larger than 9 then I get something like the following:
0000000
000121797
001717171716
0101617171617
001616161617
081617161717
001417171716
What I would like to know is how do I make the above example print out like so:
0 0 0 0 0 0 0
0 0 0 12 17 9 7
0 0 17 17 17 17 16
0 10 16 17 17 16 17
0 0 16 16 16 16 17
0 8 16 17 16 17 17
0 0 14 17 17 17 16

You could try using java.text.NumberFormat and a pattern that displays every number with a fixed width.. Then concatenate them all in a single line...

System.out.print(doubleIntArray[x][y] + "\t");
\t prints a tab
but in this case it will print something like this: (but i guess thats okay for you)
0 0 0 0 0 0 0
0 0 0 12 17 9 7
0 0 17 17 17 17 16
0 10 16 17 17 16 17
0 0 16 16 16 16 17
0 8 16 17 16 17 17
0 0 14 17 17 17 16

Or use String.format("%4d", myinteger) to have each integer occupy 4 chars, and be properly right padded.

System.out.printf("%4d", doubleIntArray[x][y]);
4 represents the minimum space that the number will print.
The other options for this method are explained here

You have 2 options. Firstly, you can use \t in the second for loop. But I think you could add \t and whitespace character to avoid deterioration. Can provide this too, adding if-else structure in second for loop. I mean
if(doubleIntArray[y].length<10){
System.out.print(doubleIntArray[x][y] + "\t ");
//Tab+two whitespace.
} else {
if(doubleIntArray[y].length>10) {
System.out.print(doubleIntArray[x][y] + "\t ");
//Tab+one whitespace
} else {
System.out.print(doubleIntArray[x][y] + "\t");
//Tab+NO whitespace
}
}
Logic is that I think. Sorry for my answers design. I am on the bus now and I cannot write smoothly. If I had a mistake sorry about that again.

public static void printDoubleIntArray(int[][] doubleIntArray) {
for (int x = 0; x < doubleIntArray.length; x++) {
System.out.println();
for (int y = 0; y < doubleIntArray[x].length; y++) {
System.out.print(doubleIntArray[x][y],"\t");
}
System.out.print("\n");
}
}

Related

How can I print out 1-50 even numbers only, but the it start a new line every multiples of 10. (JAVA)

I'm a newbie, but I'm willing to learn how to code.
I tried using this code:
int n = 50;
int counter = 0;
System.out.print("Even Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
counter++;
if (counter == 2) {
System.out.println(i + " ");
counter = 0;
%10== 0
to find all even numbers between 1 to 50 and make a new line at multiples of 10 just follow these steps -
Make one loop which will go 1 to 50
Check if the number is even by checking the remainder after diving it with 2, if YES print that number.
Check if the number is a multiple of 10 by checking the remainder after dividing it by 10, if YES make a new line
The code will look something like this -
int i = 1;
while(i<=50){
if(i%2 == 0) System.out.print(i + " ");
if(i%10 == 0) System.out.println();
i++;
}
Output -
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
It's up to you which looping method you want to use for me While loop looks cleaner.
I hope this solves all your queries.
PFB Snippet:
public class Main
{
public static void main(String[] args) {
for(int i=1;i<=50;i++){
if (i%2 == 0) //Check whether number is even
{
System.out.print(i+" ");
if (i%10 == 0) // Check if it is multiple of 10
{
System.out.print("\n");
}
}
}
}
}
Output:
2 4 6 8 10
12 14 16 18 20
22 24 26 28 30
32 34 36 38 40
42 44 46 48 50
"\n" is a Escape Sequence which means new line

Creating Triangular Multiplication Table using Do-while Loop

I want to ask a question or a probable favor on how am I going to make my program coding "do-while loop" in creating a Triangular Multiplication. Is there a probable way on to create such thing without using any other statement?
public class Main {
static void ssbr(int n) {
int i = 1;
do{
System.out.printf("%4d", n * i);
i = i + 1;
} while(i <= 7);
System.out.println("");
}
public static void main(String[] args) {
int i = 1;
do{
ssbr(i);
i = i + 1;
} while (i <= 7);
}
}
Output it gave:
1 2 3 4 5 6 7
2 4 6 8 10 11 12
3 6 9 12 15 18 21
4 8 12 16 20 24 30
5 10 15 20 25 30 35
6 12 18 24 30 36 42
7 14 21 28 35 42 49
Output I wanted:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
You can do it with the following algorithm:
You have to do it 7 times and therefore you can use a loop that should run 7 times.
Each row starts with the row number, and run for row number * row number times with a step-value equal to the row number.
Given below is the implementation of this algorithm using for loop and I leave it to you to implement it using the do-while loop (as it seems to be your homework 😀)
public class Main {
public static void main(String[] args) {
int n = 7;
for (int row = 1; row <= n; row++) {
for (int col = row; col <= row * row; col += row) {
System.out.printf("%-4d", col);
}
System.out.println();
}
}
}
Output:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49

Array Printf issue

I'm working on simple program that is supposed to list the numeric values in an array; a certain way. This is how I would like to have the output look like:
Printing Array:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22
It has to be lined as seen above and line must only contain 10 numbers.
I seem to have everything formatted correctly but my output doesn't look like that.
Here is what I am getting:
Printing Array:
1
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22
I'm not sure exactly what I'm doing wrong but here's my code:
//disregard the name 'Juice', I like to give my programs weird names
public class Juice
{
public static void main(String[] args)
{
//sets up the array
int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};
//title
System.out.println("Printing Array: ");
//counting the elements
for (int i = 0; i < numbers.length; i++)
{
//prints each element value with 4 spaces in between
System.out.printf("%4d", numbers[i]);
//once line reaches ten values; print new line
if (i % 10 == 0)
{
System.out.printf("\n");
}
}
}
}
if ((i+1) % 10 == 0)
{
System.out.printf("\n");
}
Your code does what you asked it to.
On first loop, i=0, but i % 10 == 0 is also true, so it prints new line.
You can use many different approaches to fix this, but probably easiest one will be to replace this condition to (i+1) % 10 == 0 or to i % 10 == 9.
you almost did it
public class Juice
{
public static void main(String[] args)
{
//sets up the array
int[] numbers = {1,2,3,4,5,6,7,8,9,10,12,11,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};
//title
System.out.println("Printing Array: ");
//counting the elements
for (int i = 0; i < numbers.length; i++)
{
//prints each element value with 4 spaces in between
System.out.printf("%4d", numbers[i]);
//once line reaches ten values; print new line
if (i % 10 == 9)
{
System.out.printf("\n");
}
}
}
}
i've modified a conditions to if (i % 10 == 9)
OUTPUT
Printing Array:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32
Alternatively to avoid the confusion between the index to the array element and the element count switch to using a foreach loop.
//counting the elements
int i = 1;
for (int number : numbers) {
//prints each element value with 4 spaces in between
System.out.printf("%4d", number);
//once line reaches ten values; print new line
if (i % 10 == 0) {
System.out.println();
}
i++;
}

for increment java issue

I'm new to Java. I am trying to create a multiplication table with 12 on each side of the table, so 12 going to the right and 12 going down. On each line, we will see the two values multiple. So my plan is to use 12 very similar for statements to print each of the twelve lines. One value will increment within a loop. The issue is, the first line isn't increment my y value. So it just prints out spaced out 1s.
If you have any suggestions on my latter part of the assignment, it'd be helpful. Once I get the first line to print 12 digits, I can make 11 other for statements. But I feel like there may be a simpler way to get the rest of the statements.
public class Sixthree
{
public static void main (String[] args)
{
int x = 1;
int y = 1;
System.out.print(" ");
for ( int c= x*y; y<= 12; y++)
{
System.out.print(c + " ");
}
}
}
I want the out put to look like this to start off with.:
1 2 3 4 5 6 7 8 9 10 11 12
But the current output looks like this:
1 1 1 1 1 1 1 1 1 1 1 1 1
But I want it to eventually like this: http://math.about.com/blgrid.htm
But without the blue lines.
You are getting all 1s because the loop initialization statement int c= x*y will be executed only once for a for loop. That is it is executed the first time when x=1 and y=1 and since, it is given as the loop initialisation statement and not in the loop body, it is never reevaluated. The for loop works as :
The loop initialisation statement is executed only once at the beginning of the loop. After each iteration the loop update expression is executed and the loop condition is reevaluated. for(loop_initialisation;loop_condition;loop_update) { ... }
So you should update c inside the loop, something like :
for ( int c= x*y; y<= 12; y++)
{
c = x*y;
System.out.print(c + " ");
}
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= 12; j++){
System.out.printf ("%3d ", j * i);
}
System.out.print ("\n");
}
The above code will give you output similar to what is shown below:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8 10 12 14 16 18 20 22 24
3 6 9 12 15 18 21 24 27 30 33 36
4 8 12 16 20 24 28 32 36 40 44 48
5 10 15 20 25 30 35 40 45 50 55 60
6 12 18 24 30 36 42 48 54 60 66 72
7 14 21 28 35 42 49 56 63 70 77 84
8 16 24 32 40 48 56 64 72 80 88 96
9 18 27 36 45 54 63 72 81 90 99 108
10 20 30 40 50 60 70 80 90 100 110 120
11 22 33 44 55 66 77 88 99 110 121 132
12 24 36 48 60 72 84 96 108 120 132 144
You should be using two nested for loops, one to iterate over the values of x, another to iterate over the values of y with each inner loop printing the value of x * y and each outer loop printing a new line character for formatting.
// Pseudo-code //
for(each x) {
for(each y) {
print(product);
}
print(newline);
}
Why it just prints out spaced out 1s ?
It is because you just assign c only once in for-loop. for ( int c= x*y; y<= 12; y++)
When value y is incvreasing, value c is not changing. The values is 1*1=1 (x=1, y=1).
As a result, you see it just prints out spaced out 1s.
You can use nested for loop to implement it.
public class Sixteen {
public static void main(String[] args) {
int x = 12;
int y = 12;
for (int i = 1; i <= x; i++) {
for (int j = 1; j <= y; j++) {
System.out.printf("%d ", i * j);
}
System.out.println();
}
}
}
for (int x = 1; x <= 12; x++)
{
for (int y = 1; y <= 12; y++)
{
int multiply = x * y;
System.out.print(multiply + "\t");
}
System.out.println();
}

Method prints table incorrectly

I was wondering if someone could help me with a small problem I'm having.
I am trying to print out an array of 100 items, every row needs 10 items, after which it starts a new row. I have done this successfully, however, every row in every column displays the same number. For example. My output would be:
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
When it should be appearing as something along the lines of:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Here is the following method I am using to print the table:
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 10; j++) {
// After 10 go to a new line
System.out.printf("%d\t", emirps[i]);
}
System.out.println("");
}
}
If anyone can help me pinpoint where I have goofed I'd greatly appreciate it.
You are printing the same emirps[i] each time through the inner loop. There are a couple of ways around this. Here's one:
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; i += 10) {
for (int j = 0; j < 10; j++) {
// After 10 go to a new line
System.out.printf("%d\t", emirps[i + j]);
}
System.out.println();
}
}
Here's another (not very elegant):
public static void printTable(int[] emirps) {
for (int i = 0; i < 100; ++i) {
System.out.printf("%d\t", emirps[i]);
if (i % 10 == 9) {
System.out.println();
}
}
}
This looks like homework, so instead of giving you the answer, here's a hint:
For the first iteration of the first loop, i = 0. When this is the case, you increment j from 0 to 9. Note that i stays the same throughout. This is when you print your first line.
Once you're through with the second for loop, i increases by 1 to, and now i = 1. Again, you increment j from 0 to 9, and i stays at 1. This is when you print your second line.
This repeats 100 times, as i goes from 0 to 99.
There are 2 ways of approaching this:
Use one for loop, and figure out when to print an end-of-line character.
Use two for loops, each going up to 10. Figure out your index by simple multiplication and addition.
You should try and implement both methods. It'll help you understand loops better.
You are not using the j index anywhere in this code.
Here is how you should print:
System.out.printf("%d\t", emirps[i+j]);
Notice how in the print statement you are now using both the i and j indices.
Also, to get your row offset working correctly, you should be incrementing i by 10 at each step of the loop.
Alternatively, declare emirps as a 2-dimensional array. Then the signature of printTable() would be:
public static void printTable(int[][] emirps)
In that setup, here is how you would print:
System.out.printf("%d\t", emirps[i][j]);
So in the first setup, i+j together index into a 1-D array. In the second setup, i is the row and j is the column, indexing together into a 2-D array. It looks like you're doing a 1-D array but this other example is for your future reference.
Use the modulo operator. Every time your number can be divided by 10 without a remainder, you can make a new line because you have filled up the row. You obtain the remainder using the 'modulo operator' which is the percent (%) sign. It can be used like /, except that rather than the quotient being the result, the remainder of the division operation is the result.
public static void main(String[] args)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 100; i++)
{
if (i != 0 && i % 10 == 0)
{
builder.append('\n');
}
builder.append(i + " ");
}
System.out.println(builder.toString());
}
Prints out:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99

Categories