0

I'm getting this error:

"Exception in thread "main" java.lang.NumberFormatException: empty String".

I don't know what I doing wrong. I've put all the right types of variables. What can I do to correct it?

package com.mycompany.quadradomagico.exercicio2;
import java.util.Scanner;

public class QuadradoMagico {
    
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in);
        //Variável de linha e coluna
        int l, c;
        
        //Recebendo os valores
        System.out.printf("Informe o número de linhas:\n");
        l = read.nextInt();
        
        System.out.printf("Informe o número de colunas:\n");
        c = read.nextInt();

        //Verificando se é um quadrado
        if (l != c) {
            System.out.printf("Não é um quadrado.");
        }
        else {
            //Matriz do quadrado
            double q[][] = new double[l][c];
            
            //Recebendo os valores da matriz
            for(int i=0; i<l; i++) {
                for (int j=0; j<c; j++) {
                    System.out.println("Insira um valor para a posição ("+(i+1)+","+(j+1)+"):");
                    q[i][j] = Double.parseDouble(read.nextLine());
                }
            }
            
            //Exibindo os valores da matriz
            for(int i=0; i<l; i++){
                for(int j=0; j<c; j++){
                    System.out.print(q[i][j]+ "  ");
                }
                System.out.println();
            }
        }
    }
}
6
  • in which line did you get this exception ? Commented Apr 3, 2021 at 0:59
  • in the 30...... Commented Apr 3, 2021 at 1:03
  • at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.base/java.lang.Double.parseDouble(Double.java:549) at com.mycompany.quadradomagico.exercicio2.QuadradoMagico.main(QuadradoMagico.java:30) Commented Apr 3, 2021 at 1:03
  • take a look at my answer Commented Apr 3, 2021 at 1:06
  • The error is that you passed an empty string to Double.parseDouble(). Commented Apr 3, 2021 at 5:55

2 Answers 2

2

You can directly use Scanner#nextDouble instead.

q[i][j] = read.nextDouble();
Sign up to request clarification or add additional context in comments.

Comments

1

Just change this q[i][j] = Double.parseDouble(read.nextLine()); line by this one q[i][j] = Double.parseDouble(read.next());

Result :

25.0  55.0  45.0  6.0  5.0  
5.0  55.0  5.0  4.0  7.0  
7.0  7.0  45.0  45.0  5.0  
65.0  45.0  65.0  56.0  645.0  
456.0  45.0  45.0  564.0  546.0  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.