create matrix using scanner enter row by row in java - java

I want to create a matrix like this way:
Enter the number of rows and columns of the array: 4 5
Enter the array:
3 4 5.5 4 5 (enter)
1 3.5 1.5 4 5 (enter)
6.5 1.5 2.5 5 1 (enter)
4 3 1 4 4.4 (enter)
then local the smallest value's location in this matrix, but I already stuck in the first step
java.util.Scanner input = new java.util.Scanner(System.in);
int row = 0, col = 0;
double numList[][] = new double[row][col];
System.out.print("Rnter the number if rows and columns of the array: ");
row = input.nextInt();
col = input.nextInt();
System.out.println("Enter the array:");
what I have already tried is here,
Is there any method? THANK YOU GUYS

You can use Scanner.nextDouble() to read the next double value.
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of rows and columns of the array: ");
int row = input.nextInt();
int col = input.nextInt();
double numList[][] = new double[row][col];
System.out.println("Enter the array:");
for(int i=0; i<row; i++){
for(int j=0; j<col; j++){
numList[i][j] = input.nextDouble();
}
}

Related

How to record each input separately in a loop separately? (Java)

My problem is that only the 5th input gets printed while the rest is not
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
}
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
System.out.println(+n);
Let's say I typed 1, 2, 3, 4, 5 respectively,
it should look like this
1
2
3
4
5
But instead I get
5
5
5
5
5
You could store your input parameters into ArrayList
public static void main(String[] args) {
Scanner ns = new Scanner(System.in);
int n = 0;
int i = 1;
List<Integer> params = new ArrayList<>();
while (i <= 5) {
System.out.println("enter a number");
n = ns.nextInt();
params.add(n);
i++;
}
for (Integer param : params) {
System.out.println(param);
}
}
Output:
1
enter a number
2
enter a number
3
enter a number
4
enter a number
5
1
2
3
4
5
All the inputs are being printed, the problem is that the while loop sets n to the value of 5 and then you print 5 five times. The correct code you are looking for is:
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
i++;
System.out.println(+n);
}
Please print number in loop. See code below :
Scanner ns = new Scanner(System.in);
int n = 0;
int i=1;
while(i<=5)
{
System.out.println("enter a number");
n = ns.nextInt();
System.out.println(n);
i++;
}

Adding 2 Matrices in java

So I was tasked to write a program that can create two matrices and then add them both together. I must ask the length and height of matrix 1, and then ask the length and height of matrix 2. Which I did here:
Scanner keyboard = new Scanner(System.in);
int length1, height1, length2, height2;
System.out.println("Welcome to the matrix adder program");
System.out.println(" ");
System.out.println("Please enter the length of the first matrix");
length1 = keyboard.nextInt();
System.out.println("Please enter the height of the first matrix");
height1 = keyboard.nextInt();
System.out.println("Please enter the length of the second matrix");
length2 = keyboard.nextInt();
System.out.println("Please enter the height of the second matrix");
height2 = keyboard.nextInt();
int[][] a = new int[length1][height2];
int[][] b = new int[length2][height2];
Then I need to enter a value for matrix 1 row 1, column 1, row 1, column 2, and so on until it meets the dimensions of the array. So I put
for (int i = 1; i < length1 + 1; ++i)
{
for (int j = 1; j < height1 + 1; ++j)
{
System.out.println("Please enter a value for matrix 1 space "+ i + "," + j);
a[i][j] = keyboard.nextInt();
}
}
And this is where I get the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at MaxtrixAddition.main(MaxtrixAddition.java:32)
It would need to output something like this:
Please enter a value for matrix 1 space 1, 1
4
Please enter a value for matrix 1 space 1, 2
1
Please enter a value for matrix 1 space 2, 1
8
Please enter a value for matrix 1 space 2, 2
7
Please enter a value for matrix 2 space 1, 1
1
Please enter a value for matrix 2 space 1, 2
3
Please enter a value for matrix 2 space 2, 1
2
Please enter a value for matrix 2 space 2, 2
4
What would I need to do to get rid of that error?
I hope that I understood what you want to do.
Arrays start at position 0.
So your code snip should be:
for (int i = 0; i < length1; ++i)
{
for (int j = 0; j < height1 ; ++j)
{
System.out.println("Please enter a value for matrix 1 space "+ i + "," + j);
a[i][j] = keyboard.nextInt();
}
}

Taking input from user in a combined way

