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

Thread: if else statement not working properly

  1. #1
    Junior Member tina G's Avatar
    Join Date
    Mar 2010
    Location
    philippines
    Posts
    7
    Thanks
    1
    Thanked 1 Time in 1 Post

    Unhappy if else statement not working properly

    help me in this code?
    my if else statement was not working properly?
    it always show valid?
    even the user put an invalid expression?
    what was i need to do? to solve it
    thanks in advance..
    import java.io.IOException;
    import java.io.*;
    import java.util.Arrays;
     
    public class datastruct {
     
     
      public static void main(String[] args) throws IOException {
     
     
      	BufferedReader A = new BufferedReader (new InputStreamReader(System.in));
      	String mathex;
      	String output = "";  
      	String out;
      	System.out.println("Enter a Mathematical Expresion:" );
    	mathex = A.readLine();
     
     
        int countOperators = 0;
        int countOperands = 0;
     
     
      for(int i = 0; i < mathex.length(); i++) {
         char ch = mathex.charAt(i);
         out = A.readLine();
     
     
         	 if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
           countOperators++;
     
     
         	if ( (out.equals("num"))){
     
    		System.out.println("invalid");
    	}
    	else if ( (out.equals("(")) || (out.equals("ch"))){	
     
    		System.out.println("valid");
    	}
    	else if ( (out.equals(")")) || (out.equals("ch"))){	
     
    		System.out.println("invalid");
    	}
    	else if ((out.equals ("(")) || (out.equals  ("ch"))){
     
    		System.out.println("valid");
    	}
    	else {
     
    		System.out.println("valid");
    	}
     
     
     
        System.out.println("INFIX is: " + mathex );
        System.out.println("POSTFIX is: " + mathex );
        System.exit(0);
     
         } 
         else
         countOperands++;
         }
     
        if((countOperators + 1) != countOperands || mathex.length() == 1){
          System.out.println("INVALID MATHEMATICAL EXPRESSION!!!!!"+ '\n');
        }
     
      	}
      }


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: if else statement not working properly

    You shoudl try to make it more clear what you are trying to achieve with your programme and where exactly the problem is. I mean you have 3 If statements.

    	else if ( (out.equals("(")) || (out.equals("ch"))){	
     
    		System.out.println("valid");
    	}
    	else if ( (out.equals(")")) || (out.equals("ch"))){	
     
    		System.out.println("invalid");
    	}
    	else if ((out.equals ("(")) || (out.equals  ("ch"))){
     
    		System.out.println("valid");
    	}

    Would I be right in saying that the first and third ones there are the same? In which case drop the third one.

    Now if out is ch this will trigger the first condition so it will never go to the second one, I would assume that it can't be valid and invalid at the same time so what is it?

    A bit of debugging you could do would be to put some distinctive print statements inside the Ifs to see which ones are being triggered(or use IDE debugging).

  3. The Following User Says Thank You to Faz For This Useful Post:

    tina G (March 31st, 2010)

Similar Threads

  1. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  2. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM
  3. Buffered Reader is not reading my file properly... HELP!
    By mannyT in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: November 8th, 2009, 08:14 PM
  4. Certain Chinese Characters not displayed properly.
    By kerwintang in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 20th, 2009, 08:23 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM