Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java programing Help?

I have a Java project where I have to build a GUI to convert Celsius to Fahrenheit and vise versa.

I have made that program:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class p8 extends JFrame //creates class p8 (extention on JFrame)

{

private JLabel TempL, ResultsL; //initates TempL and ResultsL (JLables

private JTextField inputTF, resultsTF; //initates inputTF nad resultsTF (JTextFields)

private JButton c2fB, f2cB; //initates c2fB and f2cB (JButtons)

private C2FButtonHandler C2FHandler; //initates C2FButtonHandler

private F2CButtonHandler F2CHandler; //initates F2CButtonHandler

public p8() //begins public class p8

{

Container pane = getContentPane();

pane.setLayout(new GridLayout(3,2)); //sets content pane to a 3 row x 2 column grid

TempL = new JLabel("Temperature:"); //creates new JLabel named Temperature

ResultsL = new JLabel("Results:"); //creates new JLable named Results

inputTF = new JTextField("0",10); //creates new JTextField 10 columns wide and set to zero

resultsTF = new JTextField("",40); //creates new JTextField 40 columns wide with no initial value

c2fB = new JButton("Celsius to Fahrenheit"); //creates new JButton names Celsius to Fahrenheit

f2cB = new JButton("Fahrenheit to Celsius"); //creates new JButton named Fahrenheit to Celsius

C2FHandler = new C2FButtonHandler(); //creates a new ButtonHandler (C2F) and calls it C2FHandler

c2fB.addActionListener(C2FHandler); //waits for c2fB to be pushed and uses C2FHandler when that happens

F2CHandler = new F2CButtonHandler(); //creates new ButtonHandler (F2C) and calls it F2CHandler

f2cB.addActionListener(F2CHandler); //waits for f2cB to be pushed and uses F2CHandler when that happens

pane.add(TempL); //adds TempL to emplt pane in spot 1,1

pane.add(inputTF); //adds inputTF to pane in spot 1,2

pane.add(c2fB); //adds c2fB to pane in spot 2,1

pane.add(f2cB); //adds f2cB to pane in spot 2,2

pane.add(ResultsL); //adds ResultsL to pane in spot 3,1

pane.add(resultsTF); //adds resultsTF to pane in spot 3,2

resultsTF.setEditable(false); //enables user from editing resultsTF

setTitle("Celsius and Fahrenheit conversion"); //assigns title to pane

setSize(375,120); // sets size for pane

setDefaultCloseOperation(EXIT_ON_CLOSE); //sets close operations for pane

setVisible(true); //turns on pane visibility

}

private class C2FButtonHandler implements ActionListener //implements ActionListber for button c2fB

{

public void actionPerformed(ActionEvent e) //method to preform action for button c2fB

{

int Ctemp, Ftemp; //defines integers Ctemp and Ftemp

Ctemp=Integer.parseInt(inputTF.getText()); //gets input from user converts it to an int and defines it as Ctemp

Ftemp= (Ctemp)*9/5+32; //calculates Ftemp

resultsTF.setText(+Ctemp+" Celsius is"+Ftemp+" Fahrenheit"); //display results

}

}

private class F2CButtonHandler implements ActionListener //implements ActionListner for Button f2cB

{

public void actionPerformed(ActionEvent e) //method to preforme action for button f2cB

{

int Ctemp, Ftemp; //defines integers Ctemp and Ftemp

Ftemp=Integer.parseInt(inputTF.getText()); //gets input from user converts it to an int and defines it as Ftemp

Ctemp= ((Ftemp)-32)*5/9; //calculates Ctemp

resultsTF.setText(+Ftemp+" Fahrenheit is"+ Ctemp+" Celsius"); //display results

}

}

public static void main(String[] args) //begins main method

{

p8 window = new p8(); //opens new window p8()

}

}

however there is an extra credit portion that I need help with:

to gain extra credit I have to use try/catch to only allow numeric values to be accepted and alert the user when an invalid entry has been entered.

I have this example out of the book

public static int getNumber()

throws InputMismatchException

{

int num;

try

{

System.out.print("Enter an Integer:");

num=kb.nextInt();

System.out.println();

return num;

}

catch (InputMismatchException imeRef)

{

throw imeRef;

}

}

but I dont know how to get them to work together, or how exactly try/catch works.

need help please

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    A try/catch is used when a line of code may throw an exception and is used to handle the exception so that your program does not crash.

  • 4 years ago

    Why roll your individual whilst there is in all probability a calendar widget you may reuse? by using ways, the Java API itself materials many useful training and interfaces: Date, Calendar, GregorianCalendar, DateFormat, and SimpleDateFormat.

Still have questions? Get your answers by asking now.