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

Thread: can some please help me with this?

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default can some please help me with this?

    My question is, how can i stop the for loop from executing the else statement every time it cannot find a match for the searchInput? even if the for loop found a match for searchInput the for loop still executes the else statement. how can i deal with this?



    here's my original code:

     
    // The For loop executes the Else statement until a match is found for the if statement, how can i deal with this?
    public void searchBook()
        {
        	String searchInput = JOptionPane.showInputDialog(null, "Enter Search Query: ", "Book Search", JOptionPane.INFORMATION_MESSAGE);
     
        	int x;
     
            try
            {
     
        	for(x=0 ; x<bookName.length ; x++)
        	{ 
        		if(searchInput.equalsIgnoreCase(bookName[x])) 
        		{
        			JOptionPane.showMessageDialog(null, "Book Found !" + "\nTitle: " + bookName[x] + "\nAuthor: " + bookAuthor[x] + 
        				"\nPublisher: " + bookPublisher[x] + "\nBook ISBN: " + bookID[x] , "Book Search", JOptionPane.INFORMATION_MESSAGE);
        		}
     
        		else
        		{
        			JOptionPane.showMessageDialog(null, "BOOK NOT FOUND !" , "Book Search", JOptionPane.ERROR_MESSAGE);
        		}	
        	}
            }
     
            catch(Exception e)
            {
     
            }
     
        }

    thanks in advance...
    Last edited by jaydac12; September 26th, 2012 at 08:08 AM.

  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: can some please help me with this?

    Maybe someone could.. if you had a question
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can some please help me with this?

    my question is up there. in the code section at the very top.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: can some please help me with this?

    Okay, how about you post a question outside of the code block, and post one with better detail.
    If that is your original code, then "how do I deal with it" doesn't make much sense seeing as you're the one who put the else in there.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can some please help me with this?

    i edited my post. can you help me now?

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: can some please help me with this?

    2 Simple ways to do it, you can either:
    Remove the else all together, and instead use a boolean variable which is set to true if a match is found, but outside the loop you test if(!matchfound) and put the fail code in there; OR
    Replace else with else if, and test if the current X variable is going to be the last time the loop executes and then print.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. The Following User Says Thank You to newbie For This Useful Post:

    jaydac12 (September 26th, 2012)

  8. #7
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can some please help me with this?

    well that was easy. THANKS !

Tags for this Thread