Results 1 to 5 of 5
  1. #1
    Redexe is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Converting int to string or??

    I'm completely new to java and this forum so please don't rip me to shreds here! I began writing a this code that would convert one unit of measure to another. I wanted to use GUI, there is my code. But im having problems converting the input of the user from text to int. here is the code.



    Java Code:
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    public class GUI {
    
    	private JFrame frame;
    	private JButton EnterButton;
    	private JButton CancelButton;
    	private JButton ConvertButton;
    	private JLabel label;
    	private JPanel panel;
    	private JTextField Convert ;
    	
    	public GUI(){
    		// Create and show GUI
    		frame = new JFrame();
    		frame.setSize(300, 200);
    		frame.setAlwaysOnTop(true);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		panel = new JPanel();
    		frame.add(panel);
    		// Create items
    		EnterButton = new JButton("Enter");
    		CancelButton = new JButton("Cancel");
    		ConvertButton = new JButton("Convert");
    		Convert = new JTextField("....",20);
    		//Place items on panel
    		panel.add(Convert);
    		panel.add(EnterButton);
    		panel.add(ConvertButton);
    		panel.add(CancelButton);
    		
    		// Cancel Button
    		class CancelTrig implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				System.exit(0);
    			}
    		}
    		CancelButton.addActionListener(new CancelTrig());
    		// Convert Button
    		class ConvertTrig implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				int i = Integer.parseInt(Convert.getText());
    				 
    				Convert.setText(i);
    				
    			
    			}
    			
    		}
    		ConvertButton.addActionListener(new ConvertTrig());
    	
    		
    	}
    	
    	
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Converting int to string or??


  3. #3
    Redexe is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Converting int to string or??

    Changed up the code a little bit, worked it out. Not getting any error seems to be right..

    Java Code:
    CancelButton.addActionListener(new CancelTrig());
    		// Convert Button
    		class ConvertTrig implements ActionListener{
    			public void actionPerformed(ActionEvent e){
    				int i = 0;
    				
    				try {
    				i = Integer.parseInt(Convert.getText().toString());
    				}	catch(NumberFormatException nfe){
    					
    					label = new JLabel("Problem with: " + nfe);
    					frame = new JFrame();
    					frame.setSize(450,100);
    					frame.setAlwaysOnTop(true);
    					frame.setVisible(true);
    					frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    					frame.add(label);
    					
    					
    					
    				}
    				
    				
    			
    			}
    			
    		}

  4. #4
    Rogue45 is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default Re: Converting int to string or??

    String s;

    s = Integer.toString(i);

    i is the integer to be converted.

  5. #5
    Redexe is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Converting int to string or??

    Quote Originally Posted by Rogue45 View Post
    String s;

    s = Integer.toString(i);

    i is the integer to be converted.
    Perfect, thank you so much!

Similar Threads

  1. converting binary to string
    By stegano in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 08:20 PM
  2. Converting to String
    By darek9576 in forum New To Java
    Replies: 1
    Last Post: 03-13-2010, 11:07 PM
  3. Is it OK to do this... (converting int to string)
    By Psyclone in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 06:51 PM
  4. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 09:03 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
  •