I am very new to programming and am really struggling with Java. I'm currently working on the below code, but when I compile the program, I'm getting several messages referring to multiple lines in my code, all stating things like the following:

C:\Users\Heather\Documents\HACH5_8.java:111: cannot find symbol
symbol : variable showFeet
location: class HACH5_8
displayFeet(Meters, showFeet);

Here is my code:

// import java statements here

import java.util.Scanner;			// Importing the Scanner class
import java.io.*;					// Importing the file I/O class
import java.text.DecimalFormat;		// Importing the DecimalFormat class
import javax.swing.JOptionPane;		// Importing the JOptionPane class

// Begin class HACH5_8.java

public class HACH5_8

{
	public static void main(String[] args)

// Begin main

	{
		double meters = 0;		// To hold number of meters
		double kilometers = 0;	// To hold number of kilometers
		double inches = 0;		// To hold number of inches
		double feet = 0;		// To hold number of feet

		// Get the number of meters

			meters = getMeters();

		// Convert the number of meters to kilometers using kilometers = meters * 0.001

			showKilometers = meters * 0.001;

		// Convert the number of meters to inches using inches = meters * 39.37

			showInches = meters * 39.37;

		// Convert the number of meters to feet using feet = meters * 3.281

			showFeet = meters * 3.281;

		// Display number of kilometers

			displayKilometers(Meters, showKilometers);
			System.exit(0);

		// Display number of inches

			displayInches(Meters, showInches);
			System.exit(0);

		// Display number of feet

			displayFeet(Meters, showFeet);
			System.exit(0);

// End main

	}

public static double getMeters()
	{
		String input;				// To hold input
		double numberMeters;		// To hold meters

		// Allow user to enter the number of meters

		input = JOptionPane.showInputDialog(
				"This program converts measurements\n" +
				"in meters to kilometers, meters to\n" +
				"inches, and meters to feet. Enter\n" +
				"the number of meters.");

		// Convert the input to a double

		numberMeters = Double.parseDouble(input);

		// Return the number of meters

		return numberMeters;
	}

	// Allow user to convert to kilometers using kilometers = meters * 0.001

	public static double showKilometers(double numberMeters)
	{
		return numberMeters * 0.001;
	}

	public static void displayResults(double numberMeters, double showKilometers)
	{
		JOptionPane.showMessageDialog(null,
				meters + " meters equals " +
				kilometers + " showKilometers.");
	}

	// Allow user to convert to inches using inches = meters * 39.37

	public static double showInches(double numberMeters)
	{
		return numberMeters * 39.37;
	}

	// Display results of inches

	public static void displayResults(double numberMeters, double showInches)
	{
			JOptionPane.showMessageDialog(null,
					meters + " meters equals " +
					inches + " showInches.");
	}

	// Allow user to convert to feet using feet = meters * 3.281

	public static double showFeet(double numberMeters)
	{
		return numberMeters * 3.281;
	}

	// Display results of feet

	public static void displayResults(double numberMeters, double showFeet)
	{
			JOptionPane.showMessageDialog(null,
						meters + " meters equals " +
						feet + " showFeet.");
	}

// End public class

}

If someone could help me, I would greatly appreciate it!

Recommended Answers

All 21 Replies

Wow... lots of errors...

The first thing that strikes me is that you have several displayResults() methods, all with the same signature (double, double). It doesn't matter that you named the double variables differently in each displayResults() method -- Java identifies a method based on its name and parameters. You have 3 displayResults() methods each taking 2 doubles as parameters. The computer doesn't know which to execute, so you get an error.

The second problem is that you are basically calling methods displayKilometers(), displayFeet() etc, but you defined the methods as showKilometers() and showFeet(). Change these "show" methods to "display" methods to fix this problem -- basically you want the method calling to match up exactly with the signature of the method being called.

The third major problem is that you are using variables showFeet, showKilometers, etc. in the main method without defining them. You want to first define them all as type double at the top.

The fourth problem is that when you are calling the display methods you capitalized the word "metes." Java is case-sensitive, which means that meters and Meters would be different variables.

The last problem is that you are using System.exit(0) several times in your code. Once the first System.exit(0) is reached your code will terminate, which means the lines after it will never be executed.

Good luck with these 5 issues ^^

Ok, I apologize. Part of the program that we are suppose to write is to allow the user to select to calculate it in either kilometers, meters, inches, or feet. If I put just one system.exit() at the end of the program, will this terminate each one as it is completed? Also, can you explain to me why I would do something like:
public static double showFeet(double numbermeters)
in each calculation? Again, I apologize, but we have a teacher who doesn't teach, she refuses to help, and we have no textbook.

You wouldn't. Here's basically how I would restructure your code:

displayInches method
    prints the inputted number of meters in terms of inches
displayFeet method
    prints the inputted number of meters in terms of feet
displayKilometers method
    prints the inputted number of meters in terms of kilometers
main method
    Request user to enter a distance.
    Variable meters <-- distance entered
    Request user to enter a measurement of choice (inches, feet, kilometers)
    if (measurement of choice is inches)
        displayInches(meters)
    else if (measurement of choice is feet)
        displayFeet(meters)
    else //measurement of choice is kilometers
        displayKilometers(meters)
end main method

Of couse, this is pseudocode. Try to apply your java to this basic outline. If you have trouble, post what your attempt and I will help you with whatever is confusing.

If I put just one system.exit() at the end of the program, will this terminate each one as it is completed?

You don't need to put System.exit() in your program at all. Just take it out completely. Your program runs in a top to bottom (sequential) fashion, so it will step through your main() method step by step. When it sees a method call to getMeters() it will enter that method, go through the steps of that method, then return to where it left off in the main() method. . Once the end of the main() method is reached, your program will end automatically.

What he said. The program naturally flows top to bottom. However, you can put "forks in the road" with if statements to redirect the flow based on certain variables (as specified in my previous pseudocode post)

Ok, so I did some revisions and when I compile and run the application, it first asks me to enter the meters, so I do that. Then instead of it letting me select how I want it to be converted (inches, feet, or kilometers), it just gives me a blank black box. I've reposted my new code below. I should also add that part of our assignment is to create a code so an option box opens up at the beginning of our program to allow the user to select how they would like to convert the calculation, but we are suppose to do it in a void method, which I am very confused about because from what I know at this point, a void method returns nothing, and I would need the option box to return a value, correct?

************************************************************************/

// import java statements here

import java.util.Scanner;			// Importing the Scanner class
import java.io.*;					// Importing the file I/O class
import java.text.DecimalFormat;		// Importing the DecimalFormat class
import javax.swing.JOptionPane;		// Importing the JOptionPane class

// Begin class HACH5_8.java

public class HACH5_8

{

	public static void main(String[] args)

// Begin main

	{
		double meters 				= 0;		// To hold number of meters
		double kilometers 			= 0;		// To hold number of kilometers
		double inches 				= 0;		// To hold number of inches
		double feet 				= 0;		// To hold number of feet
		double showFeet		 		= 0; 		// To display total feet
		double showInches 			= 0;		// To display total inches
		double showKilometers 		= 0;		// To display total kilometers
		int showOptions				= 0;		// To display show options box

		// Get the number of meters

			meters = getmeters();

		if (showOptions == 1) {

		// Convert the number of meters to kilometers using kilometers = meters * 0.001

			showKilometers(meters);
		}
		else if (showOptions == 2) {

		// Convert the number of meters to inches using inches = meters * 39.37

			showInches(meters);
		}
		else if (showOptions == 3) {

		// Convert the number of meters to feet using feet = meters * 3.281

			showFeet(meters);
		}
		else if (showOptions == 4) {

		// Allow user to quit the program

			JOptionPane.showInputDialog(
			"Do you want to perform\n" +
			"another calculation?\n" +
			"Enter 'y' for yes or\n" +
			"'n' for no.");
		}


// End main

	}

public static double getmeters()
	{
		String input;				// To hold input
		double numberMeters;		// To hold meters

		// Allow user to enter the number of meters

		input = JOptionPane.showInputDialog(
				"This program converts measurements\n" +
				"in meters to kilometers, meters to\n" +
				"inches, and meters to feet. Enter\n" +
				"the number of meters.");

		// Convert the input to a double

		numberMeters = Double.parseDouble(input);

		// Return the number of meters

		return numberMeters;
	}

	// Allow user to convert to kilometers using kilometers = meters * 0.001


	public static void showKilometers(double numberMeters)
	{
		JOptionPane.showMessageDialog(null,
				numberMeters + " meters equals " +
				(numberMeters * 0.001) + " showKilometers.");
	}

	// Allow user to convert to inches using inches = meters * 39.37


	public static void showInches(double numberMeters)
	{
			JOptionPane.showMessageDialog(null,
					numberMeters + " meters equals " +
					(numberMeters * 39.37) + " showInches.");
	}

	// Allow user to convert to feet using feet = meters * 3.281


	public static void showFeet(double numberMeters)
	{
			JOptionPane.showMessageDialog(null,
						numberMeters + " meters equals " +
						(numberMeters * 3.281) + " showFeet.");
	}

	// Allow user to select if they want to calculate in inches, feet, kilometers, or exit the program

	public static void showOptions()
	{
			showOptions = JOptionPane.showInputDialog(
				"Please select calculation to perform:\n" +
				"1.  Convert to kilometers\n" +
				"2.  Convert to inches\n" +
				"3.  Convert to feet\n" +
				"4.  Exit the program");
	}


// End public class

}

Call the showOptions() method right after your call to getmeters(). And make showOptions an instance variable not a variable declared in main. Read about variable scope in Java if you don't know what I'm talking about.

Ok, adding on to this original post... I'm getting error messages stating

"C:\Users\Heather\Documents\HACH5_8.java:180: illegal start of expression
public static double getmeters()", "C:\Users\Heather\Documents\HACH5_8.java:180: ';' expected
public static double getmeters()", and "C:\Users\Heather\Documents\HACH5_8.java:244: reached end of file while parsing
}   // End public class HACH5_8
 ^".  

What exactly do these mean? When I compile the program, it's these 3 messages, but on different lines of the code. The code I am now using is below:

// import java statements here

import java.util.Scanner;           // Importing the Scanner class
import java.io.*;                   // Importing the file I/O class
import java.text.DecimalFormat;     // Importing the DecimalFormat class
import javax.swing.JOptionPane;     // Importing the JOptionPane class

// Begin class HACH5_8.java

public class HACH5_8

{

    public static void main(String[] args)


// Begin main

    {
        double meters               = 0;        // To hold number of meters
        double kilometers           = 0;        // To hold number of kilometers
        double inches               = 0;        // To hold number of inches
        double feet                 = 0;        // To hold number of feet
        double showFeet             = 0;        // To display total feet
        double showInches           = 0;        // To display total inches
        double showKilometers       = 0;        // To display total kilometers
        int showOptions             = 0;        // To display show options box
        boolean doAgain             = true;     // Boolean continue
        String yesOrNo              = "";       // "Yes" or "No"
        String theValue             = "";       // Holds input value

            // Get the number of meters

            meters = getmeters();

        while (doAgain)
        {   // Begin while (doAgain)

            showOptions();

            while (option != 4)
                    option = 0;

            while (option == 0)
            {

                try
                {
                    System.out.println();
                    System.out.println("1.  Convert to kilometers\n4.  Exit");
                    System.out.println();
                    System.out.print("Select your option: ");
                    option = Integer.parseInt(keyboard.next());
                }
                catch (Exception ex)
                {
                    System.out.println("Invalid input");
                }


            if (showOptions == 1)
            {

            // Convert the number of meters to kilometers using kilometers = meters * 0.001

            showKilometers(meters);
            }

            else if (showOptions == 2)
            {

            // Convert the number of meters to inches using inches = meters * 39.37

            showInches(meters);
            }

            else if (showOptions == 3)
            {

            // Convert the number of meters to feet using feet = meters * 3.281

            showFeet(meters);
            }

            else if (showOptions == 4)
            {

            // Allow user to quit the program
            yesOrNo = JOptionPane.showInputDialog(
            "Do you want to perform\n" +
            "another calculation?\n" +
            "Enter 'y' for yes or\n" +
            "'n' for no.");

            if ((yesOrNo.equals("Y")) || (yesOrNo.equals("y")))
                        {
                            doAgain = true;
                        }

                        else if ((yesOrNo.equals("N")) || (yesOrNo.equals("n")))
                        {
                            doAgain = false;
                        }

                        else
                        {
                            JOptionPane.showMessageDialog(null,
                                    "Invalid input.  Try again.");
                        }

            }




        }   // End while (doAgain)

    }   // End main

public static double getmeters()

    {

                double numberMeters;        // To hold meters

                // Allow user to enter the number of meters

                numberMeters = Double.parseDouble(JOptionPane.showInputDialog(
                        "This program converts measurements\n" +
                        "in meters to kilometers, meters to\n" +
                        "inches, and meters to feet. Enter\n" +
                        "the number of meters."));

                // Convert the input to a double


                // Return the number of meters

        return numberMeters;
    }   // End public class

    // Allow user to convert to kilometers using kilometers = meters * 0.001


    public static void showKilometers(double numberMeters)
    {
        JOptionPane.showMessageDialog(null,
                numberMeters + " meters equals " +
                (numberMeters * 0.001) + " showKilometers.");
    }   // End public class

    // Allow user to convert to inches using inches = meters * 39.37


    public static void showInches(double numberMeters)
    {
            JOptionPane.showMessageDialog(null,
                    numberMeters + " meters equals " +
                    (numberMeters * 39.37) + " showInches.");
    }   // End public class

    // Allow user to convert to feet using feet = meters * 3.281


    public static void showFeet(double numberMeters)
    {
            JOptionPane.showMessageDialog(null,
                        numberMeters + " meters equals " +
                        (numberMeters * 3.281) + " showFeet.");
    }   // End public class

    // Allow user to select if they want to calculate in inches, feet, kilometers, or exit the program

    public static void showOptions()
    {
            JOptionPane.showMessageDialog(null,
                        "Please select calculation to perform:\n" +
                        "1.  Convert to kilometers\n" +
                        "2.  Convert to inches\n" +
                        "3.  Convert to feet\n" +
                        "4.  Exit the program");
    }   // End public class

}   // End public class HACH5_8

Please use code tags. Just go back and edit them into your post.

Please use code tags. Just go back and edit them into your post.

What exactly are you referring to when you say "code tags"?

http://www.daniweb.com/forums/announcement9-3.html

Click the button that says "(code)" on the reply box. Put your code inside of the two tags that will show up. I thought you already knew this because your previous posts in this thread used code tags..

I did do this. I am redoing it now.

// import java statements here

import java.util.Scanner;			// Importing the Scanner class
import java.io.*;					// Importing the file I/O class
import java.text.DecimalFormat;		// Importing the DecimalFormat class
import javax.swing.JOptionPane;		// Importing the JOptionPane class

// Begin class HACH5_8.java

public class HACH5_8

{

	public static void main(String[] args)


// Begin main

