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: Logical operators don't seem to work in a very basic class

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Logical operators don't seem to work in a very basic class

    I'm starting out in Java

    I produced the following class to test switch and conditional operators:

    [highlight = Java]
    import java.util.Scanner;


    public class TestingSwitchThenConditionals {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter password ");

    String password = input.nextLine();


    switch (password)
    {
    case "Paris" : System.out.println("Correct");
    System.out.println("you're in");
    break;

    case "London" : System.out.println("Almost");
    break;

    default: System.out.println("No entry!!!");

    }

    for (double count = 1;count <=5; count++)
    {
    System.out.printf("%25s to the power of %.0f = %.0f\n", count, count , Math.pow(count, count));
    if ((count>=2) && (password == "London") )
    {
    System.out.printf("%s\n", password);
    }
    }
    }
    [/highlight]

    When I use the && conditional it doesn't seem to 'work' in the sense that if I enter the password as London the contents of the if statement are ignored even when count is >=2

    When I use the ^ conditional & enter London as the password I was expecting the if statement to execute only if count = 1 i.e only 1 of the operands is true & the other false (I'm using Java, How to Program 9th ed, p 226 for definition of ^ which tells me that BOTH operands are evaluated)

    Any help would be greatly appreciated


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Logical operators don't seem to work in a very basic class

    Please see the announcements page for the use of code tags.

    After that have a look at the java api for the string class and see what it has to say about comparing string objects.

Similar Threads

  1. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  2. This Code worked on a separate class but does not work on another class
    By titowinky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 25th, 2012, 08:48 AM
  3. Help with logical operators
    By BiaxialPainter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:35 AM
  4. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 06:26 PM
  5. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 06:52 AM