02-07-2012 04:55 PM
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
Solved! Go to Solution.
02-07-2012 05:58 PM
You'll need to do a better job of describing the problem, and on what line of code it is occurring.
02-07-2012 06:08 PM
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!
02-07-2012 06:25 PM
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.
02-08-2012 05:12 AM
02-08-2012 08:23 AM
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).
02-08-2012 10:52 AM
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.
02-08-2012 11:06 AM
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!
02-08-2012 11:12 AM
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!
02-08-2012 11:23 AM - last edited on 02-08-2012 11:23 AM
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!