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

Thread: logical errors

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default logical errors

    Hi all, I have 2 segments of two different codes that are having problems and I can't seem to find what I am doing wrong. The first code has a logical error. Any ideas?

    First code :
    Scanner console = new Scanner(system.in);
    System.out.print("Type a number: ");
    int number = console.nextInt();
     
    if (number % 2 == 0) {
         if (number % 3 == 0) {
              System.out.println("Divisible by 6.");
         } else {
              System.out.println("Odd.");
         }
    }


    Second code :
    Scanner console = new Scanner(System.in);
    System.out.print("What is your favorite color?");
    String name = console.next();
     
    if (name == "blue") {
         System.out.println("Mine, too!");


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: logical errors

    What seems to be the problem here? Post error messages, questions, expected output and the output you have now... etc etc

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,859
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: logical errors

    Don't use the equals operator, ==, to compare Strings. use the equals() method.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: logical errors

    if (name equals("blue")) {
         System.out.println("Mine, too!");

    like this?

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: logical errors

    It should be 'name.equals' instead of 'name equals', but yes.

Similar Threads

  1. Logical
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2013, 01:08 AM
  2. Logical Problem
    By dougie1809 in forum AWT / Java Swing
    Replies: 18
    Last Post: February 21st, 2013, 01:17 PM
  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. I have a logical error
    By n00b in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2011, 02:52 PM
  5. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 05:26 PM