954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Menu Help

Hi Everyone,

I am writing a program to convert measurements. I need the to program to have a menu. I have different units of measure to convert to one particular one. I think that I need to use a case statement. But I have no idea on how to construct one. If anyone can lead me into the right direction please.

Thanks Jeremy

simpsonjr
Newbie Poster
6 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

Hey Everyone this what I got so far


System.out.println("Conversion Program");
System.out.println("");
System.out.println("Conversion Types:");
System.out.println("1. Convert BTUs to joules");
System.out.println("2. Convert calories to joules");
System.out.println("3. Convert joules to BTUs");
System.out.println("4. Convert joules to calories");
System.out.println("5. Exit the program");
System.out.println("");
System.out.print("Please select an option from 1-5");
Input = Integer.parseInt(stdin.readLine());

if (Input < 0) or (Input > 5);
{
System.out.println("You have entered an invalid selection, please try again");
return;
}

System.out.print("Please enter the amount you wish to convert");
Amount = Integer.parseInt(stdin.readLine());

if (Input == 1);
{
Total == (Amount * 1056);
}
if (Input == 2);
{
Total == (Amount * 4.184);
}
etc.

Once you go through the ifs then do:
System.out.println("Conversion Complete: " + Total + " joules");

Am I on the right path thanks

Jeremy

simpsonjr
Newbie Poster
6 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

OK I got to Compile but it is not doing what I want. Here is the Code

import java.util.*;
import java.io.*;


public class UnitConverter {
public static void main(String[] args) {

int Input;
double Amount;
double Total;
System.out.println("Conversion Program");
System.out.println("");
System.out.println("Conversion Types:");
System.out.println("1. Convert BTUs to joules");
System.out.println("2. Convert calories to joules");
System.out.println("3. Convert joules to BTUs");
System.out.println("4. Convert joules to calories");
System.out.println("5. Exit the program");
System.out.println("");
System.out.print("Please select an option from 1-5 ");
Scanner in = new Scanner(System.in);
Input = in.nextInt();
System.out.print("What is the amount to be converted");
Amount = in.nextInt();
if (Input == 1);
{
Total = (Amount * 1056);
}
if (Input == 2);
{
Total = (Amount * 4.184);
}
if (Input == 3);
{
Total = (Amount / 1056);
}
if (Input == 4);
{
Total = (Amount / 4.184);
}
if((Input < 0) || (Input > 5));
{
System.out.println(" You have entered an invalid selection, please try again");
}

System.out.println("Conversion Complete: " + Total + " joules");
}
}

Here is what I get:

Conversion Types:
1. Convert BTUs to joules
2. Convert calories to joules
3. Convert joules to BTUs
4. Convert joules to calories
5. Exit the program

Please select an option from 1-5 1
What is the amount to be converted22
You have entered an invalid selection, please try again
Conversion Complete: 5.25812619502868 joules

Any help would be great Thanks.

Jeremy

simpsonjr
Newbie Poster
6 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

What do you mean you want to have a menu? Are you meaning a graphical menu, because your program looks like it runs at the command line. I think what you meant is you want something to list the available units of measure or something, if so let us know.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

server_crash

you are right. I got it to work here is the end code tell me what you think

import java.util.*;
import java.io.*;

public class UnitConverter {
public static void main(String[] args) {

int Input = 0;
double Amount = 0.0;
double Total = 0.0;

System.out.println("Conversion Program");
System.out.println("");

while (Input != 5) {

System.out.println("Conversion Types:");
System.out.println("1. Convert BTUs to joules");
System.out.println("2. Convert calories to joules");
System.out.println("3. Convert joules to BTUs");
System.out.println("4. Convert joules to calories");
System.out.println("5. Exit the program");
System.out.println("");
System.out.print("Please select an option from 1-5 ");

Scanner in = new Scanner(System.in);
Input = in.nextInt();

while ((Input < 1) || (Input > 5))
{
System.out.println(" You have entered an invalid selection, please try again");
System.out.print("Please select an option from 1-5 ");
in = new Scanner(System.in);
Input = in.nextInt();
}

if (Input == 5){
System.out.println("Goodbye");
}
else{
System.out.print("What is the amount to be converted ");
Amount = in.nextInt();

if (Input == 1)
{
Total = (Amount * 1056);
System.out.println("Conversion Complete: " + Total + " joules\n");
}
else if (Input == 2)
{
Total = (Amount * 4.184);
System.out.println("Conversion Complete: " + Total + " joules\n");
}
else if (Input == 3)
{
Total = (Amount / 1056);
System.out.println("Conversion Complete: " + Total + " joules\n");
}
else
{
Total = (Amount / 4.184);
System.out.println("Conversion Complete: " + Total + " joules\n");
}
}
}
}
}


Thanks
Jeremy
:D

simpsonjr
Newbie Poster
6 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

Dude, I really liked how you had the menu's, Ithought that was pretty cool. Overall the program looks pretty good. You might think about submiting it to the java code bank on this site.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Thanks Server_Crash

How do I do that?

Jeremy
:rolleyes:

simpsonjr
Newbie Poster
6 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

In the java forum there should be something at the top that says: java code snipets. just go there and find were it says to submit one.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You