	{
		double meters 				= 0;		// To hold number of meters
		double kilometers 			= 0;		// To hold number of kilometers
		double inches 				= 0;		// To hold number of inches
		double feet 				= 0;		// To hold number of feet
		double showFeet		 		= 0; 		// To display total feet
		double showInches 			= 0;		// To display total inches
		double showKilometers 		= 0;		// To display total kilometers
		int showOptions				= 0;		// To display show options box
		boolean doAgain				= true;		// Boolean continue
		String yesOrNo				= "";		// "Yes" or "No"
		String theValue				= ""; 		// Holds input value

			// Get the number of meters

			meters = getmeters();

		while (doAgain)
		{	// Begin while (doAgain)

			showOptions();

			while (option != 4)
					option = 0;

			while (option == 0)
			{

				try
				{
					System.out.println();
					System.out.println("1.  Convert to kilometers\n4.  Exit");
					System.out.println();
					System.out.print("Select your option: ");
					option = Integer.parseInt(keyboard.next());
				}
				catch (Exception ex)
				{
					System.out.println("Invalid input");
				}


			if (showOptions == 1)
			{

			// Convert the number of meters to kilometers using kilometers = meters * 0.001

			showKilometers(meters);
			}

			else if (showOptions == 2)
			{

			// Convert the number of meters to inches using inches = meters * 39.37

			showInches(meters);
			}

			else if (showOptions == 3)
			{

			// Convert the number of meters to feet using feet = meters * 3.281

			showFeet(meters);
			}

			else if (showOptions == 4)
			{

			// Allow user to quit the program
			yesOrNo	= JOptionPane.showInputDialog(
			"Do you want to perform\n" +
			"another calculation?\n" +
			"Enter 'y' for yes or\n" +
			"'n' for no.");

			if ((yesOrNo.equals("Y")) || (yesOrNo.equals("y")))
						{
							doAgain = true;
						}

						else if ((yesOrNo.equals("N")) || (yesOrNo.equals("n")))
						{
							doAgain = false;
						}

						else
						{
							JOptionPane.showMessageDialog(null,
									"Invalid input.  Try again.");
						}

			}




		}	// End while (doAgain)

	}	// End main

public static double getmeters()

	{

				double numberMeters;		// To hold meters

				// Allow user to enter the number of meters

				numberMeters = Double.parseDouble(JOptionPane.showInputDialog(
						"This program converts measurements\n" +
						"in meters to kilometers, meters to\n" +
						"inches, and meters to feet. Enter\n" +
						"the number of meters."));

				// Convert the input to a double


				// Return the number of meters

		return numberMeters;
	}	// End public class

	// Allow user to convert to kilometers using kilometers = meters * 0.001


	public static void showKilometers(double numberMeters)
	{
		JOptionPane.showMessageDialog(null,
				numberMeters + " meters equals " +
				(numberMeters * 0.001) + " showKilometers.");
	}	// End public class

	// Allow user to convert to inches using inches = meters * 39.37


	public static void showInches(double numberMeters)
	{
			JOptionPane.showMessageDialog(null,
					numberMeters + " meters equals " +
					(numberMeters * 39.37) + " showInches.");
	}	// End public class

	// Allow user to convert to feet using feet = meters * 3.281


	public static void showFeet(double numberMeters)
	{
			JOptionPane.showMessageDialog(null,
						numberMeters + " meters equals " +
						(numberMeters * 3.281) + " showFeet.");
	}	// End public class

	// Allow user to select if they want to calculate in inches, feet, kilometers, or exit the program

	public static void showOptions()
	{
			JOptionPane.showMessageDialog(null,
						"Please select calculation to perform:\n" +
						"1.  Convert to kilometers\n" +
						"2.  Convert to inches\n" +
						"3.  Convert to feet\n" +
						"4.  Exit the program");
	}	// End public class

}	// End public class HACH5_8

1. You never declared the variable "option". You can't use a variable that was never declared.
2. You never declared the variable "keyboard". You used the statement "keyboard.next()" which implies you wanted to use keyboard as if it was a Scanner Object. Somewhere before that line, put this line:

Scanner keyboard = new Scanner(System.in);

3. Where you have the comment that says "End while (do again)", you don't have enough closing brackets. You need to add another } after that line.

I think an IDE like Eclipse might be very helpful to you. It took me only a few seconds each to spot all of your errors while using Eclipse - it highlights things like undeclared variables and not using the correct number of closing brackets for you.

Ok, thank you. I know some more advanced students use Eclipse. What version do you use? Also, have you had any problem in using it with windows 7? I had a hard time getting Java installed because of Windows 7 errors.

I use the version I linked you to - Eclipse for Java Developers. And no, there are no issues that I know of with using it for Windows 7 (which is what I'm using right now).

Ok, thank you. I will work on getting that downloaded tonight. I have one more question. I need to put a catch in my program that will not allow the user to enter a negative number for meters. Can I do that in the same area that I currently have the try/ catch, or do I need to create a whole new area for that with another while loop?

You shouldn't use a try catch to make sure that the user enters a positive number. You should use a try catch to prevent error conditions from crashing your program. Entering a negative number is not an error condition. An example of an error condition would be attempting to parse an int when the user entered a String (for example, see the code below)

try{
int value = Integer.parseInt("This String can't be parsed as an int! It'll cause an Exception!");
} catch(Exception e){
System.out.println("womp womp.");
}

To answer your original question, whether or not you can combine the two try catches depends on how you declare the catch statement. For example, an "Exception" will catch more errors than a NumberFormatException because an Exception is less specific (that is, more things fall under the category of 'Exception'). However, having more specific types like NumberFormatException is useful because the error message will be more clear -- it'll tell you more precisely what has gone wrong with your program. So you should balance these two needs carefully. In most cases, I would recommend using the specific type of Exception (for example, since the parseInt() method that I showed you above throws a NumberFormatException if there is an error, you should therefore use a NumberFormatException in the catch block). However, sometimes you may just want to catch any Exceptions in which case just use Exception and put all of your code into the same try block. I hope that helps.

Ok, so I added the following part to my code and now I am getting an error of "java:155: showOptions() in HACH5_8 cannot be applied to (java.lang.String) if (showOptions(""))". I'm assuming there is something I need to do to have it be converted to an integer, but when I tried doing:

showOptions = Integer.parseInt(option);

but that's didn't work. Here is the part of my code I am referring to:

if (showOptions(""))
			{
				JOptionPane.showMessageDialog(null,
						"No input value.  Try again.");
			}

			if ((showOptions < 0) || (showOptions > 4))
			{
				JOptionPane.showMessageDialog(null,
						"Please select option 1, 2, 3, or 4.");
			}

I placed this after my final else if closing bracket from where the user would enter "y" or "n" to continue.

Repost your up to date code. And I just noticed you're using

option = Integer.parseInt(keyboard.next());

when it would be more intuitive to use

option = keyboard.nextInt()

You should also consider rewriting some of your code for modularity. For example, you have an entanglement of loops and if statements - as a code reviewer, I'm not even sure what you're trying to do. I'm going to repost the code that you showed me earlier to show you how modularity can make your code easier to read and less spaghetti looking.

  • Use informative variable names. As an example, "doAgain" is a bad variable name because it tells us nothing about when and in what circumstances you want to "do something again".
  • In your getmeters method, if the user enters invalid input, your program will crash. You should use a try catch.
  • getmeters should be getMeters. It is just convention, use camelcase.
  • Most people would consider naming a variable showOptions and naming a method showOption() a bad idea. It is confusing to look at.
//This doesn't make sense!
while (option != 4) option = 0;
//Use this instead!
if(option != 4) option = 0;

And furthermore, why would you want to do that to begin with? Comments could help a lot here. What is option 4, what is it supposed to do, and why do you want it to be option 0 instead?

Chunks of code like the one below should not be in main(), they should be in their own method:

// Allow user to quit the program
			yesOrNo	= JOptionPane.showInputDialog(
			"Do you want to perform\n" +
			"another calculation?\n" +
			"Enter 'y' for yes or\n" +
			"'n' for no.");

			if ((yesOrNo.equals("Y")) || (yesOrNo.equals("y")))
						{
							doAgain = true;
						}

						else if ((yesOrNo.equals("N")) || (yesOrNo.equals("n")))
						{
							doAgain = false;
						}

						else
						{
							JOptionPane.showMessageDialog(null,
									"Invalid input.  Try again.");
						}

			}

You should make a method to ask the user whether or not they want to quit the program. Then your main method would look more like this:

main(String[] args){
. .
doAgain = askUserQuitOrContinue();
. . 
}

So you see, using another method there will clean up your main method quite a bit, not to mention make it easier to see what is going on in main. Use clear method names and clear variable names and use methods that do one thing and you will make a huge improvement. You could also work on comments - try to design comments that would be helpful to you if you were to come back to your code in a year and read it. If you do that, then other people will understand your code as well.

Ok, I made some revisions to my code and now have it so there are no compiling errors, but when I run the application, it gives me the input box to enter in the meters, so I do that. Then it takes me to the input box to enter which option I want to select. I can enter a number, but when I hit okay or cancel on the input box, nothing will happen- I literally have to close the windows. What am I missing?

// import java statements here

import java.util.Scanner;			// Importing the Scanner class
import java.io.*;					// Importing the file I/O class
import java.text.DecimalFormat;		// Importing the DecimalFormat class
import javax.swing.JOptionPane;		// Importing the JOptionPane class

// Begin class HACH5_8.java

public class HACH5_8

{

