Welcome to Dream.In.Code
Become a Java Expert!

Join 149,708 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,273 people online right now. Registration is fast and FREE... Join Now!




Help with Old School Menu

2 Pages V  1 2 >  
Reply to this topicStart new topic

Help with Old School Menu, Help with Old School Menu

codeninja
16 Feb, 2007 - 08:15 AM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions
Hi.

I am working with some studnets on various projects which they have been asked to complete to earn a certificate in high school level computer science. The structure of their program and curriculum are highly regulated by a 3rd party organization....like the SAT tests are.

During my first year in this program with them I followed the rules I was given too closely. They were not supposed to have to learn or know any code relating to GUI's. Their program is actually graded on paper! Although I must certify it before it leaves the school.

Anyway they need to make a menu that fires their main classes. The menu needs to look like this:

[terminal/console output]
-----------Menu-------------
1. option 1 (means press 1 on keyboard)
2. option 2
3. option 5
4. kill program.


The programs are all going ok -- simple database things based on xml.

But the only experience I have with Java is limited and limited to Guis. Because I did not teach the GUI -- i messed up thinking it would be easy to find and study old-school menu examples.

They get no points for any gui or graphic elements.

Anyway if anyone can direct me to a tutorial --or if you have a simple file I can start with I would appreciate it.

I should have lots of files to contribute as well. I will keep posting around.

CN










User is offlineProfile CardPM
+Quote Post

keems21
RE: Help With Old School Menu
16 Feb, 2007 - 11:44 AM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
I've read through your post a bunch of times, but I'm not sure that I really understand your problem.

Are you having trouble teaching how to build guis, or are you having trouble with the idea of creating a program where a user controls the flow of the program depending on their input?


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Help With Old School Menu
16 Feb, 2007 - 02:01 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
If I am understanding your question correctly, you want to know how to create an app that only uses a console interface instead of a GUI.

You can find everything you need at the following tutorials.
http://www.cs.wisc.edu/~cs302/io/JavaIO.html
http://java.sun.com/docs/books/tutorial/es...l/io/index.html
User is offlineProfile CardPM
+Quote Post

Ryan Smith
RE: Help With Old School Menu
16 Feb, 2007 - 02:02 PM
Post #4

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 28


My Contributions
A simple way to get Standard Input from a keyboard is:

CODE
try {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String string = "";
        while (string != null) {
            System.out.print("> prompt ");
            string = in.readLine();
            doWhatever(string);
        }
    } catch (IOException e) {
    }


Run this simple class and see if it meets any of what you need to do:

CODE

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String string = "";
            System.out.println("Say something to me!");
        while (string != null) {
            System.out.print("> prompt ");
            string = in.readLine();
            doWhatever(string);
            }
        } catch (IOException e) {
        }
    }
    private static void doWhatever(String str) {
        if(str.equalsIgnoreCase("exit") || str.equalsIgnoreCase("quit") || str == null)
            System.exit(0);
        System.out.println("You say " + str + "?");
    }
}


You should be able to read the options from the doWhatever() method.

Hope that helps;


User is offlineProfile CardPM
+Quote Post

keems21
RE: Help With Old School Menu
16 Feb, 2007 - 06:44 PM
Post #5

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
If that's really what you mean, taking input from the console, I've got a very easy solution for you.

Use the Scanner class.

CODE

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    String read = input.nextLine();
    Int num = input.nextInt();
   }
}


The Scanner class is very easy to use, and it prevents you from needing to understand all of the wrapping that goes on with the IO package.

Just create a new Scanner object like I did with the constructor above. Then, everytime you call input.nextLine() or input.nextInt(), the console will wait for you to input some data, which is immediately stored in the variable preceeding the call.

Here's the api in case you need to look something up:
http://java.sun.com/j2se/1.5.0/docs/api/ja...il/Scanner.html

Enjoy.
User is offlineProfile CardPM
+Quote Post

Ryan Smith
RE: Help With Old School Menu
16 Feb, 2007 - 10:09 PM
Post #6

New D.I.C Head
*

Joined: 14 Feb, 2007
Posts: 28


My Contributions
Ha wow, I've never even used that...
User is offlineProfile CardPM
+Quote Post

codeninja
RE: Help With Old School Menu
17 Feb, 2007 - 10:31 AM
Post #7

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions
Wow! So much good feedback. I will let my kids try it out and I as well and then post our results!

Tony D aka codeninja the domination

QUOTE(keems21 @ 16 Feb, 2007 - 07:44 PM) *

If that's really what you mean, taking input from the console, I've got a very easy solution for you.

Use the Scanner class.

CODE

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    String read = input.nextLine();
    Int num = input.nextInt();
   }
}


The Scanner class is very easy to use, and it prevents you from needing to understand all of the wrapping that goes on with the IO package.

Just create a new Scanner object like I did with the constructor above. Then, everytime you call input.nextLine() or input.nextInt(), the console will wait for you to input some data, which is immediately stored in the variable preceeding the call.

Here's the api in case you need to look something up:
http://java.sun.com/j2se/1.5.0/docs/api/ja...il/Scanner.html

Enjoy.


User is offlineProfile CardPM
+Quote Post

