Error java.lang.NumberFormatException

1

I have a problem with this program, it works so that when entering a text like "234 + 312" convert the strings into numbers and do the corresponding operation, but I get an error

package Pruebas;
import java.util.Scanner;
public class prueba {

 public static void main(String ...S) {
    Scanner sc = new Scanner (System.in);
    String ope;
    System.out.print ("Ingrese la operacion:");
    ope = sc.nextLine();
    String strNum1 = "", strNum2 = "";
    boolean centinela = false;
    char car = '
package Pruebas;
import java.util.Scanner;
public class prueba {

 public static void main(String ...S) {
    Scanner sc = new Scanner (System.in);
    String ope;
    System.out.print ("Ingrese la operacion:");
    ope = sc.nextLine();
    String strNum1 = "", strNum2 = "";
    boolean centinela = false;
    char car = '%pre%', operador = '%pre%';
    double num1 = 0, num2 = 0, res = 0;
    int i = 0;
    while (i > ope.length()) {
        car = ope.charAt(i);
        if (car == '+' || car == '-' || car == '*' || car == '/') {
            operador = car;
            centinela = true;
        } else {
            if (centinela == false) {
                strNum1 = strNum1 + car;
            } else {
                strNum2 = strNum2 + car;
            }
        }
        i++;
    }
    num1 = Double.parseDouble(strNum1);
    num2 = Double.parseDouble(strNum2);
    switch (operador) {
    case'+':
        res = num1 +num2;
        break;
    case'-':
        res = num1 - num2;
        break;
    case'*':
        res = num1 * num2;
        break;
    case'/':
        res = num1 / num2;
        break;  
    }
    System.out.print (res);
}



}
', operador = '%pre%'; double num1 = 0, num2 = 0, res = 0; int i = 0; while (i > ope.length()) { car = ope.charAt(i); if (car == '+' || car == '-' || car == '*' || car == '/') { operador = car; centinela = true; } else { if (centinela == false) { strNum1 = strNum1 + car; } else { strNum2 = strNum2 + car; } } i++; } num1 = Double.parseDouble(strNum1); num2 = Double.parseDouble(strNum2); switch (operador) { case'+': res = num1 +num2; break; case'-': res = num1 - num2; break; case'*': res = num1 * num2; break; case'/': res = num1 / num2; break; } System.out.print (res); } }

    
asked by Star Hikari 04.11.2018 в 06:33
source

1 answer

0

The problem is in the following line:

num1 = Double.parseDouble(strNum1);
num2 = Double.parseDouble(strNum2);

Since the error says that the parameter it receives is an empty String, that is, it has no value, so it can not be cast to Double type. This happens because of the code you have here:

String strNum1 = "", strNum2 = "";

And then here

if (centinela == false) {
    strNum1 = strNum1 + car;
} else {
    strNum2 = strNum2 + car;
}

For some reason in the iteration of the while, it never enters the strings of strNum1.

And that's because the condition is poorly written, it should be like this:

while (i < ope.length()) { //Simplemente cambié el ">" que tenias por "<"

This should solve the problem:)

    
answered by 04.11.2018 / 07:04
source