Results 1 to 2 of 2
  1. #1
    ivylim88 is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default convert characters to its equivalent value in decimal and hexadecimal

    Basically, i'm trying to create a panel where it can convert char to hex value and decimal value.
    I'm a beginner in java, my actionlistener doesn't seem to work :(
    Can i convert character to integer directly by using Character.getNumericValue() ? :confused:


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CharConverterPanel extends JPanel
    {	
    	private JLabel Insert, Base, Eq, value, box;
    	private JRadioButton decimal, hexa;
    	private JButton convert;
    	private JTextField blank;
    		
    	public CharConverterPanel()
    	{
    	 blank = new JTextField(5);
    	 blank.addActionListener(new SelectionListener());
    	 
    	 Insert = new JLabel("Insert char: ");
    	 
    	 Base = new JLabel("Base: ");
    	 box = new JLabel("   ");
    	 
    	 
    	 Eq = new JLabel("Equivalent to: ");
    	 
    	 value = new JLabel("    ");
    	 
    	 decimal = new JRadioButton(" Decimal ");
    	 hexa = new JRadioButton(" Hexadecimal ");
    	 
    	 convert = new JButton (" Convert ");
    	 convert.addActionListener( new SelectionListener());
    	 
    	 ButtonGroup group = new ButtonGroup();
    	 group.add(decimal);
    	 group.add(hexa);
    	 
    	 decimal.addActionListener(new SelectionListener());
    	 hexa.addActionListener(new SelectionListener());
    	 
    	 add(Insert);
    	 add(blank);
    	 add(Base);
    	 add(box);
    	 add(Eq);
    	 add(value);
    	 add(decimal);
    	 add(hexa);
    	 add(convert);
    	 
    	 setPreferredSize(new Dimension(300, 100));
    	 }
    	 public class SelectionListener implements ActionListener
    	 {
    		public void actionPerformed (ActionEvent e)
    		{
    			char temp;
    			String b = blank.getText();
    			temp = b.charAt(0);
    				if(e.getSource()==convert){ 
    				if( e.getSource()==decimal){
    				int tempp =Character.getNumericValue(temp);
    				value.setText(Integer.toString(tempp));
    				box.setText("10");
    				}
    				if(e.getSource()==hexa){
    				int tempp2 =Character.getNumericValue(temp);
    				value.setText(Integer.toHexString(tempp2)); 
    				box.setText("16");
    				}
    				
    	}
    	}				 
    	}
    	}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ivylim88 View Post
    Can i convert character to integer directly by using Character.getNumericValue() ? :confused:
    I bet you're just wildly guessing and that you've never read the API documentation for the Character class and all its utility methods.

    kind regards,

    Jos

Similar Threads

  1. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  2. How to convert a String into an Hexadecimal ?
    By ze snow in forum New To Java
    Replies: 7
    Last Post: 02-16-2010, 10:31 PM
  3. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  4. Replies: 0
    Last Post: 04-06-2008, 07:40 PM
  5. Replies: 7
    Last Post: 01-12-2008, 09:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •