-1

The problem is when you entry an input with scanner ,it shows on console. I want them to shown in an order. I want them shown like a matris. But with nextInt method all shows bottom of each other.

I want a console output like this:

this

But with nextInt() method your new int shows on nextLine like this:

this

How can i show multiple variables in same line with scanner?

import java.util.Scanner;

public class ProbilityMatrixTest {

static int M;
static int N;
static float[][] matrixX;
static float[][] matrixY;
static boolean isProbilityMatrix;

public static void main(String[] args) {
    initiate();
    testMatrix(matrixX);
    System.out.println();
    multiplyMatrix();
    testMatrix(matrixY);
}

public static void initiate() {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter the row and column size of matrix : ");
    M = sc.nextInt();
    N = sc.nextInt();
    System.out.println();


    matrixX = new float[M][N];
    System.out.println("Enter values of " + M + "x" + N + " matrix :");
    for (int j = 0; j < N; j++) {
        for (int i = 0; i < M; i++) {
            matrixX[i][j] = sc.nextFloat();
        }
    }
}

public static void testMatrix(float[][] givenMatrix) {
    isProbilityMatrix = true;
    if (M != N) {
        isProbilityMatrix = false;
    }
    for (int j = 0; j < N; j++) {
        float rowVariablesTotal = 0;
        for (int i = 0; i < M; i++) {
            rowVariablesTotal += givenMatrix[i][j];
            if (givenMatrix[i][j] < 0) {
                isProbilityMatrix = false;
            }
        }
        if (rowVariablesTotal != 1.0f) {
            isProbilityMatrix = false;
        }
    }
    System.out.print("TEST RESULT : ");
    if (isProbilityMatrix) {
        System.out.println("Probility matrix");
    } else {
        System.out.println("not Probility matrix");
    }
}

public static void multiplyMatrix() {

        matrixY = new float[M][N];
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                float newMatrixVariable = 0;
                for (int a = 0; a < M; a++) {
                    newMatrixVariable += (matrixX[i][a] * matrixX[a][j]);
                }
                matrixY[i][j] = newMatrixVariable;
            }
        }
        System.out.println("The square of given matrix:");
        for (int j = 0; j < M; j++) {
            for (int i = 0; i < N; i++) {
                System.out.print(matrixY[i][j] + "  ");
            }
            System.out.println();
        }


}

}

Thats

  • 1
    Please show your code attempt and tell the details of your problems with this code. You understand that Scanner objects have nothing to do with display of results, that instead you will want to focus on the `System.out` methods of `PrintStream` including `print`, `println` and `printf` (the most versatile of the bunch) – Hovercraft Full Of Eels Jul 09 '18 at 01:01
  • I dont need to use system.out.print when showing variables on console. Scanner variable already appears on console. What i want is show them in order. But with every nextInt() method i go one line bottom. I want to show matris like: http://prntscr.com/k43vvb But instead it shows all bottom of each otter. The problem in not on print methods its on scanner showing inputs nextline always(bad english sry) – Furkan Atılgan Jul 09 '18 at 01:48
  • You need to actually type the values on a single line – OneCricketeer Jul 09 '18 at 02:14
  • Yes exactly. I want to decide the parts where the scanner inputs gonna write. So the inputs can look like a matris. – Furkan Atılgan Jul 09 '18 at 02:18

1 Answers1

1

You need to scan entire lines at a time. Otherwise, you're always pressing the enter key, causing it to look like you're entering one value before the other on previous lines

For example, type 3 3, then enter, then you can type three space separated decimal values, enter, then repeat that twice

System.out.print("Enter the row and column size of matrix : ");
String[] mn = sc.nextLine().split("\\s+");
int M = Integer.parseInt(mn[0]);
int N = Integer.parseInt(mn[1]);
System.out.println();

double[][] matrixX = new double[N][];
for (int i = 0; i < N; i++) {
    matrixX[i] = new double[M];
    String[] row = sc.nextLine().split("\\s+");
    for (int j = 0: j < M: j++) {
        matrix[i][j] = Double.parseDouble(row[j]);
        //... 
    } 
} 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • [codePicture](http://prntscr.com/k444od) i m getting ArrayIndexOutOfBoundsException. Btw you are saying i can show scanner inputs like what i want with this code right? – Furkan Atılgan Jul 09 '18 at 02:28
  • Enter the row **and** column size... Not sure why you typed only one number. Please try to understand what the code does before copying – OneCricketeer Jul 09 '18 at 02:30
  • Now i understand it sory. You are saying i need to write with space. I can not entry every code. Yea that works. But i think there is no way doing it with entrying every input. – Furkan Atılgan Jul 09 '18 at 02:35
  • Scanner will only accept input once you press the enter key, so there is no other way to format your values – OneCricketeer Jul 09 '18 at 02:38
  • So scanner has to take other input from next line it can not take it from the same line. I had to do 1 input with 2 variables in it than split it. Btw how i can found thoose split codes like "s//+" i only found that "s//" means next word i think. – Furkan Atılgan Jul 09 '18 at 02:41
  • It's called a regular expression pattern. `\\s` is a space. A plus afterwards means that one or more spaces – OneCricketeer Jul 09 '18 at 02:46
  • It works but i face with [this](http://prntscr.com/k44e49) problem now. It fixes when i turn floats to double. But why its not working with floats? Why it dont calculate corretctly with floats. – Furkan Atılgan Jul 09 '18 at 03:13
  • I suggest you accept this answer, then and post a new question after you read https://stackoverflow.com/questions/588004/is-floating-point-math-broken – OneCricketeer Jul 09 '18 at 03:15
  • I didnt know you can accept answers sory. I asked here because i thought its kinda connected. Ty for your helping. – Furkan Atılgan Jul 09 '18 at 03:18
  • The final math calculation is not completely related to how the numbers get entered, though. Doubles have twice the "precision" as floats – OneCricketeer Jul 09 '18 at 03:23
  • Okay i am gonna read some stackOverFlow guides. And try to learn my mistakes on question asking. Ty for your caring. – Furkan Atılgan Jul 09 '18 at 03:23
  • I wanted to point out how numbers entered so if anyone faces the same problem they dont stuck in same place. And i didnt know you can accept answers so i edited to specify that my question resolved. – Furkan Atılgan Jul 09 '18 at 03:27
  • About precision. 0.1 x 0.1 is always 0.01 why it calculated 0.01000001 with floats. I didnt understand that. – Furkan Atılgan Jul 09 '18 at 03:34