Main Menu (switch help)

T

Tuurbo46

Hi

Im currently experiencing some problems in getting the structure correct to
enable a user to select between menus.

I have chosen to create this menu in main and then allow all flow to other
classes from here. Is this considered the correct way of doing this?
Currently my version frows errors, saying there is to many brackets. I have
looked at it for a long time and i cannot see a problem. I have added this
code below

Hope somebody can point me in the right direction.

Tuurbo

############################################

public static void main(String args[]) {

public void mainMenu(void)
int tempMyoption;
do {
tempMenuScreen();
tempMyoption = readInOption();
switch (tempMyoption) {
case '1':
System.out.println("one is working");
break;
case '2':
Systme.out.println("two is woriking");
break;
}
}
}

public void tempMenuScreen(void)
{

System.out.println("");
System.out.println("\t ### MAIN MENU ###");
System.out.println("\t 1) Add new motorbike");
System.out.println("\t 2) Exit");

}
}
 
A

Andrew Thompson

Tuurbo46 said:
Im currently experiencing some problems in getting the structure correct to
enable a user to select between menus.

It would seem those problems are overshadowed by compilation
problems, but UI design and compilation problems should be
covered in separate threads.

I will only adress the compilation errors and one or two
related matters.
Currently my version frows errors,

Did you mean 'throws' errors?

If so, my reply is *rubbish*. For a Java program to throw an
Error while running (the only time an 'Error' can be 'thrown')
is a very different to javac report compilation errors. ...
..saying there is to many brackets.

....Or, to put that another way, please always copy/paste
the error output.
...I have
looked at it for a long time and i cannot see a problem. I have added this
code below

What you have added below is a snippet.
Posting an SSCCE of your problem is often much clearer.
<http://www.physci.org/codes/sscce.jsp>

It seems you have tried to define a method inside the
'main()' method (not allowed), but since it is a snippet,
it is hard to tell for sure what is happening.
Hope somebody can point me in the right direction.

A better group for those new to Java is
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH
 
B

Bjorn Abelli

Tuurbo46 said:
Im currently experiencing some problems in getting the structure correct
to enable a user to select between menus.

I have chosen to create this menu in main and
then allow all flow to other classes from here.
Is this considered the correct way of doing this?

Both yes and no.

the "main"-method contains the "main-flow" of an application, so when you're
dealing with procedural programming, it works.

As you're a beginner in Java, it could be a plausible start, until you're
ready to begin with the more intrinsic parts of object oriented programming,
where the "main-flow" in most cases is something else than just a sequential
flow.
Currently my version frows errors, saying there is to
many brackets. I have looked at it for a long time and
i cannot see a problem. I have added this code below

The error is more of *where* you have put the brackets...

- First, you *must* have a class to put the methods in.

- Second, you *can't* define a method for the same class within *another*
method.

- Third, "void" isn't legal as a parameter.

- Fourth, a "do"-loop must end with a "while"-condition.

- Fifth, for beginners a better newsgroup would be comp.lang.java.help.


With a better strategy of indentation, some of your errors would be easier
to spot.

Lets strip down your code to "only brackets"...

public static void main(String args[]) {

public void mainMenu()

do {
switch (tempMyoption) {
}
}
}

public void tempMenuScreen() {
}
}

Now it's obvious that you're missing a beginning bracket for the body of
"mainMenu".

You also see clearly that you've defined "mainMenu" and "tempMenuScreen"
*inside* the main-method, instead of defining them outside the main-method,
and just make calls to them from within the main-method.

Personally I find the "Allman style" more readable than the
"Kerrighan/Ritchie style", as I see the brackets lining up better that way.

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top