I already made the code and i can input values using the JOptionPane Input Dialog.
But the problem is when printing the ouptut of the 2d array it shows like this:
The output should be like this: (can be without the equal and underline)
5 9 8 = 22
3 8 2 = 13
4 3 9 = 16
__________________________
12 20 19
This is the code. I can input values but the only problem is how can i print the matrix.
import javax.swing.JOptionPane;
public class TwoDimensional_Array {
public static void main(String[] args) {
String output = "";
int [][] num = new int [3][3];
int [] sum = new int[10];
int [] sum1 = new int [10];
System.out.println("\n3 by 3 Array - Enter Matrix Elements.");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
String input = JOptionPane.showInputDialog(null, "Enter elements in pockets ["+ i +"]["+ j +"]:", "3 by 3 - Two-Dimensonal Array", -1);
num[i][j] = Integer.parseInt(input);
}
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
sum[i] = sum[i] + num[i][j];
sum1[i] = sum1[i] + num[j][i];
}
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
output += (num[i][j] + "\t");
}
output += ("=\t" + sum[i]); //print the sum of rows
}
output += ("__________________________________");
for(int i = 0; i < 3; i++)
{
output += (sum1[i] + "\t"); // print the sum of columns
}
JOptionPane.showMessageDialog(null, output, "Two-Dimensional Array", -1);
}
}
To get a new line in the dialog, append the \n character instead of the \t character.
Here ist some more information about the Java line separators.
I want to break a one-dimensional array in rows.
Array dimension 50. I need to output the array to the console with 10 elements per line. (lang Java 1.8) Thanks!
public void print() {
for (int i = 0; i < arr.length; i++) {
if (i<=9) {
System.out.print(arr[i] + " ");
}else {
System.out.print("\r");
}
}
}
Sample output
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
etc....
You can see it from 2 differents point of view
Each 10 numbers, print a new line : when the index ends with a 9 you reach ten elements so you print a new line println()
public void print() {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
if (i % 10 == 9) {
System.out.println();
}
}
}
Print enough number of line and on each one : print 10 elements
public void print() {
for (int i = 0; i < arr.length / 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(arr[i * 10 + j] + " ");
}
System.out.println();
}
}
Code for any number of elements per line:
public void print(int elementsPerLine) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i % elementsPerLine == 0 && i > 0) {
System.out.println();
} else {
System.out.print(" ");
}
}
}
Use the following code,
public static void printResult(int[][] result)
{
for(int i=0; i<5; i++)
{
for(int j=0; j<10; j++)
{
System.out.print(result[i][j] + ", ");
}
System.out.println();
}
}
public static int[][] modifyArray( int[] singleArray )
{
int columns = 10;
int rows = singleArray.length/10;
int[][] result = new int[rows][columns];
for(int i=0; i<rows; i++)
{
for(int j=0; j<columns; j++)
{
result[i][j] = singleArray[columns*i + j];
}
}
return result;
}
public static void main(String[] args)
{
int[] singleArray = {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};
int[][] result = modifyArray( singleArray);
printResult( result );
}
You must use the modulo operator
https://en.wikipedia.org/wiki/Modulo_operation
public void print() {
for (int i = 0; i < arr.length; i++) {
if (i%10 == 0) {
System.out.print("\r");
}else {
System.out.print(arr[i] + " ");
}
}
}
Maybe you could write something like
if (i % 10 == 0)
System.out.print("\r");
}
System.out.print(arr[i] + " ");
Take slices of 10 items into a new array each time and print that array:
int count = 10;
int iterations = (arr.length / count) + ((arr.length % count) > 0 ? 1 : 0);
for (int i = 1; i <= iterations; i++) {
int[] slice = new int[count];
System.arraycopy(arr, (i - 1) * count, slice, 0, i == iterations ? (array.length % count) : count);
System.out.println(Arrays.toString(slice));
}
The above code works for any value of count.
Take a look at a code down below:
public class PrintEach10thElements {
/**
* #param args the command line arguments
*/
static List<Integer> arrays = new ArrayList<>();
public static void main(String[] args) {
//create 50 elements in an array
for (int i = 0; i <= 49; i++) {
arrays.add(i);
}
for (int g = 0; g < arrays.size(); g++) {
if ( arrays.get(g) % 10 != 0) {
System.out.print(arrays.get(g) + " ");
} else {
System.out.println();
System.out.print(arrays.get(g) + " ");
}
}
}
}
If you don't mind using a Guava library, you can also do this
List<Integer> myList = Arrays.asList(myArray);
System.out.println(Lists.partition(myList, 10).stream()
.map(subList -> subList.stream()
.map( Object::toString )
.collect(Collectors.joining(" ")))
.collect(Collectors.joining("\n")));
I can't seem to figure out how to figure this problem out. I want to add a number and remove a space on each line working down from 1.
for (int line = 1; line <= 5; line++) {
for (int space = 5; space >= line + 1; space--) {
System.out.print(" ");
}
System.out.println(line);
}
The trick to your problem is recognizing that 5 characters need to be printed on each line. Each digit is printed the same number of times it represents, with spaces filling in the rest.
for (int i=1; i <= 5; i++) {
// print 5 minus i spaces
for (int j=5; j >= i + 1; j--) {
System.out.print(" ");
}
// repeat the ith digit i times
for (int k=0; k < i; ++k) {
System.out.print(i);
}
System.out.print("\n");
}
Output:
1
22
333
4444
55555
But note that you could even simplify this further, if you want fewer lines of codes, to this:
for (int i=1; i <= 5; ++i) {
String line = new String(new char[5-i]).replace("\0", " ") +
new String(new char[i]).replace("\0", String.valueOf(i));
System.out.println(line);
}
Try this
for (int line = 1; line <= 5; line++) {
for (int space = 5; space >= line + 1; space--) {
System.out.print(" ");
}
for (int k = 0; k < line ; k++) {
System.out.print(line);
}
System.out.println("");
}
import java.util.Scanner;
public class diamond {
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer");
int lines = scan.nextInt();
for(int counter = 1; counter <= lines; counter++)
{
if (counter%2 != 0)
{
for(int count2 = 1; count2 <= counter; count2++){
System.out.print("*");
}
System.out.println();
}
}
}
}
I am supposed to ask the user for a number of lines and output a diamond made of asterisks that number of lines tall. I need some help figuring out how to center the asterisks. I know for strings there is some String.utils method or something, but the output comes in pieces based on a for loop, so I don't think that really works here. If it does, by all means let me know though.
You need to print a certain amount of spaces before each line. Then, you would need another for loop for the opposite.
Try this code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter an integer ");
int lines = scan.nextInt();
for (int counter = 1; counter <= lines; counter++) {
if (counter % 2 != 0) {
for (int i = 0; i < lines - (counter / 2) - 3; i++) {
System.out.print(" ");
}
for (int count2 = 1; count2 <= counter; count2++) {
System.out.print("*");
}
System.out.println();
}
}
for (int counter = lines - 1; counter >= 1; counter--) {
if (counter % 2 != 0) {
for (int i = 0; i < lines - (counter / 2) - 3; i++) {
System.out.print(" ");
}
for (int count2 = 1; count2 <= counter; count2++) {
System.out.print("*");
}
System.out.println();
}
}
}
I think you should prepare a string to print out on each line, then you will know exactly how many characters it has, when the line increases, remove the two '*' in center of the string and add one " " in front of it, then print it out again.
This is not homework. I am a beginner (novice) java programmer, trying to read and complete the exercises at the end of ivor horton's beginning java book.
Write a program to create a rectangular array containing a multiplication table from 1 X 1 to 12 X 12. Output the table as 13 columns with the numeric values right aligned in columns. (The first line of output will be the column headings, the first column with no heading, then the numbers 1-12 for the remaining columns. The first item in each of the succeeding lines is the row heading which ranges from 1-12.
NOTE: I have only learned about Arrays & Strings, Loops & Logic, data types, variables, and calculations. I have not learned about classes and their methods and etc......so no fancy stuff please. THANKS!
public class Chapter4Exercise2 {
public static void main(String[] args)
{
int[][] table = new int[12][12];
for(int i=0; i <= table.length-1; i++)
{
for (int j=0; j <= table[0].length-1; j++)
{
table[i][j] = (i + 1) * (j + 1);
if (table[i][j] < 10)
System.out.print(" " + table[i][j] + " ");
else
if (table[i][j] > 10 && table[i][j] < 100)
System.out.print(" " + table[i][j] + " ");
else
System.out.print(table[i][j] + " ");
}
System.out.println(" ");
}
}
}
As long as the numbers are less than 1000, try this:
As #Mr1158pm said:
public class Chapter4Exercise2 {
public static void main(String[] args) {
int tableSize = 10;
int[][] table = new int[tableSize][tableSize];
for(int i=0; i < table.length; i++) {
for (int j=0; j < table[i].length; j++) {
table[i][j] = (i+1)*(j+1);
if(table[i][j] < 10) //Where i*j < 10
System.out.print(" "+(table[i][j])+" ");
else if(table[i][j] < 100) //Where i*j < 100
System.out.print(" "+(table[i][j])+" ");
else //Where i*j < 1000
System.out.print(" "+(table[i][j])+" ");
}
System.out.println("");
}
I don't think that you have to declare an array data structure to print out this table.
Just use two nested for loops and do calcs in the loops.
Here is a working method that you can work on. Just need to fix column alignment, add space here and there.
FYI row<10?" "+row:row is a form on inline if statement. If you haven't seen it before google it. It's quite useful.
public static void main(String[] args) {
for(int row=0; row<13; row++)
{
for(int col=0; col<13; col++)
{
if(row==0) //ffirst row
{
if(col==0)
System.out.print(" ");
else
System.out.print(col<10?" "+col:" "+col);
}
else
{
if(col==0)
System.out.print(row<10?" "+row:row);
else
System.out.print(row*col<10?" "+(row*col):(row*col<100? " "+(row*col):" "+(row*col)));
}
}
System.out.println();
}
}
import java.util.Scanner;
public class Back {
public static void main(String[] args) {
Scanner a1 =new Scanner(System.in);
int row,col;
String ch;
System.out.println("Enter width of screen:");
row = a1.nextInt();
System.out.println("Enter height of screen:");
col = a1.nextInt();
System.out.println("Enter background character:");
ch =a1.next();
String twoD[][] = new String[row][col];
int i,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++){
twoD[i][j] = ch;
}
for(i=0;i<row;i++){
for(j=0;j<col;j++)
System.out.print(twoD[i][j]+" ");
System.out.println();
}
int width = 10;
int height = 10;
int[][] table = new int[width][height];
for(int i = 0; i < width; i++) {
for(int j = 0; j < height; j++) {
System.out.print(" " + table[i][j] + " ");
}
System.out.println("");
}