im working on a question where the input is given in the below format:
5
7
121
123
7
121
###
4
3
3
2
5
Explanation of the input:
The first number is N, here in example N=5 (N>1 and N<100000). the next are N lines with different numbers ranging from 1 to 100. then comes ###. then the next number is K, here in example K=4. the next are K lines with different values ranging from 1 to 100. the '###' is used to separate N and K inputs.
my question here is how do i take the input from the user using the '###'. and later how can i differentiate between N and K. Kindly help.
please find my code below. but im not able to figure the format to write this type of input. kindly help.
int n=0,N=0,k=0,y=0,K=0;
System.out.println("Enter 'N': ");
n=in.nextInt();
if(n>=1 && n<=100000) {
N=n;
}
int[] a1 = new int[N];
int[] x = new int[N];
System.out.println("Enter values of N: ");
for (int i=0;i<a1.length;i++) {
x[i] = in.nextInt();
}
for (int i=0;i<a1.length;i++) {
if(x[i]>=1 && x[i]<=5000) {
a1[i] = x[i];
}
}
System.out.println("Enter 'K': ");
k=in.nextInt();
if(k>=1 && k<=100) {
K=k;
}
int[] a2 = new int[K];
System.out.println("Enter values of K: ");
for (int i=0;i<a2.length;i++) {
a2[i] = in.nextInt();
}
You should be able to simply use in.skip("###") before reading k to skip the line with the separator.

Trying to get my matrix printed not the heap address

The code seems to run except what I am getting is not a matrix of a specified (by the user) size but what I think is a heap address
Here's what it returns when the user inputs 2 for the size and then 4 numbers:
Enter matrix size:
2
Enter a 2 by 2 matrix row by row: 2 3 4 5
The row-sort matrix is...[[D#3c954549BUILD SUCCESSFUL (total time: 8 seconds)
here is the code....thank you in advance.
import java.util.Scanner;
public class Exercise7_26M {
public static void main (String[]args)
{
//Prompt user for input of matrix size
System.out.println("Enter matrix size: ");
Scanner input = new Scanner(System.in);
int size = input.nextInt();
double[][] m = new double [size][size];
//prompt user for input of array
System.out.print("Enter a " + size + " by " + size + " matrix row by row: ");
for (int row = 0; row < 2; row++)
for (int column = 0; column < 2; column++)
m[row][column] = input.nextDouble();
System.out.print("The row-sort matrix is..." + m);
}
Java arrays do not override toString() so you are getting the default implementation from Object. Instead, you can use Arrays.deepToString(Object[]) like
System.out.println("The row-sort matrix is..." + Arrays.deepToString(m));

Java Multiplication

How will do a program that displays a multiplication table based on the size that the user inputs? And will add each row and each column? Something like this:
Enter a number: 4
1 2 3 4 10
2 4 6 8 20
3 6 9 12 30
4 8 12 16 40
10 20 30 40
I tried this:
Scanner s = new Scanner(System.in);
System.out.print("Enter a number: ");
int x = s.nextInt();
for(int i = 1; i <= x; i++)
{
for (int j = 1; j <=x; j++)
{
System.out.print((i*j) + "\t");
}
System.out.println();
}
Sample Output:
Enter a number: 4
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
How I will do to add each row and each column?
Since this seems like homework, I wouldn't feel comfortable writing your code for you. However, keep the following things in mind.
Your matrix will always be a square, as the user only enters a single number, of n x n numbers.
Since these numbers increment by one along the row and column, the sum of each row and column pair will be the same. In other words, the total of row[n] will equal the total of column[n].
Using that, you can create a single array of size n to store the sum of each row. For example:
Enter a number: 3
1 2 3 x
2 4 6 y
3 6 9 z
x y z
When you're looping through each row, you can store the row total in the array.
Row 0: Add 1 + 2 + 3 and store in array[0]
Row 1: Add 2 + 4 + 6 and store in array[1]
Row 2: Add 3 + 6 + 9 and store in array[2]
At the end of each row you can simply display the total in array[row]. When you finish drawing all rows, you'd simply loop through array and display each total value.
Hope this points you in the right direction!
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Enter size of table: ");
int x = s.nextInt();
int r = 0;
int l = 0;
int f = 0;
for(int i=1;i<=x;i++){
for (int j=1; j <=x; j++)
{
r = r + j;
System.out.print(i*j+"\t");
}
System.out.print(r);
System.out.println();
System.out.println();
l=l+i;
}
for(int k = 1; k<=x;k++)
{
f=f+l;
System.out.print(f + "\t");
}

Categories

Resources