1

My program is to enter a substring and return all the books based upon the search.i want to ask the user again to search again from the same records.The code is not asking for the user input for the second time.Kindly help

    boolean runnable=true;
        while(runnable)
        {
            System.out.println("\n\nInput Books you wish for search");
            String search;
            search=br.nextLine();   


            System.out.println("\n\nBooks by your search");

            for(int i=0;i<noOfrecords;i++)
            {
            if(books[i].toLowerCase().contains(search.toLowerCase()))
            {

                System.out.println(books[i]);

            }
            }

            System.out.println("\n\nMore Books");
            for(int i=0;i<noOfrecords;i++)
            {

            if(!(books[i].toLowerCase().contains(search.toLowerCase())))

            {

                System.out.println(books[i]);
            }
            }

            System.out.println("do you wish to search again from the same records?? (y/n)");
            char searchagain=br.next().charAt(0);
            if(searchagain!='y')
            {
                runnable=false;
            }
            else if(searchagain=='y')
            {
                runnable=true;
            }

        }
  • do one thing change searchagain to string and input yes or no... print search again after else if(searchagain.equalsIgnorecCase.("yes")) { runnable=true; } to verify – Naren Feb 28 '14 at 11:11

4 Answers4

0

Because you are not able to read single character from console - System.in because new bytes are available in stream after Enter key is pressed.

Can be useful:

m-szalik
  • 3,546
  • 1
  • 21
  • 28
0

I made little changes in your code.

public void searchMethod(List<String> books){
            Scanner scanner = new Scanner(System.in);
            System.out.println("\n\nInput Books you wish for search");
     //this code is executed when books is null, so first time you can invoke that method passing as argument simply null.
     if(books == null){
              while(scanner.hasNext())
                books.add(scanner.nextLine());  
      }

            System.out.println("\n\nBooks by your search");

            for(String book : books)
            {
            if(book.toLowerCase().contains(search.toLowerCase()))
            {

                System.out.println(book);

            }
            }

            System.out.println("\n\nMore Books");
            for(String book:books)
            {

            if(!(book.toLowerCase().contains(search.toLowerCase())))

            {

                System.out.println(book);
            }
            }

            System.out.println("do you wish to search again from the same records?? (y/n)");
            char searchagain=scanner.nextLine();
            if(!searchagain.equals("y"))
            {
                 return;

             }
            else if(searchagain.equals("y"))
            {
                searchMethod(books);

            }

        }
}
RMachnik
  • 3,598
  • 1
  • 34
  • 51
  • I like "Break" more than "Return" – Thanh Le Feb 28 '14 at 11:14
  • There is no while now. I made reqursive call to the same method look deeper. :) – RMachnik Feb 28 '14 at 11:17
  • thx bro but how do i use this method without any parameters beacuse this is not the complete code.i have saved data in the array books[] outside this method and noOfelements as well?? – user3364497 Feb 28 '14 at 11:28
  • Yes I think. It can be done in that way. You have only replace it and it will works fine. When books[] will be null it will ask about fulfilling it by user but if it is not null it will skip that part. – RMachnik Feb 28 '14 at 11:35
  • You can accept that answer and add vote if it was helpfull to you, thanks! – RMachnik Feb 28 '14 at 12:36
0

I am sure that you have an extra unconsumed new line character in your buffer. Consume it by adding a line at the end of the while loop after else-if

   else if(searchagain=='y')
        {
            runnable=true;
        }    
   br.nextLine();

When you hit enter after keying the first input, Scanner.nextLine() advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. so, the line seperator \n is still in the buffer, which is been consumed while iterating the loop for the second time.we need to consume it purposefully by invoking br.nextLine(); at the end of the while loop and leave the new line character \n in air and go for second loop for fresh input. This will make it to stop and get the input for the second time.

Let me explain you in a simple way. Your program asks for

Input Books you wish for search

Now, you give Effective Java and press enter. This would become

EffectiveJava\n

\n (unconsumed char) in the end of your input will not be consumed by br.nextLine(). So, we need to explicitly get rid of it at the end of while loop. so, that \n will not be consumed by the br.nextLine() during the second loop and it will ask for your input

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Glad, it worked :) Please accept the answer to let others know the question is answered and worked. – Keerthivasan Feb 28 '14 at 11:42
  • but bro i didn't understand 'extra unconsumed char' thing.CAn you please explain in a simple way.That will be great help – user3364497 Feb 28 '14 at 11:46
  • but even if i press 'n' it is repeating the loop.it has become a kind of an infinite loop . – user3364497 Feb 28 '14 at 12:04
  • Did the explanation helped you understand? – Keerthivasan Feb 28 '14 at 12:06
  • Please create a new question because that will get you more answers. You can find the button at the right top of the page to create a new question – Keerthivasan Feb 28 '14 at 12:07
  • bro my doubt is if we ask the user to input 2 values say in variables a and b one after another.the user enters "hello" in variable a,so the value of a is hello\n.now what happens to \n in this case? does \n affect the value of b?? – user3364497 Feb 28 '14 at 12:11
  • yes, you need to remove it by consuming it by adding a line in between or skip it. Check this [link](http://stackoverflow.com/questions/17158875/java-is-skipping-a-line-strings-in-a-scanner) to see the accepted answer on how to do it – Keerthivasan Feb 28 '14 at 12:22
0

change next().charAt(0) to nextLine().charAt(). it will work.i think reason for this is next() sperates the token by spaces and read the token one by one from the buffer and stay at the line. when next() reaches the end of line and if u call nextLine(), then nextLine() encounter the end of line and nextLine() return blank line.

char searchagain = br.next().charAt(0); to char searchagain = br.nextLine().charAt(0);

cJ_
  • 486
  • 1
  • 4
  • 19