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: Whats wrong with my code

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Whats wrong with my code

    Hello. I am new to java learning online. Please tell me whats wrong with my code. When i input A, B, C or D it gives no result no error just blank


    package Loops;

    import java.util.Scanner;

    public class city_if_loop {

    private static Scanner city;

    public static void main(String[] args) {

    city = new Scanner(System.in);

    System.out.println("Enter Your City");

    String c= city.nextLine();

    if (c=="A"){System.out.println("Your zipcode is A");}
    else if (c=="B"){System.out.println("Your zipcode is B");}
    else if (c=="C"){System.out.println("Your zipcode is C");}
    else if (c=="D"){System.out.println("Your zipcode is D");}
    }
    }


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

    Default Re: Whats wrong with my code

    When posting code, please use the highlight tags to preserve formatting.

    Your problem is you're comparing String values with == instead of the equals() method. Do a search on google or on these forums for a better explanation of why == won't work.
    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. The Following User Says Thank You to KevinWorkman For This Useful Post:

    ansiaries (March 27th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Whats wrong with my code

    thanks i got it

Similar Threads

  1. Whats wrong in my code?
    By kalaicse30@gmail.com in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2013, 12:44 AM
  2. Can anyone see whats wrong with this code
    By javapol in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 27th, 2013, 03:59 PM
  3. Whats wrong with this code????
    By westberge in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 10th, 2012, 07:49 PM
  4. Can someone see whats wrong with my code, please?
    By DJBENZ10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 11th, 2012, 08:54 PM
  5. Whats Wrong with this code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 9th, 2011, 10:59 PM

Tags for this Thread