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++;
}
Related
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
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
public static void applicationB(int A, int B) {
int number = 1;
for (int row = 0; row < A; row++) {
for (int col = 0; col < B; col++) {
int output = number + row++;
System.out.printf("% 4d", output);
}
// does it skip because of this?
System.out.println("");
}
}
It outputs with A = 20, B = 5
1 2 3 4 5
7 8 9 10 11
13 14 15 16 17
19 20 21 22 23
The correct output should be
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
I cannot figure out how to get it to stop skipping 6, 12, and 18.
Am I just doing this a horrible way? or am I on the right track?
You are incrementing row in two places. Also, it would be easier to just have one loop, and output a line break every B elements (you can use i % B to test for this).
I would suggest the following approach. Iterate over the values you want to print out, from 1 to the maximum value (A). Then, print a newline whenever the remainder of value divided by the number of columns (B) is zero.
for (int value = 1; value <= A; value++) {
System.out.printf("% 4d", value);
if (value % B == 0) {
System.out.println("");
}
}
I am sorry if this has already been answered somewhere, I have spent over an hour searching through many previous questions and none of them were able to help me with this error.
I randomly receive an error, probably 70% of the time:
--------------------Configuration: MergeSort - JDK version 1.7.0_45 <Default> - <Default>- -------------------
Random array: 17 14 3 4 1 10 13 9 3 1 6 9 10 2 17 8 10 5 7 8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at MergeSort.merge(MergeSort.java:64)
at MergeSort.mergeSort(MergeSort.java:26)
at Driver.main(Driver.java:18)
Process completed.
Line 64 would refer to: "scratch[scratch_index] = list[i];"
public void merge(int[] list, int first, int middle, int last)
{
int[] scratch = new int[list.length];
int midpoint = (first+last)/2;
int left_index = first;
int right_index = middle + 1;
int scratch_index = left_index;
while((left_index <= midpoint) && (right_index <= last))
{
if(list[left_index] <= list[right_index])
{
scratch[scratch_index] = list[left_index];
left_index +=1;
}
else
{
scratch[scratch_index] = list[right_index];
right_index +=1;
}
scratch_index +=1;
}
for(int i=left_index;i<=midpoint;i++)
{
scratch[scratch_index] = list[i];
scratch_index +=1;
}
for(int i=right_index;right_index<=last;i++) // I am line 64
{
scratch[scratch_index] = list[i];
scratch_index +=1;
}
for(int i=0;i<list.length;i++)
{
list[i] = scratch[i];
}
}
This is my first question ever on this site, sorry if I have not formatted this question correctly, any advice is appreciated.
If any other information is needed to help me with solving this error, just let me know.
Thank you!
At the comment // I am line 64 You are incrementing the i and not the right_index which will cause the index to keep increasing until reaching index out of bound, so replace this,
for(int i=right_index;right_index<=last;i++) // I am line 64
by this:
for(int i=right_index; i<=last;i++)
Indexes use "zero-based numbering" >> https://en.wikipedia.org/wiki/Zero-based_numbering
String example = "01234";
System.out.println(example.size()); //Prints out 5
System.out.println(exmaple.indexOf("1")); //Prints out 1
Your for statement needs to be checking up TO the last index
for(int i=right_index;right_index<last;i++)
OR (not recommended)
for(int i=right_index;right_index<=last-1;i++) //Works, but not recommended due to conventions
//index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Random array: 17 14 3 4 1 10 13 9 3 1 6 9 10 2 17 8 10 5 7 8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
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");
}
}