Centimeter to Meter converter in Java

Centimeter to Meter converter in Java
  • Version
  • Download 68
  • File Size 3.84 KB
  • Create Date May 12, 2016

Centimeter to Meter converter in Java

This not just a centimeter to meter converter, you can also convert meter to centimeter. This is a java program written in console and gui version that allows the user to select an option whether to convert centimeter to meter or meter to centimeter. This will also allow you to choose whether to close the program or continue and run again the program.

Source code:

import javax.swing.*;
public class CentimeterToMeterGUI
{ 
public static void main(String args[]) {

String input1;	 
double centimeterToConvert=0, meterToConvert=0;
double result=0;
String cont="n";
String option;

do
{
		option=JOptionPane.showInputDialog(null, "Please select an operation:" + "
" + "1 for centimeter to meter" + "
" + "2 for meter to centimeter" );

if (option.matches("1")){
	input1 = JOptionPane.showInputDialog(null,"Enter centimeter value: ");
	centimeterToConvert=Double.parseDouble(input1);
	
	result = centimeterToConvert / 100 ;
	JOptionPane.showMessageDialog(null,  centimeterToConvert + " cm " + " is equal to " + result + " meter(s)");

} else if (option.matches("2")){
	input1 = JOptionPane.showInputDialog(null,"Enter meter value: ");
	meterToConvert=Double.parseDouble(input1);

	result = meterToConvert * 100 ;
	JOptionPane.showMessageDialog(null,  meterToConvert + " m " + " is equal to " + result + " centimeter(s)");

}

	cont =	JOptionPane.showInputDialog(null,"Try Again? (press y or n) ");
	}while (cont.matches("y"));

JOptionPane.showMessageDialog(null,"Program closing"  );

	}
}

, , , , , ,

Post navigation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.