Java program to demonstrate menu selection : Console « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » ConsoleScreenshots 
Java program to demonstrate menu selection
Java program to demonstrate menu selection
  
/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton


ISBN: 0849308100
Publisher: CRC Press
*/


// File name: MenuDemo.java
//Reference: Chapter 9
//
//Java program to demonstrate menu selection
//Topics:
// 1. Using the switch construct
//
//Requires:
// 1. Keyin class in the current directory

public class ConsoleMenuDemo {

  public static void main(String[] args) {
    // Local variable
    int swValue;

    // Display menu graphics
    System.out.println("============================");
    System.out.println("|   MENU SELECTION DEMO    |");
    System.out.println("============================");
    System.out.println("| Options:                 |");
    System.out.println("|        1. Option 1       |");
    System.out.println("|        2. Option 2       |");
    System.out.println("|        3. Exit           |");
    System.out.println("============================");
    swValue = Keyin.inInt(" Select option: ");

    // Switch construct
    switch (swValue) {
    case 1:
      System.out.println("Option 1 selected");
      break;
    case 2:
      System.out.println("Option 2 selected");
      break;
    case 3:
      System.out.println("Exit selected");
      break;
    default:
      System.out.println("Invalid selection");
      break// This break is not really necessary
    }
  }
}

//**********************************************************
//**********************************************************
//Program: Keyin
//Reference: Session 20
//Topics:
// 1. Using the read() method of the ImputStream class
//    in the java.io package
// 2. Developing a class for performing basic console
//    input of character and numeric types
//**********************************************************
//**********************************************************

class Keyin {

  //*******************************
  //   support methods
  //*******************************
  //Method to display the user's prompt string
  public static void printPrompt(String prompt) {
    System.out.print(prompt + " ");
    System.out.flush();
  }

  //Method to make sure no data is available in the
  //input stream
  public static void inputFlush() {
    int dummy;
    int bAvail;

    try {
      while ((System.in.available()) != 0)
        dummy = System.in.read();
    catch (java.io.IOException e) {
      System.out.println("Input error");
    }
  }

  //********************************
  //  data input methods for
  //string, int, char, and double
  //********************************
  public static String inString(String prompt) {
    inputFlush();
    printPrompt(prompt);
    return inString();
  }

  public static String inString() {
    int aChar;
    String s = "";
    boolean finished = false;

    while (!finished) {
      try {
        aChar = System.in.read();
        if (aChar < || (charaChar == '\n')
          finished = true;
        else if ((charaChar != '\r')
          s = s + (charaChar; // Enter into string
      }

      catch (java.io.IOException e) {
        System.out.println("Input error");
        finished = true;
      }
    }
    return s;
  }

  public static int inInt(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Integer.valueOf(inString().trim()).intValue();
      }

      catch (NumberFormatException e) {
        System.out.println("Invalid input. Not an integer");
      }
    }
  }

  public static char inChar(String prompt) {
    int aChar = 0;

    inputFlush();
    printPrompt(prompt);

    try {
      aChar = System.in.read();
    }

    catch (java.io.IOException e) {
      System.out.println("Input error");
    }
    inputFlush();
    return (charaChar;
  }

  public static double inDouble(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Double.valueOf(inString().trim()).doubleValue();
      }

      catch (NumberFormatException e) {
        System.out
            .println("Invalid input. Not a floating point number");
      }
    }
  }
}
           
         
    
  
Related examples in the same category
1.Turn System.out into a PrintWriterTurn System.out into a PrintWriter
2.Output to standard output and standard error; and input from standard input.Output to standard output and standard error; and input from standard input.
3.VarArgsDemo - show 1.5 variable argumentsVarArgsDemo - show 1.5 variable arguments
4.How to read from standard inputHow to read from standard input
5.How to deal with the console parametersHow to deal with the console parameters
6.Read input from consoleRead input from console
7.Command line input
8.Read an int from Standard Input
9.Read an int from Standard Input, using 1.5
10.SimpleCalc -- simple calculator using 1.5 java.util.Scanner
11.Show use of Argv to get an integer value from command lineShow use of Argv to get an integer value from command line
12.Java program to calculate the area of a circleJava program to calculate the area of a circle
13.Utility class for printing aligned columns of text
14.Processing the command line
15.A representation of the command line arguments passed to a Java class' main(String[]) method
16.Helper class for storing long command lines inside temporary file.
17.CommandLine Parser
18.This parses command line arguments optimized for ease of programmer use.
19.Console read and write Utils
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.