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: why is the program not entering the if statement?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why is the program not entering the if statement?

    package catalog;

    import java.util.*;

    public class Catalog {

    static String products[] = new String[3];
    static int answer;

    public static void main(String[] args) {

    System.out.println("------------------");
    System.out.println("Shopping Catalog");
    System.out.println("------------------");

    String[] pCode = new String[3];
    float pPrice[] = new float[3];
    int orderNum = 0;
    int quantity=0;

    Scanner s = new Scanner(System.in);
    System.out.println("------------------------------------------");
    System.out.println("condensed milk [M3487], $9.50 per can.");
    System.out.println("");
    System.out.println("Distilled Water [W3876], $3.00 a bottle.");
    System.out.println("");
    System.out.println("Pack Rice [R9983], $12.75 for 5lbs.");
    System.out.println("------------------------------------------");




    do{

    System.out.println("Please enter order number (0 to stop)");
    pCode[orderNum] = s.nextLine();

    if(pCode[orderNum] == "M3487"){
    System.out.println("condensed milk $9.50");
    System.out.println("Enter Quantity");
    quantity = s.nextInt();
    }//close if statement
    orderNum++;
    if(answer == 0){
    break;
    }//close if
    }while(true);//close while loop

    }//close main method

    }//close class


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: why is the program not entering the if statement?

    Please format your code for better readability.
    if(pCode[orderNum] == "M3487")

    use equals() method instead of == operator for comparing strings like this..

    if (pCode[orderNum].equals("M3487"))

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

    GregBrannon (May 5th, 2014)

  4. #3
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why is the program not entering the if statement?

    thank you

Similar Threads

  1. Is there anyway to use a Do While statement in this program?
    By Kyllian in forum Loops & Control Statements
    Replies: 6
    Last Post: February 26th, 2014, 09:55 AM
  2. Entering and running a Program
    By Bijaysadhok in forum Java Theory & Questions
    Replies: 1
    Last Post: February 23rd, 2012, 06:15 AM
  3. Help with If- Else statement program please
    By Tctwins in forum Loops & Control Statements
    Replies: 6
    Last Post: February 20th, 2012, 03:58 PM
  4. Entering objects from a file into an array
    By lookalive34 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2011, 08:33 PM
  5. Not entering while loop
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 01:15 PM

Tags for this Thread