	public static void main(String[] args)


// Begin main

	{
		double meters 				= 0;		// To hold number of meters
		double kilometers 			= 0;		// To hold number of kilometers
		double inches 				= 0;		// To hold number of inches
		double feet 				= 0;		// To hold number of feet
		double showFeet		 		= 0; 		// To display total feet
		double showInches 			= 0;		// To display total inches
		double showKilometers 		= 0;		// To display total kilometers
		int showOptions				= 0;		// To display show options box
		boolean doAgain				= true;		// Boolean continue
		String yesOrNo				= "";		// "Yes" or "No"
		String theValue				= ""; 		// Holds input value
		int option					= 0;		// To display option

			// Get the number of meters

			meters = getmeters();

		while (doAgain)
		{	// Begin while (doAgain)

			showOptions();

			if (showOptions == 1)
			{

			// Convert the number of meters to kilometers using kilometers = meters * 0.001

			showKilometers(meters);
			}

			else if (showOptions == 2)
			{

			// Convert the number of meters to inches using inches = meters * 39.37

			showInches(meters);
			}

			else if (showOptions == 3)
			{

			// Convert the number of meters to feet using feet = meters * 3.281

			showFeet(meters);
			}

			else if (showOptions == 4)
			{

			// Allow user to quit the program
			yesOrNo	= JOptionPane.showInputDialog(
			"Do you want to perform\n" +
			"another calculation?\n" +
			"Enter 'y' for yes or\n" +
			"'n' for no.");

			if ((yesOrNo.equals("Y")) || (yesOrNo.equals("y")))
						{
							doAgain = true;
						}

						else if ((yesOrNo.equals("N")) || (yesOrNo.equals("n")))
						{
							doAgain = false;
						}

						else
						{
							JOptionPane.showMessageDialog(null,
									"Invalid input.  Try again.");
						}


			}  // End else if

			if ((showOptions < 0) || (showOptions > 4))
			{
				JOptionPane.showMessageDialog(null,
						"Please select option 1, 2, 3, or 4.");
			}

		}	// End while (doAgain)

	}	// End main

public static double getmeters()

