Help, with this search tree code "bad operand types for binary operator '-' first type: String; second type: int "[closed]

1
import java.io.*;
public class Test
{
    public static void main(String[] args)throws IOException
    {
        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
        PrintStream p=System.out;

        CArbolBinarioDeBusqueda arbolbb=new CArbolBinarioDeBusqueda();


        String matricula;
        String marca;
        String modelo;
        String propietario;
        int i=0,cod,op,res;

        p.println("Introducir datos. Finalizar con Ctrl+Z");

        p.println("Matricula: ");
        while((matricula=in.readLine())!=null)
        {
            p.println("Marca: ");
            marca=String.valueOf(in.readLine());
            cod=arbolbb.insertar(new CDatos(matricula, marca, modelo, propietario));

            if(cod==CArbolBinarioDeBusqueda.YA_EXISTE)
            {
                if((marca>=0)) //en esta parte me sale mal, bad operand types for binary operator '-' first type: String;      second type: int
                {
                    CDatos datos=(CDatos)   arbolbb.buscar(new CDatos(matricula,marca,modelo,propietario));
                    datos.asignarMarca(marca);
                }
                else
                {
                    arbolbb.borrar(new CDatos(matricula,marca,modelo,propietario))  ;
                    p.println("Nodo borrado");
                }

            }
            p.println("Matricula: ");
        }
        p.println("\n");

        do
        {
            p.println("\nArbol: ");
            p.println("MENU DE RECORRIDOS");
            p.println("\n1. Recorrido Inorden ");
            p.println("\n2. Recorrido Preorden ");
            p.println("\n3. Recorrido Postorden ");

            p.println("\n4. Salir ");
            p.println("\nSeleccione un tipo de recorrido: ");
            op=Integer.parseInt(in.readLine());

            switch(op)
            {
                case 1:
                    p.println("\nRecorrido Inorden ");
                    arbolbb.visitarInorden();
                    break;
                case 2:
                    p.println("\nRecorrido Preorden ");
                    arbolbb.visitarPreorden();
                    break;
                case 3:
                    p.println("\nRecorrido Postorden ");
                    arbolbb.visitarPostorden();
                    break;
                case 4:
                    System.exit(0);
                    break;
                default: p.println("Opción invalida...");
                    break;

            }   
            p.println("\nDesea continuar (1/2): ");
            res=Integer.parseInt(in.readLine());

        }while(res!=2);
    }
}

    
asked by Enrique G. 02.11.2016 в 20:25
source

2 answers

1

In Java it does not allow to compare an int with a String, mainly for that reason you get the error, what you could do is convert that String to Int first with

Integer.parseInt(marca).

Or also if you just want to know if you can bring data to the String:

if((marca>=0)) //sustituye esto por
if(!marca.isEmpty())

Your question is not clear what you need to execute on that line, which is what it does to help you more

    
answered by 02.11.2016 / 20:36
source
0

You have to convert from String to Int by Integer.parseInt() so you can perform the operation since you are making a comparison between two different types.

Change from

if((marca>=0))
{

to:

if(Integer.parseInt(marca)>=0)
{

There is another important situation to consider, in which the value of the variable marca may not have a numeric value. for which I advise you to add a method to perform this validation.

public static boolean esNumerico(String str)  
{  
  try  
  {  
    double d = Double.parseDouble(str);  
  }  
  catch(NumberFormatException nfe)  
  {  
    return false;  
  }  
  return true;  
}

We would use the method in this way:

if((esNumerico(marca)?Integer.parseInt(marca):0)>=0)//Donde :0 define un valor default 0 en caso de la variable no contener un valor numérico.
    
answered by 02.11.2016 в 20:38