I need to complete this project by next Monday.
How do I do a menu selection
EXAMPLE:
Press 1 to enter the model number.
Press 2 to delete a record.
Press 3 to exit the program.
I'm doing this project on NetBean using Java.
Can someone please teach me?
Thanks!!!!

Recommended Answers

All 10 Replies

My java is a little rusty, but this should give you the basic idea (I'm doing this from memory, so the code might not compile just right, but you should be able to fix it from there):

BufferedReader reader = new BufferedReader(new InputStreamReader());
int choice = int(reader.readLine().trim());

if (choice == 1) {
    // call the function corresponding to choice 1
} elseif {
    // do something else
} // etc..

My java is a little rusty, but this should give you the basic idea (I'm doing this from memory, so the code might not compile just right, but you should be able to fix it from there):

BufferedReader reader = new BufferedReader(new InputStreamReader());
int choice = int(reader.readLine().trim());

if (choice == 1) {
    // call the function corresponding to choice 1
} elseif {
    // do something else
} // etc..

I aldready had a BufferedReader, do i need to create a new one?

No I think. You can use the existing one.

No I think. You can use the existing one.

I think using that code but there is an error

Ok. What kind of menu selection are you using? You can use the below code

import java.io.*;
public test{
public static void main(String args[]) throws IOException
{
BufferedReader bfr= new 
BufferedReader(new InputStreamReader(System.in));
System.out.println("MENU");
System.out.println("enter a number");
int n=Integer.parseInt(bfr.readLine());
if(n==1)
{
//condition  
System.out.println("number is one");
}
else if(n==2)
{
//condition  
System.out.println("number is two");
}
else if((n>2)&&(n<1))
{
//condition  
System.out.println("sorry wrong number");
}
}
}

I adlready have a public class and public static void main. Do i need to create a new one?

No. Actually you don't need to. You can use the lines between public main class that is from buffered reader and use it in your class. If you need any help you can reply me to my mail. SNIP

Please mark it as solved ,if it is so.

I am completely confused about what this thread is, and what everybody has posted. It seems to be a jumble of different unrelated threads all thrown into one.

@fairy1992224 rule of the forum We only give homework help to those who show effort

and for the rest, you should reconsider it how much help is good with given interest from original poster. You maybe helping another lazy programmer after whom you could be fixing errors in real life

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.