J2ME string and base type conversions

Source: Internet
Author: User
Tags integer numeric numeric value uppercase letter

When MIDlet is in operation, you can obtain the property values of the attributes in the manifest file or description file through the Midlet.getappproperty () function, which is treated as a string. If we need to do some math, we have to convert the string to a numeric value before we can do it.

To convert a string to a numeric value, you must rely on the Parsexxx () method in the byte, Short, Integer, Long four categories defined in Java.lang, which are all integer types, only the difference in scope, if we intercept the string, When converted to a value that exceeds the range that the form can afford, an exception is created.

Because floating-point numbers are not supported in CLDC 1.0, there is no category for floating-point numbers, only integers. But after CLDC 1.1 to support floating-point numbers, so after CLDC 1.1

New float, Double two categories will begin.

Let's say we have two custom attributes, MyAttr1 and MYATTR2, where the MYATTR1 value is 3, and the MYATTR2 value is 6.

We're going to take two values out, multiply and then output on the screen, and the sample program is as follows:

TransformTest.java
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class TransformTest extends MIDlet
{
public TransformTest()
{
}
public void startApp()
{
String attr1 = getAppProperty("MyAttr1");
String attr2 = getAppProperty("MyAttr2");
int attr1val1 = Integer.parseInt(attr1) ;
int attr1val2 = Integer.parseInt(attr2) ;
System.out.println(attr1val1*attr1val2) ;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}

Execution results:

18

After taking out the string, we can also use Java.lang.Character to determine whether a character is a number, an uppercase letter, and a lowercase letter. You can also use this category to convert the case of English letters.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.