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: Choice condition IF based on value

  1. #1
    Member
    Join Date
    Jun 2017
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Choice condition IF based on value

    How come I can't get into the else? where did I go wrong? I would like to do this test based on this structure that I have outlined.
    I don't want to use switches and other things.
    Only with this structure.

    public static void main(String[] args) {
    		String a = "10";
    		String b = "6";
    		boolean t = false;
    		boolean v = false;
     
    		for (int i = 0; i < a.length(); i++) {
    			for (int j = 0; j < b.length(); j++) {
    				if (a.equals(b)) {
    					t = true;
    					System.out.println("sono uguali");
    				}
    			}
    			if (t == false) {
    				if(a != b) {
    				System.out.println("non sono uguali: " + a + "!=" + b);
    				}
    				else {
    					v= true;
    					if(a.equals("10")) {
    						System.out.println("Non inserire il numero 10");
    					}
    				}
     
    			} 
    		}
    	}

    suppose that a and b are objects, does this approach work the same?

    public static void main(String[] args) {
    		String a = new String();
    		String b = new String();
    		boolean t = false;
    		boolean v = false;
    		a = "10";
    		b = "7";
     
    		for (int i = 0; i < a.length(); i++) {
    			for (int j = 0; j < b.length(); j++) {
    				if (a.equals(b)) {
    					t = true;
    					System.out.println("sono uguali");
    				}
    			}
    			if (t == false) {
    				if (a != b) {
    					System.out.println("non sono uguali: " + a + "!=" + b);
    				} else {
    					if (a.equals("10")) {
    						v = true;
    						System.out.println("Should not be  10");
    					}
     
    				}
     
    			}
     
    		}
    	}
     
    }
    does not enter me

    if (a.equals("10")) {
    v = true;
    System.out.println("Should not be 10");
    }



    suppose there is get and set does it work the same?
    Last edited by glprobot; July 16th, 2019 at 04:52 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Choice condition IF based on value

    can't get into the else
    You won't enter the else unless the if is false.
    if (a != b) {
    Always use the equals method when comparing objects like Strings.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multiple choice help
    By asteroidvirus in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2018, 10:01 PM
  2. LSP: weaken pre-condition and strengthen post-condition
    By Capital in forum Object Oriented Programming
    Replies: 1
    Last Post: July 13th, 2014, 12:46 PM
  3. need help with simple multiple choice
    By levitron18 in forum Other Programming Languages
    Replies: 2
    Last Post: June 17th, 2014, 10:43 AM
  4. Is scanner the right choice?
    By Badformat in forum Java Theory & Questions
    Replies: 1
    Last Post: January 20th, 2014, 03:02 PM
  5. Which interface gives more control on serialization of an object?
    By abhishekraok2003 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 16th, 2009, 10:17 AM