String into int & vice versa
Hi all
i cant convert the String into int although using
Integer.parseInt & vice versa
any ideas
here is the code
// fontCB & styleCB & sizeCB are ComboBoxes
String fontName,fontStyle,fontSize;
int fontStyleInt=1,fontSizeInt=1;
fontName = (String)fontCB.getSelectedItem();
fontStyle= (String)styleCB.getSelectedItem();
fontSize= (String)sizeCB.getSelectedItem();
try
{
fontStyleInt = Integer.parseInt(fontStyle);
fontSizeInt = Integer.parseInt(fontSize);
}
catch (NumberFormatException nfe)
{
System.out.println("I'm an error");
}
Font fontTemp = new Font(fontName,fontStyleInt,fontSizeInt);
myTextArea.setFont(fontTemp);
[769 byte] By [
TheInsider] at [2007-9-30 20:53:45]

The getSelectedItem() method returns a type called "Object". Therefore, to convert it to a string, u have to use String.valueOf(fontCB.getSelectedItem);
Font styles are integers, i.e. PLAIN=0, BOLD=1, ITALIC=2, BOLD & ITALIC =3
So, u can do the following,
String styles={"Plain","Bold","Italics","Bold and Italics"};
styleCB=new JComboBox(styles);
To get the appropriate style, all u have to do is:
int style=styleCB.getSelectedIndex();
As for the font size,
int size=Integer.parseInt(String.valueOf(sizeCB.getSelectedItem()));
I think u r having plans for a text editor. right?
Aswin at 2007-7-7 2:26:35 >

There is a static method in the Integer class which returns a String object representing a specified integer. Take a look at the [url=http://java.sun.com/j2se/1.4.2/docs/api/index.html]API docs[/url] for more information.
public static String toString(int i)
Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.
Parameters:
i - an integer to be converted.
Returns:
a string representation of the argument in base?10