kunaguvarun
kunaguvarun

Reputation: 711

Scanner cannot read input

package algo.chapter1.Ex1_1_33;

import java.util.Scanner;
import edu.princeton.cs.introcs.StdIn;
import edu.princeton.cs.introcs.StdOut;


public class MatrixClient
{
    static Scanner scan = new Scanner(System.in);
    public static void main(String[] args)
    {
       while(true)
       {
        switch (getInput())
        {
            case 1:
                //Dot Product
                performDotProduct();
                break;

            case 2:
                performMatrixProduct();
                break;

            case 3:
                break;

            case 4:
                break;

            case 5:
                break;

            case 6:
                StdOut.println("Thank You!");
                System.exit(0);
                break;

            default:
                StdOut.println("Invalid Choice!!!");
        }
    }
}

public static int getInput()
{
    StdOut.println("******************************");
    StdOut.println("1.Dot Product");
    StdOut.println("2.Matrix-Matrix product");
    StdOut.println("3.Transpose");
    StdOut.println("4.Matrix-Vector Product");
    StdOut.println("5.Vector-Matrix product");
    StdOut.println("6.Exit");
    StdOut.println("******************************");
    StdOut.print("\nChoose one from the above: ");

    return StdIn.readInt();
}

public static void performDotProduct()
{
    StdOut.print("Enter elements of vector A: ");
    double[] A = readVector();

    StdOut.print("Enter elements of vector B: ");
    double[] B = readVector();

    StdOut.println("The dot product is: " + Matrix.dot(A, B));
}

public static void performMatrixProduct()
{
    StdOut.print("Enter the size of matrix A: ");
    int size = scan.nextInt();

    double[][] A = readMatrix(size);
    for (double[] row : A)
    {
        for (double item : row)
            StdOut.print(item + "\t");
        StdOut.println();
    }
}
public static double[] readVector()
{

    String[] input = scan.nextLine().split(" ");
    double[] x = new double[input.length];

    for (int i = 0; i < input.length; i++)
        x[i] = Double.parseDouble(input[i]);

    return x;
}

public static double[][] readMatrix(int N)
{
    int counter = 0;
    double[][] matrix = new double[N][N];
    while (counter < N)
    {
        StdOut.print("Enter elements of row " + (counter + 1) + ": ");
        matrix[counter] = readVector();
        counter++;
    }

    return matrix;
}
}

I'm using readVector() method to read a row of doubles and return a double array. When it is used for dot product, input is being read returned fine, but when I use matrix-matrix multiplication option, readVector() doesn't wait for input. It throws NumberFormatExcpetion: empty string.

I don't understand why readVector() behaves differently.

Upvotes: 0

Views: 229

Answers (2)

Abdullah AlHazmy
Abdullah AlHazmy

Reputation: 1299

In your code change:

 int size = scan.nextInt();

To

 int size = scan.nextInt();
scan.nextLine();

Upvotes: 0

fajarkoe
fajarkoe

Reputation: 1563

The problem is in this line:

int size = scan.nextInt();

When your code asks to enter the size of the matrix, you enter a number, and then hit enter. The above code, however, only reads the number that you enter. It does not consume the enter (\n character).

After your code consume the matrix size, your code does:

String[] input = scan.nextLine().split(" ");

which consumes the enter (\n character), and then split it by space. This (scan.nextLine()), of course, returns an empty string.

To fix it, instead of using:

int size = scan.nextInt();

use:

int size = scan.nextInt();
scan.nextLine();

Upvotes: 2

Related Questions