Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you link numbers to a table ie "1" goes to menu 1

Posted on 1999-10-03
5
Medium Priority
?
237 Views
Last Modified: 2010-05-18
I'll provide what I have of the program

1) I can't link the 4 choices to my menu
2) How do I quit the program by pressing the number 4
3) After one calculation is finished how does the user do another calculationimport java.io.*;
      
class converter {
      
public static void main (String[] args) throws IOException {

            BufferedReader stdin = new BufferedReader
            (new InputStreamReader(System.in));
            String message;
            
      double ounces, grams;
      double inches, meters;
      double celsius, fahrenheit;
      
System.out.println("This program will convert some imperial units to some Metric units");
      System.out.println("");
      System.out.println("Main Menu");
      System.out.println("");
      System.out.println("1. Ounces to Grams Table");
      System.out.println("2. Inches to Meters Table");
       System.out.println("3. Celsius to Fahrenheit Table");
      System.out.println("4. Quit");
      System.out.println("");
      System.out.print("Please enter the number to select the table you want to create:  ");
      System.out.print("");
      System.out.println("");
      
      int choice = Integer.parseInt (stdin.readLine());
      if(choice <= 0)
            System.out.println("You suck");
      
      if(choice == 1);
            System.out.println("Conversion from Ounces to Grams");
            System.out.print("Ounces = ");
              ounces = new Double(stdin.readLine()).doubleValue();
             grams = ounces * 28.35;
            System.out.println("Grams = " + grams + "g");
      
      if (choice == 2);
            System.out.println("Conversion from Inches to Meters");
            System.out.print("Inches = ");
                  inches = new Double(stdin.readLine()).doubleValue();
                  meters = inches / 39.37079;
            System.out.println("Meters = " + meters + "m");
      
      if (choice == 3);
            System.out.println("Conversion from Celsius to Fahrenheit");
            System.out.print("Celsius =  ");
                  celsius = new Double(stdin.readLine()).doubleValue();
                  fahrenheit = ((celsius *9 /5) + 32);
            System.out.println("Fahrenheit = " + fahrenheit + "F");
            
      if (choice == 4);
            System.out.println("Thank you, have a nice day!");
      
      
      
      }
}

0
Comment
Question by:lee540
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Expert Comment

by:nebeker
ID: 2095138
To have the program run more than once, wrap the whole thing in a "while" loop that never ends.  This way, when the user has chosen an option, the program will wrap back up to the top of the top and re-present the menu.

hint:  Put everything between your first and last "System.out.println(...)" statements in the while loop.

To exit a java program, use System.exit(0);


0
 
LVL 5

Expert Comment

by:Jod
ID: 2095318
The reaon none of your choices link up is the semi colon on the end of each if statement.

if(choice == 1);
if (choice == 2);
if (choice == 3);
if (choice == 4);

You need to execute the block of code for each statement only IF the choice is the correct one. To do this you need to create a block of code after the IF statement that will only run if the IF statement is true.

For example:

if(choice == 1) {
  System.out.println("Conversion from Ounces to Grams");
  System.out.print("Ounces = ");
  ounces = new Double(stdin.readLine()).doubleValue();
  grams = ounces * 28.35;
  System.out.println("Grams = " + grams + "g");
}

See how the open curly bracket is used after the IF statement to specify the start of your block and the closing one specifies the end of the block.

Everything in this block will only execute if choice is 1. You need to do the same for the other coices and then they will link up with your menu options and only run when the correct number is entered.
0
 
LVL 1

Expert Comment

by:RAKESH
ID: 2102375
1)First since you want to quit your programme use infinite
  while loop so that it will look out for user input from      command line .use skip function since when you enter number and press enter key it will attach '\n' character with it.
2)your menu is not linking because there is semicolon where
  braces are required.
3)It is bad programming practice to use multiple if statement rather use switch statement.
0
 

Author Comment

by:lee540
ID: 2113605
Sorry I already figured out the problem using hints

0
 
LVL 2

Accepted Solution

by:
shaveri earned 200 total points
ID: 2172367
Try out the corrected code...


                import java.io.*;

                public class converter {

                public static void main (String[] args) throws IOException {

                BufferedReader stdin = new BufferedReader
                (new InputStreamReader(System.in));
                String message;

                double ounces, grams;
                double inches, meters;
                double celsius, fahrenheit;
                while(true){
                System.out.println("This program will convert some imperial units to some Metric units");
                System.out.println("");
                System.out.println("Main Menu");
                System.out.println("");
                System.out.println("1. Ounces to Grams Table");
                System.out.println("2. Inches to Meters Table");
                       System.out.println("3. Celsius to Fahrenheit Table");
                System.out.println("4. Quit");
                System.out.println("");
                System.out.print("Please enter the number to select the table you want to create:  ");
                System.out.print("");
                System.out.println("");

                int choice = Integer.parseInt (stdin.readLine());
                if(choice <= 0)
                System.out.println("You suck");

                if(choice == 1){
                System.out.println("Conversion from Ounces to Grams");
                System.out.print("Ounces = ");
                ounces = new Double(stdin.readLine()).doubleValue();
                grams = ounces * 28.35;
                System.out.println("Grams = " + grams + "g");
                }      
                if (choice == 2){
                System.out.println("Conversion from Inches to Meters");
                System.out.print("Inches = ");
                inches = new Double(stdin.readLine()).doubleValue();
                meters = inches / 39.37079;
                System.out.println("Meters = " + meters + "m");
                }      
                if (choice == 3){
                System.out.println("Conversion from Celsius to Fahrenheit");
                System.out.print("Celsius =  ");
                celsius = new Double(stdin.readLine()).doubleValue();
                fahrenheit = ((celsius *9 /5) + 32);
                System.out.println("Fahrenheit = " + fahrenheit + "F");
                }      
                if (choice == 4){
                System.out.println("Thank you, have a nice day!");
                System.exit(0);      
                }
                }
            
                }
                }
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question