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: Array iteration Problem.. Please help

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

    Default Array iteration Problem.. Please help

    Item[]items = new Item[4];
    items[0]= new Item("n",2.2,4);
    items[0].setName("OJ");
    items[0].setCost(6.00);
    items[0].setQty(10);
     
    items[1]= new Item("n",2.2,4);
    items[1].setName("Milk");
    items[1].setCost(3.99);
    items[1].setQty(20);
     
    items[2]= new Item("n",2.2,4);
    items[2].setName("Snicker");
    items[2].setCost(0.79);
    items[2].setQty(75);
     
    items[3]= new Item("n",2.2,4);
    items[3].setName("Wheat Bread");
    items[3].setCost(2.49);
    items[3].setQty(5);
     
     
    items[0].getName();
    items[0].getCost();
    items[0].getQty();
     
     
    items[1].getCost();
    items[1].getName();
    items[1].getQty();
     
    items[2].getName();
    items[2].getCost();
    items[2].getQty();
     
    items[3].getName();
    items[3].getCost();
    items[3].getQty();	
     
    Scanner scan = new Scanner(System.in);
    String input = scan.next();
    for(int i=0; i<items.length; i++) 
    {
    if(input.equals(items[i].getName()))
    {
    System.out.println("Item Found");
    }
     
    else
    {
    System.out.println("Item Not Found");
    }
     
    }
    The output is
    Item Not Found
    Item Found
    Item Not Found
    Item Not Found

    It is comparing every ite
    m in the array and printing the message. How can only have it print Item Found with out the other message. Please help me with this.
    Last edited by helloworld922; November 24th, 2012 at 01:15 AM. Reason: please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array iteration Problem.. Please help

    hint: take a look at these lines of code.

    for(int i=0; i<items.length; i++) 
    {
        if(input.equals(items[i].getName()))
        {
            System.out.println("Item Found");
        }
        else
        {
            System.out.println("Item Not Found");
        }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array iteration Problem.. Please help

    Im sorry I dont know how to fix it. I see that my if statement compare the input to every item in the list and then print out the message..
    But i dun know how to fix it.

    --- Update ---

    Please let me know how i can fix it

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Array iteration Problem.. Please help

    your code is saying every time it is in the for loop check the array element and print if the item is found or not so its going to print 4 times... u should make a method that returns true if the user input matches an item in the array.... you can then write an if statement in the main method that prints item found or not based on the results of the method.... that way it will only print one time

  5. The Following User Says Thank You to C++kingKnowledge For This Useful Post:

    learningjava1 (November 24th, 2012)

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array iteration Problem.. Please help

    It worked!! thank you!!!!

Similar Threads

  1. permutations of a string using iteration
    By ueg1990 in forum Java Theory & Questions
    Replies: 11
    Last Post: August 11th, 2012, 03:23 PM
  2. hash map iteration
    By uniqe_mohini in forum Member Introductions
    Replies: 2
    Last Post: September 10th, 2011, 10:14 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM
  5. Converting a recursion to iteration
    By javaplus in forum Algorithms & Recursion
    Replies: 7
    Last Post: March 3rd, 2010, 05:38 PM