	{

				double numberMeters;		// To hold meters

				// Allow user to enter the number of meters

				numberMeters = Double.parseDouble(JOptionPane.showInputDialog(
						"This program converts measurements\n" +
						"in meters to kilometers, meters to\n" +
						"inches, and meters to feet. Enter\n" +
						"the number of meters."));

				if (numberMeters < 0)
							{
								JOptionPane.showMessageDialog(null,
										"Please enter number greater than 0.");
							}


				// Return the number of meters

		return numberMeters;


	}	// End public class

	// Allow user to convert to kilometers using kilometers = meters * 0.001


	public static void showKilometers(double numberMeters)
	{
		JOptionPane.showMessageDialog(null,
				numberMeters + " meters equals " +
				(numberMeters * 0.001) + " showKilometers.");
	}	// End public class

	// Allow user to convert to inches using inches = meters * 39.37


	public static void showInches(double numberMeters)
	{
			JOptionPane.showMessageDialog(null,
					numberMeters + " meters equals " +
					(numberMeters * 39.37) + " showInches.");
	}	// End public class

	// Allow user to convert to feet using feet = meters * 3.281


	public static void showFeet(double numberMeters)
	{
			JOptionPane.showMessageDialog(null,
						numberMeters + " meters equals " +
						(numberMeters * 3.281) + " showFeet.");
	}	// End public class

	// Allow user to select if they want to calculate in inches, feet, kilometers, or exit the program

	public static void showOptions()
	{
			JOptionPane.showInputDialog(null,
						"Please select calculation to perform:\n" +
						"1.  Convert to kilometers\n" +
						"2.  Convert to inches\n" +
						"3.  Convert to feet\n" +
						"4.  Exit the program");
	}	// End public class

}	// End public class HACH5_8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.