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 3 of 3

Thread: If else statement not working.

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If else statement not working.

    I'm trying to make a test on java. So far I'm on Question 1 and I'm having this issue. The if else statement won't allow me to add to the score.

    class ExamEngine {
        static CinReader Cin= new CinReader();
        static int Score=0;
        public static void main(String[] args) {
            System.out.println("Type in your name.");
            String name=Cin.readString();
            System.out.println("Hello " +name+".");
            System.out.println("Welcome to your Midterm.");
            System.out.println("Type in the correct word for the definition below.");
            System.out.println("Spelling counts, so make sure you spell the answer correctly.");
            Question1();
        }
            public static void Question1()
        {
            System.out.println("          ");
            System.out.println("1. The operator (=) used to store the value of an expression into a variable, for instance"); 
            System.out.println("variable = expression; The right-hand-side is completely evaluated before the assignment is made.");
            System.out.println(" An assignment may, itself, be used as part of an expression.");
            System.out.println("The following assignment statement stores zero into both variables. x = y = 0;");
            String Q1=Cin.readString();
            if(Q1.equals("assignment operator"))
            System.out.println("Good job!");
            Score++;
            else{System.out.println("Incorrect.");
            }
        }
    }

    If I remove the "Score++;" bit, everything is fine. But I need to add to the score when they answer a question right. However, with "Score++;" there, I get this issue: 'else' without 'if'.

    Note that this cannot be multiple choice, so that's why I'm using if-else, because I don't think a case system would work here.

    Is there a way I can make this work while adding to the score?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: If else statement not working.

    You should always use curly brackets{} after if statements, even if they only contain one line.

    Since your if statement does not have curly brackets, it is only applied to the very next line, which is the print statement.

    In other words, add curly brackets and put whatever you want to happen as a result of the if statement inside of them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: If else statement not working.

    Also it is better NOT to hide {s by putting code after them on the same line.
      else{System.out.println("Incorrect.");   // { hidden
    vs
      else {               // { easier to see
         System.out.println("Incorrect.");
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. If/else statement working incorrectly...
    By jcfor3ver in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2013, 11:26 PM
  2. if else if elese if.... statement not working
    By ahans in forum Loops & Control Statements
    Replies: 4
    Last Post: October 25th, 2013, 01:54 PM
  3. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  4. switch statement not working for JFrame Calculator
    By kaddyshack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:35 AM
  5. if else statement not working properly
    By tina G in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 29th, 2010, 08:04 AM