Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Help with a "switch" statement

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with a "switch" statement

    Hello,

    I'm relatively new to Java (less than a week into it), but I'm needing helping displaying a method using a switch statement.

    Per the code below, how do I get the program to show the menu, then once the user selects the option (only the "1" at this point), how do I get the code to output the "showIf" method?

    Please let me know if I need to clarify in any way. Any help would be greatly appreciated!! Thank you.



    import java.util.Scanner;
     
    public class Help {
    	private Scanner input = new Scanner(System.in);
     
    	public int showMenu() {
                    System.out.println("Help on:");
                    System.out.println("  1. if");
    		System.out.println("  0. Exit Program");
    		System.out.println("");
    		System.out.println("enter the option:");
    		return input.nextInt();
    	}
     
    	public void showIf() {
                    System.out.println("if:");
                    System.out.println("if(condition) {");
                    System.out.println("\tstatement(s);");
                    System.out.println("}");
                    System.out.println("The if statement executes one or more statements");
                    System.out.println("only if the condition evaluates to true");
    	}
     
    	public static void main(String[] args) {
    		Help needhelp = new Help();
     
    		do{
    			int option = needhelp.showMenu();
    			s result = 0;
     
    			switch (option) {
    				case 0: System.out.println("Bye bye...");
    					System.exit(0);
    					break;
    				case 1: result = needhelp.showIf();
    					break;		
     
    				default:
    					System.out.println("invalid option");
    					break;
    				}
     
    			System.out.println(result);				
     
    		} while (true);
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2014
    Posts
    23
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with a "switch" statement

    what error or exception u are getting??
    please let us know..

  3. #3
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Red face Re: Help with a "switch" statement

    Hi,

    case 1: result = needhelp.showIf();
             break;

    " showIf()" method returns nothing then why you assigned to variable "result".

    Explain clearly what problem u r facing in the above program. What is your expectation. Happy to help u.

    thanks,

  4. #4
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a "switch" statement

    Hello,

    Basically, I'd like the code to display the menu (showMenu method). From there, expectation is for the user to either choose an option from said menu. As of right now, I only have one option, "1".

    When option "1" is selected by a user, using the switch statement, the methods should display the content created within the "showIf" method, then prompt the user to hit the space-bar to re-display the menu.

    Essentially, I'm going to be building a Help menu that defines and provides an example of specific conditions. As of right now, I'm only trying to get it to work with one condition help option, "if".



    @Praful - at this point, I'm getting lots of errors, as I'm sure I'm screwing up in different areas. to @Ganeprog's point around my assigning the "showif()" method to a variable, it's returning nothing. I figured that was wrong, but I do not know how to correct.

    Like I said, I'm very knew to this and would appreciate any help you could provide.

    Looking forward to hearing back! Take care.

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Help with a "switch" statement

    In the code "s result = 0;" s does not signify any data type. Please change it.
    Hope you have imported all the java packages.
    Are you using an IDE like eclipse or netbeans, or just writing code in a notepad?
    Thanks and regards,
    Sambit Swain

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a "switch" statement

    Hi Sambit,

    Do I really need the variable "result" in this case?

    I'm looking to accept user input using the Java Scanner. Based on said input, in this case it's "0" to terminate the loop or "1" to output the lines within the showIf() method. I'm not quite sure how to do it.

    I am using Notepad++, and am positive I'm importing all required java packages.

    Thanks for your help!

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,116
    My Mood
    Mellow
    Thanks
    244
    Thanked 755 Times in 741 Posts

    Default Re: Help with a "switch" statement

    @skakels, Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

  8. #8
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Help with a "switch" statement

    You are printing the result value toward the end of the program. So, i didnt asked anything about this.
    But i feel, result variable wont be required. You can remove that, and program will still work as expected.
    Thanks and regards,
    Sambit Swain

  9. The Following User Says Thank You to Sambit For This Useful Post:

    GregBrannon (January 24th, 2014)

  10. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a "switch" statement

    @Sambit - Thanks for the feedback. I removed the variable (updated code below).

    That said, it compiled and executed!!! w00t!

    I'm not going to have time to revist until tonight, so I MIGHT have a follow-up question or two. You definitely helped me past one, LARGE hurdle.

    Thank you!



    import java.util.Scanner;
     
    public class Help {
    	private Scanner input = new Scanner(System.in);
     
    	public int showMenu() {
            System.out.println("Help on:");
            System.out.println("  1. if");
    		System.out.println("  0. Exit Program");
    		System.out.println("");
    		System.out.println("enter the option:");
    		return input.nextInt();
    	}
     
    	public void showIf() {
            System.out.println("if:");
            System.out.println("if(condition) {");
            System.out.println("\tstatement(s);");
            System.out.println("}");
            System.out.println("The if statement executes one or more statements");
            System.out.println("only if the condition evaluates to true");
    	}
     
    	public static void main(String[] args) {
    		Help needhelp = new Help();
     
    		do{
    			int option = needhelp.showMenu();
     
    			switch (option) {
    				case 0: System.out.println("Bye bye...");
    					System.exit(0);
    					break;
    				case 1: needhelp.showIf();
    					break;		
     
    				default:
    					System.out.println("invalid option");
    					break;
    				}
     
    			System.out.println(needhelp);				
     
    		} while (true);
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 11:30 AM
  2. Trying to get an "if" statement to work in a "for loop".
    By JAKATAK in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 2nd, 2013, 12:16 AM
  3. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 08:15 AM
  4. using if statement on string System.getProperty("os.name") not working
    By sauyon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 01:09 PM
  5. Convert from 'C' to "java" (switch)
    By chronoz13 in forum Loops & Control Statements
    Replies: 7
    Last Post: October 6th, 2009, 09:20 PM