Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
PCaceres
Posts: 20
Registered: 02-07-2012
My Carrier: Movistar
Accepted Solution

I need HELP.

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class Calc extends UiApplication {

 public Calc() {
  pushScreen(new Operaciones());
 }

 public static void main(String[] args) {
  Calc app = new Calc();
  app.enterEventDispatcher();
 }
}

final class Operaciones extends MainScreen{
 private ObjectChoiceField choiceField;
    private int select;
    private int a, b, r;
   
    public Operaciones()
    {
         //Invoke the MainScreen constructor.
         super();
         a = b = r = 0;
         //Add a screen title.
         LabelField title = new LabelField("Calculador Básico by PGCG.89",
                             LabelField.ELLIPSIS |
                             LabelField.USE_ALL_WIDTH);
         setTitle(title);

         //Add a text label.
         add(new RichTextField("Ingrese dos números"));

         //Add a drop-down list with the names of three cities as choice items:
         //Los Angeles, Chicago, or New York.
         String choices[] = {"Sumar", "Restar", "Multiplicar", "Dividir"};
         choiceField = new ObjectChoiceField("Tipo Operación", choices);
         add(choiceField);    
        
         select = choiceField.getSelectedIndex();
        
         LabelField lf = new LabelField();
         BasicEditField N1 = new BasicEditField("N1: ", null, 20,
            Field.EDITABLE);
         BasicEditField N2 = new BasicEditField("N2: ", null, 20,
            Field.EDITABLE);
         BasicEditField R = new BasicEditField("R: ", null, 50,
              Field.READONLY);
         add(lf);
         add(new SeparatorField());
         add(N1);
         add(N2);
         add(R);
        
         a = Integer.parseInt(N1.getText());
         b = Integer.parseInt(N2.getText());

         //Populate fields with data for the city the BlackBerry device user
         //selects. The 'select' variable stores the value for the city.

         if (select == 0)
         {
           r = a + b;
              lf.setText("La suma es: ");
              R.setText(Integer.toString(r));
         }
         else if (select == 1)
         {
          r = a - b;
          lf.setText("La resta es: ");
          R.setText(Integer.toString(r));
         }
         else if (select == 2)
         {
          r = a * b;
          lf.setText("La multiplicación es: ");
          R.setText(Integer.toString(r));
         }
         else if (select == 3)
         {
          lf.setText("La división es: ");
          if (b != 0){
           r = a / b;
           R.setText(Integer.toString(r));}
          else
           R.setText("División entre cero");      
         
         }
      
    }
   
    public boolean onClose()
    {
         Dialog.alert("Goodbye!");
         System.exit(0);
         return true;
    }

    //Create a menu item for BlackBerry device users to click to see more
    //information about the city they select.
    private MenuItem _viewItem = new MenuItem("Seleccionar", 110, 10)
    {
         public void run()
         {
              //Store the index of the city the BlackBerry device user selects
           select = choiceField.getSelectedIndex();
         }
    };

    //Create a menu item for BlackBerry device users to click to close the
    //BlackBerry device application.
    private MenuItem _closeItem = new MenuItem("Close", 200000, 10)
    {
         public void run()
         {
              onClose();
         }
    };

    //To add menu items to the menu of the BlackBerry device application,
    //override the makeMenu method.
    protected void makeMenu( Menu menu, int instance )
    {
         menu.add(_viewItem);
         menu.add(_closeItem);
    }   
}

 

 

i cant run this app!.. the error is with the variables integers

Please use plain text.
Developer
RexDoug
Posts: 4,649
Registered: 07-21-2008

Re: I need HELP.

You'll need to do a better job of describing the problem, and on what line of code it is occurring.

 

Please use plain text.
Developer
PCaceres
Posts: 20
Registered: 02-07-2012
My Carrier: Movistar

Re: I need HELP.

Ok!...

 

Error Information:

 

App Error 104

Uncaught:

NumberFormatException

 

well that all information about of problem!...

 

I'm sorry in my country speak spanish, so my ingles is moraless! =S!

 

Thank you!

Please use plain text.
Developer
PCaceres
Posts: 20
Registered: 02-07-2012
My Carrier: Movistar

Re: I need HELP.

Hello again! I debug the application and the error is heres:

 

a = Integer.parseInt(N1.getText()); 

b = Integer.parseInt(N2.getText());

 

and i dont know because is it.

 

the convert of type in java is correct how i made.

 

 

Please use plain text.
Developer
swap_chau
Posts: 121
Registered: 06-15-2011
My Carrier: Vodaphone

Re: I need HELP.

All you have to do is add
N1.setText("12"); after N1 = new BasicEditField(...

You are not setting the text, so it is "" and you are trying to convert blank to integer.
Please use plain text.
Developer
RexDoug
Posts: 4,649
Registered: 07-21-2008

Re: I need HELP.

If you are getting that "number format" exception, then there is someting invalid in the string that you are trying to convert (some non-numeric data).

 

Please use plain text.
Developer
PCaceres
Posts: 20
Registered: 02-07-2012
My Carrier: Movistar

Re: I need HELP.

Well, yes i understand that. But i want recive a number that user put, I need recive two numbers deposit into flieds! So normaly in java i to convert a string to int.

Please use plain text.
Contributor
handyway2011
Posts: 33
Registered: 02-02-2012
My Carrier: Vodafone

Re: I need HELP.

Hice hace tiempo una calculadora similar, prueba haciendo algo como:

 

int uno=Integer.parseInt(operando1.getText().toString());

int dos=Integer.parseInt(operando2.getText().toString());

 

Suerte!

Please use plain text.
Developer
PCaceres
Posts: 20
Registered: 02-07-2012
My Carrier: Movistar

Re: I need HELP.

Me sigue saliendo el error!...

 

No entiendo porque!, normalmente en java usabas el integer para hacer el cambio y listo, pero creo que para BB, las cosas cambian!..

Lo intente pero no funcionó sigo viendo igual!

 

Gracias!

 

 

Please use plain text.
Contributor
handyway2011
Posts: 33
Registered: 02-02-2012
My Carrier: Vodafone

Re: I need HELP.

[ Edited ]

:smileysad: Lo siento!

 

Has probado a Debug-ear y ver qué contiene ese string que intentas convertir?

Puede que como te estaba diciendo alguien en el foro, lo que recibas no sea un únicamente un string numerico.

Yo que tu haría algo como esto:

 

String op1=operando1.getText().toString();

String op2=operando2.getText().toString();

 

Debug-ea y mira que es lo que recibes... supongo que despues de ver eso solucionaras el problem!

Mucha suerte!

Please use plain text.