Mam problem z jednym MIDletem, a dokładnie z funkcją bibloteczną klasy Integer. Źródło kompiluje się bez problemu, dopiero po uruchomieniu MIDletu na java Wierless Toolkit, i przesłaniu cyfry wywala taki błąd:
java.lang.NumberFormatException: 123
at java.lang.Integer.parseInt(+214)
at java.lang.Integer.parseInt(+6)
at Server.sumuj(+4)
at Server.run(+174)
Co to może być? Chodzi dokładnie o zamiane stringu na liczbę typu int i odwrotnie:
public String sumuj(String c){
int cyfra=Integer.parseInt(c);
suma=suma+cyfra;
String pom=Integer.toString(suma);
return pom;
}
Czy ktoś ma jakieś pomysły? Proszę o rady.
zacząć od nauki javy zamiast robienia jakiś midletów?
Bo co ma oznaczać ten "+" przed liczbą - możesz mi wytłumaczyć?
Uruchom sobie na poczatek ponizszy program ...
public class Main
{
public Main()
{
}
public static void main(String[] args)
{
Oblicz oblicz = new Oblicz(0);
oblicz.sumuj("50");
// oblicz.sumuj("+40"); <-to spowoduje wyjatekNumberFormatblablabla
System.out.println(oblicz);
}
}
class Oblicz
{
public Oblicz(int liczbaSt)
{
this.liczbaSt = liczbaSt;
}
public String sumuj(String c)
{
int cyfra=Integer.parseInt(c);
suma = suma + cyfra;
String pom = Integer.toString(suma);
return pom;
}
public String toString()
{
return "suma:" + this.suma;
}
private int liczbaSt;
private int suma = 0;
}
chociaż jak wywołujesz, to będzie wiecej wiadomo
Funkcja wygąda ok, błąd też ;) ale przyczyną wystąpienia tego błędu jest prawdopodobnie wywołanie
Poza tym dziwi mnie wywołanie parseInt (widoczne w opisie błędu)
Standardowo wywołanie wygląda tak: parseInt(String), a jest u ciebie parseInt(+90). Tj. 1 - nie ma "" po 2. znak + na początku
wejściowego do zmiennej klasy StringBuffer a następnie za pomocą metody toString() konwertuje na łąńcuch. Ze strumienia pobierane są bajty i przed umieszczeniem ich w zmienej typu StringBuffer następuje rzutowanie na typ char
public class Server implements Runnable, CommandListener {
private SocketMIDlet parent;
private Display display;
private Form f;
private StringItem si;
private int suma;
private boolean stop;
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private final Alert alert = new Alert("Error", "", null, AlertType.ERROR);
InputStream is;
OutputStream os;
SocketConnection sc;
ServerSocketConnection scn;
Sender sender;
public class Oblicz{
private int num;
private int sum;
public Oblicz(int licz){
this.num=licz;
this.sum=0;
}
public void sumuj(String c){
int cyfra=Integer.parseInt(c);
sum = sum + cyfra;
}
public String toString(){
return "Suma: "+this.sum;
}
}
public Server(SocketMIDlet m) {
parent = m;
suma=0;
display = Display.getDisplay(parent);
f = new Form("Socket Server");
si = new StringItem("Status:", " ");
f.append(si);
f.addCommand(exitCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
public void start() {
Thread t = new Thread(this);
t.start();
}
if (s.length() == 0) {
alert.setString("No Argument");
display.setCurrent(alert);
throw new NumberFormatException();
}
try {
ob.sumuj(s);
} catch (NumberFormatException e) {
alert.setString("argument is out of range.");
display.setCurrent(alert);
throw e;
}
return ob.toString();
}
public void run() {
try {
si.setText("Czekam na polaczenie...");
scn = (ServerSocketConnection) Connector.open("socket://:5000");
// Wait for a connection.
sc = (SocketConnection) scn.acceptAndOpen();
si.setText("Connection accepted");
is = sc.openInputStream();
os = sc.openOutputStream();
sender = new Sender(os);
// Allow sending of messages only after Sender is created
while (true) {
StringBuffer sb = new StringBuffer();
int c = 0;
jeszcze jedno - trochę nie rozumiem tego co poniżej:
while (((c = is.read()) != '\n') && (c != -1)) {
sb.append((char) c);
}
Nie lepiej zastować jakiś obiekt opakowujący strumień wejściowy (o ile się mylę w interpretacji kodu ... is = sc.openInputStream(); ...)
Czyli zastąpić tym co poniżej bez bawienia się metoda read ?
BufferedReader we = new BufferedReader(InputStreamReader(is));
String linia;
while((linia = we.readLine()) ! = null)
{
sb.append(linia);
}
public class Client implements Runnable, CommandListener {
private SocketMIDlet parent;
private Display display;
private Form f;
private StringItem si;
private TextField tf;
private boolean stop;
private Command sendCommand = new Command("Send", Command.ITEM, 1);
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
InputStream is;
OutputStream os;
SocketConnection sc;
Sender sender;
public Client(SocketMIDlet m) {
parent = m;
display = Display.getDisplay(parent);
f = new Form("Socket Client");
si = new StringItem("Status:", " ");
tf = new TextField("Liczba:", "", 30, TextField.DECIMAL);
f.append(si);
f.append(tf);
f.addCommand(exitCommand);
f.addCommand(sendCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
/**
* Start the client thread
*/
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
sc = (SocketConnection) Connector.open("socket://localhost:5000");
si.setText("Connected to server");
is = sc.openInputStream();
os = sc.openOutputStream();
// Start the thread for sending messages - see Sender's main
// comment for explanation
sender = new Sender(os);
// Loop forever, receiving data
while (true) {
StringBuffer sb = new StringBuffer();
int c = 0;
public static int parseInt(String s,
int radix)
throws NumberFormatException
Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. The resulting integer value is returned.
An exception of type NumberFormatException is thrown if any of the following situations occurs:
* The first argument is null or is a string of length zero.
* The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
* Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
* The value represented by the string is not a value of type int.
Parameters:
s - the String containing the integer representation to be parsed
radix - the radix to be used while parsing s.
Returns:
the integer represented by the string argument in the specified radix.
Throws:
NumberFormatException - if the String does not contain a parsable int.
podałeś ;), ale nie mam możliwości aby gdzieś to wrzucić i sprawdzić co jest nie tak (m.in. dlatego że nie mam odpowiednich pakietów). Może coś później wymyśle, jak narazie sam musisz prześledzić co się dzieje po drodze (jakieś wstawki które ci wyświetlą co dzieje przez tym błędem, najlepiej jakimś debugerem)