codeninja
RE: Help With Old School Menu
17 Feb, 2007 - 07:50 PM
Post #8

New D.I.C Head
*

Joined: 16 Feb, 2007
Posts: 44


My Contributions
Hi-

I looked over everything and most of it I already knew how to do. The scanner class was new.

What I am missing- and again maybe it is just too old school ---

When the program starts I would like a list of options to appear on the screen.

-- I can do that.


Then when a user presses the 1 key on the keyboard I would like to call a method.

--I can call the method I just need to know how to assign the number 1 on the keyboard so it works the same as clicking the mouse on a Button.

--I am going to try this with the scanner class by storing the info in a variable but I thought each key had a number that could be accessed and then I am assuming there is an api available to help work with it.


Ideas?


I am having my students do the tutorials today as they are well written and should reinforce alot of needed skills.

Thanks.

Codeninja


QUOTE(codeninja @ 17 Feb, 2007 - 11:31 AM) *

Wow! So much good feedback. I will let my kids try it out and I as well and then post our results!

Tony D aka codeninja the domination

QUOTE(keems21 @ 16 Feb, 2007 - 07:44 PM) *

If that's really what you mean, taking input from the console, I've got a very easy solution for you.

Use the Scanner class.

CODE

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    String read = input.nextLine();
    Int num = input.nextInt();
   }
}


The Scanner class is very easy to use, and it prevents you from needing to understand all of the wrapping that goes on with the IO package.

Just create a new Scanner object like I did with the constructor above. Then, everytime you call input.nextLine() or input.nextInt(), the console will wait for you to input some data, which is immediately stored in the variable preceeding the call.

Here's the api in case you need to look something up:
http://java.sun.com/j2se/1.5.0/docs/api/ja...il/Scanner.html

Enjoy.



User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Help With Old School Menu
17 Feb, 2007 - 08:02 PM
Post #9

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
I'm not sure I understand exactly what you'd like...simply to display your list of choices, then take input from the user, and perform a set of actions based on that input. Is that correct?

CODE

import java.util.Scanner.*;

public class ReadConsole
{
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Menu System - Please enter your choice");
    System.out.println("Option 1");
    System.out.println("Option 2");
    System.out.println("Option 3");
    Int num = input.nextInt();
    if(num==1)
       functionforoption1();
    else if(num==2)
       functionforoption2();
    else if(num==3)
       functionforoption3();
    else
       //default case
   }
}

Of course, there are a million ways to do it - case statements might be more beneficial here.
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Help With Old School Menu
17 Feb, 2007 - 10:16 PM
Post #10

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
It sounds like what you're asking is if you can set up a KeyListener and catch events for console inputs, and I think (think) the answer is no. It would be kind of cool, but I think you'll have to settle for having to hit enter, validate the input, and then run your method based on the input. If you were allowed to use Swing, you'd be all set.

This post has been edited by alcdotcom: 17 Feb, 2007 - 10:19 PM
User is offlineProfile CardPM
+Quote Post

keems21
RE: Help With Old School Menu
17 Feb, 2007 - 10:28 PM
Post #11

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
QUOTE(codeninja @ 17 Feb, 2007 - 08:50 PM) *

Hi-

I looked over everything and most of it I already knew how to do. The scanner class was new.

What I am missing- and again maybe it is just too old school ---

When the program starts I would like a list of options to appear on the screen.

-- I can do that.


Then when a user presses the 1 key on the keyboard I would like to call a method.

--I can call the method I just need to know how to assign the number 1 on the keyboard so it works the same as clicking the mouse on a Button.

--I am going to try this with the scanner class by storing the info in a variable but I thought each key had a number that could be accessed and then I am assuming there is an api available to help work with it.


Ideas?


I am having my students do the tutorials today as they are well written and should reinforce alot of needed skills.

Thanks.

Codeninja


OK, now I see what you want. The only problem is that I don't know of any immediate solution. I can't say that I've ever tried to do something like this.

Here's my only thought as of now, create your own UI to replace the terminal/console, then add a key listener (or a key bind) to it.
I'll try and throw something together for you as an example.


And here's that api you were talking about with all of the key variable assignments.
http://java.sun.com/j2se/1.5.0/docs/api/ja...t/KeyEvent.html
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Help With Old School Menu
18 Feb, 2007 - 05:56 PM
Post #12

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(keems21 @ 18 Feb, 2007 - 12:28 AM) *

OK, now I see what you want. The only problem is that I don't know of any immediate solution. I can't say that I've ever tried to do something like this.

Here's my only thought as of now, create your own UI to replace the terminal/console, then add a key listener (or a key bind) to it.
I'll try and throw something together for you as an example.


And here's that api you were talking about with all of the key variable assignments.
http://java.sun.com/j2se/1.5.0/docs/api/ja...t/KeyEvent.html

The Swing console emulation occurred to me, but he said that they get no credit for using Swing. Plus, it kinda seemed like overkill for their purposes. But it would be interesting to see your implementation. That's the cool thing about programming: everyone will have a different way of doing something.

I still feel like there should be some way to set up a KeyListener and get KeyEvents in a console because I've seen C apps that can do it. But I just don't know how at the moment. I'll do one more quick Google and see if I find anything.

User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:23AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month