Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Norm

Thread: Making an Interactive Code

  1. #1
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Question Making an Interactive Code

    Hey Fellow Java Coders,

    I am new Java coder and happy to be here. I hope I can make new friends with you fellow Java Coders.
    Right now, I seem to be at a stuck. I am making a program that asks the question 'Do you like me', and depending on your answer (Yes or No),
    the program will iniciate a certain respond. I am doing it with the switch statement, not with if else statements, because I want to practice using switch
    statements right now.
    The problem is that no matter what response you put in, the program will always go to the default statement.
    Please help me fix this code, and in return, I will make you a Java!

    Thank you

    - SimeonTG

    Java Code:
    // Name of code is SwitchStatements.java
    public class SwitchStatements {
    
    	public static void main(String[] args)
    			 throws java.io.IOException {
    
    		// Asking for the feedback 
    		char Yay = 'a';
    		char Boo = 'b';
    		System.out.println("Do you like me?");
    		char User_Answer = (char) System.in.read();
    		
    		// Now for the switch statements 
    		switch(User_Answer) {
    		
                    // If user inputs the letter a, then say this 
    		case 0: User_Answer = Yay;
    		System.out.println("Thank you!");
    		System.out.println("You have typed " + User_Answer);
    		break;
                    // If user inputs the letter b, then say this 
    		case 1: User_Answer = Boo;
    		System.out.println("You suck!");
    		System.out.println("You have typed " + User_Answer);
    		break;
    		
                    /* 
                    The default statement; it will always go to this statement
                     no matter what the user types
                    */
    		default: 
    			System.out.println("What are you talking about?");
    			System.out.println("You have typed " + User_Answer);
    			break;
    		}
    
    }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Making an Interactive Code

    The syntax of the switch statement isn't what you seem to think it is. Go through The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    What your code at line 17 does is, if User_Answer is 0 then assign the value of Yay to User_Answer. That's not what you want to do.

    Tangentially, variable names should start with a lowercase letter and use camelCase, not underscores, to distinguish words. So, userAnswer, not User_Answer.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Oct 2015
    Posts
    8
    Rep Power
    0

    Default Re: Making an Interactive Code

    The use of switch statements has so many limitations that always discourages me from using it. I never use it. For example, my version of java can not accept strings in switch statements. I will advise you to just stick with "if...else" statements, that is my personal preference though! Concerning your code, may I humbly point out to you, that what you are actually testing in your two case (case 0 and case 1) is the value for User_Answer. In other words, except the user enters either '0' or '1', the switch case code blocks will never be reached. Go and read more on switch case statements or just stick to the ever "powerful" if...else statements.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Making an Interactive Code

    Quote Originally Posted by Prince_Jobless View Post
    The use of switch statements has so many limitations
    Hmm, that implies quite a few. Name five that other implementations in other languages support.

    my version of java can not accept strings in switch statements.
    So it's your version that has limitations, not Java.

    It also means you are using a version <= 6. Java 7 was released 4 years ago
    so if this is a management decision you may want to raise the issue (and you are missing out on
    some great stuff in Java 8 - released a year ago).

    I recommend you at least bring yourself up to speed on the latest versions of Java. Then you
    will be ready when you are expected to know it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Making an Interactive Code

    Quote Originally Posted by Prince_Jobless View Post
    what you are actually testing in your two case (case 0 and case 1) is the value for User_Answer. In other words, except the user enters either '0' or '1', the switch case code blocks will never be reached.
    They won't be reached if the user enters '0' of '1'.

    In Java, char is a numeric type. The char '0' has a value of 48.

    db
    Prince_Jobless likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Making an Interactive Code

    Another advantage of the switch statement is it guarantees that only one variable's value is used to make the decision. With if/else if statements, each if condition is independent of the others making it necessary to visually verify the code's correctness vs having the compiler handle it.
    Prince_Jobless likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Making an Interactive Code

    Quote Originally Posted by Prince_Jobless View Post
    The use of switch statements has so many limitations that always discourages me from using it. I never use it. For example, my version of java can not accept strings in switch statements. I will advise you to just stick with "if...else" statements, that is my personal preference though! Concerning your code, may I humbly point out to you, that what you are actually testing in your two case (case 0 and case 1) is the value for User_Answer. In other words, except the user enters either '0' or '1', the switch case code blocks will never be reached. Go and read more on switch case statements or just stick to the ever "powerful" if...else statements.
    I tried this using the if statement - it works. But if I were to try using the switch statement, will it still work?

  8. #8
    SimeonTG's Avatar
    SimeonTG is offline SimeonTG
    Join Date
    Oct 2015
    Posts
    21
    Rep Power
    0

    Default Re: Making an Interactive Code

    So, what should the code look like if the switch statement would work?

  9. #9
    Join Date
    Oct 2015
    Posts
    8
    Rep Power
    0

    Default Re: Making an Interactive Code

    Is there really a Java 8? Wow. I thought 7 was the latest version. I code for fun, I am not a professional programmer. That might be one of the reasons why I am still in the dark about the latest versions. Thanks for the enlightenment. I just don't like the idea of always remembering to add breaks to my switch statements. I'd stick with if...else.

  10. #10
    Join Date
    Oct 2015
    Posts
    8
    Rep Power
    0

    Default Re: Making an Interactive Code

    Post the "If...else" version of your code here. I will help you convert it, using the switch statements.

Similar Threads

  1. Making this code more efficient...
    By andy_d in forum New To Java
    Replies: 2
    Last Post: 03-31-2014, 08:44 AM
  2. making a executble fro simple java code
    By NeO2 in forum New To Java
    Replies: 2
    Last Post: 02-16-2014, 07:39 PM
  3. Replies: 1
    Last Post: 04-03-2013, 08:28 PM
  4. Making java code simpler.
    By majeed14 in forum Java 2D
    Replies: 4
    Last Post: 01-29-2013, 02:33 AM
  5. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •