0
  1. I want to keep taking choices from the user until he gives an exit statement.
  2. How can I come out of the while loop if I use it in this code?
  3. Is there any other way to take choice from the user than switch case:
  4. And if I try to use while loop for this then it is going in an infinite loop:

Code:

import java.util.Scanner;
class lib
{
public static void main(String args[])
{
    Scanner in=new Scanner(System.in);
    String c;
    int j=5,a=5,s=5,cg=5;
    int d=0;
    System.out.println("Available Copies :");
    System.out.println("java=5,ada=5,sp=5,cg=5");
    System.out.println("enter book title");
    //System.out.println("enter exit for exit if don't want to search");
    c=in.nextLine();
    while(d==0)
    {
        switch(c)
       {
        case "java":
            System.out.println("author is herbert");
            System.out.println("price :500");
            j--;
            break;

        case "ada":

            System.out.println("author is corman");
            System.out.println("price :600");
            a--;
            break;

        case "sp":

            System.out.println("author is dhamdhire");
            System.out.println("price :550");
            s--;
            break;

        case "cg":

            System.out.println("author is pearson");
            System.out.println("price :700");
            cg--;
            break;
        case "exit":
            d++;
            break;

        default:
            System.out.println("book not available");
    }
    }
    if(j!=0)                                      
    System.out.println("number of available copies is"+j);

 }
Harsha pps
  • 2,012
  • 2
  • 25
  • 35
Deep tank
  • 25
  • 8
  • 2
    I was writing an answer but then realized that I have no idea what all your counter variables are supposed to be doing. It would help your question if you could add a clear problem statement. – Tim Biegeleisen Jul 21 '18 at 13:13
  • actually sir, counters are for decreasing the number of book copies after each issue which is 5 initially. – Deep tank Jul 21 '18 at 17:46

2 Answers2

-1

I think what your looking for is moving your nextLine inside the while loop.

This part of the code:

//System.out.println("enter exit for exit if don't want to search");
    c=in.nextLine();
    while(d==0)
    {
        .....

To this:

//System.out.println("enter exit for exit if don't want to search");

    while(d==0)
    {
        c=in.nextLine();
        .....
Ogiez
  • 97
  • 6
-1

If you want to keep taking input from the user until they give the exit command then you need to keep taking input inside the while loop. Move c=in.nextLine() into the while loop right before the switch statement.

If you want to prompt the user as well then add a print statement at the end of the loop right after the switch statement ends, and instead move c=in.nextLine() to the end of the while loop right after the print statement. Something like:

System.out.print("Enter the title of another book: ");
c=in.nextLine();
buckeyeguyy
  • 121
  • 8