I don't know why it doesn't workssssssssss


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: I don't know why it doesn't workssssssssss

  1. #1
    Join Date
    Sep 2005
    Posts
    1

    I don't know why it doesn't workssssssssss

    Hi I am trying to run the below code.. at one point it doesnt taks the input at all...(i have highligted it in green) can anyone please tell me why it doe not prompt me for input ?? when i run it separatly it work perfect but in this when the particular case is selected it just works until it gets the movies name...but doesn't prompts me for any particular movie name..please lemme know ..thanks
    (The sql part is right though !)



    while(Flag) {
    System.out.println();
    System.out.println();

    System.out.println("MENU");

    System.out.println("(M) LIST ALL MOVIES");

    System.out.println("(A) LIST ALL ACTORS");

    System.out.println("( R ) USER RATINGS");

    System.out.println("(Q) QUIT");

    System.out.print("ENTER YOUR CHOICE : ");
    int c = System.in.read();
    try
    {

    switch ((char) c)
    {
    case 'm':
    case 'M' :

    System.out.println("You have selected M");

    rst = stmt.executeQuery("select Title from Movie");

    // Print the result
    System.out.println("THE MOVIE TITLES ARE :");

    while (rst.next()) {
    System.out.println(" " + rst.getString(1));
    }
    System.out.println("You have selected M");

    System.out.print("please enter movie name:");
    String m= br.readLine();
    System.out.println("moviename:" +m);

    String sql="select AName from acts_in where title= ?";
    PreparedStatement pstmt =conn.prepareStatement(sql);
    pstmt.setString(1,m);
    rst=pstmt.executeQuery();
    System.out.println("the actors are");
    while(rst.next())
    {
    System.out.println( " "+rst.getString(1));
    }
    System.out.println("\nQuery executed. Connection closed");

    rst.close();
    stmt.close();
    conn.close();



    break;
    case 'a':
    case 'A': System.out.println("Your are in A");


    case 'q' :
    case 'Q' : Flag=false; break;
    default: System.out.println(); System.out.println("Invalid Choice"); break;
    } //end switch
    }// end switch try

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    You should use another way to get console input:

    Check out this little class
    Code:
    import java.io.*;
    import java.text.*;
    
    public class UserInput {
      static DecimalFormat df=new DecimalFormat("0.000");
      // treat System.in like a buffered reader
      static BufferedReader cons=
          new BufferedReader(new InputStreamReader(System.in));
    
      public UserInput() {}
    
      /**
       * Get an integer from the console
       * The Integer.MIN_VALUE is returned on blank input
       */
      public int getUserInt(String prompt) {
        String inLine=null;
        while (true) { // keep on until value ok
          System.out.println(prompt+" (blank to stop)");
          try {
            inLine=cons.readLine().trim();
            if (inLine.length()==0) {
              System.out.println("Bye for now");
              return Integer.MIN_VALUE;
            }
            return Integer.parseInt(inLine);
          }
          catch (IOException ex) {
            System.err.println("An error occurred");
            ex.printStackTrace();
          }
          catch (NumberFormatException ex) {
            System.err.println(inLine+" is not an integer");
          }
          System.err.println("Please reenter");
        }
      }
      /**
       * Get a double from the console
       * The Double.MIN_VALUE is returned on blank input
       */
      public double getUserDouble(String prompt) {
        String inLine=null;
        while (true) { // keep on until value ok
          System.out.println(prompt+" (blank to stop)");
          try {
            inLine=cons.readLine().trim();
            if (inLine.length()==0) {
              System.out.println("Bye for now");
              return Double.MIN_VALUE;
            }
            return Double.parseDouble(inLine);
          }
          catch (IOException ex) {
            System.err.println("An error occurred");
            ex.printStackTrace();
          }
          catch (NumberFormatException ex) {
            System.err.println(inLine+" is not a valid decimal number");
          }
          System.err.println("Please reenter");
        }
      }
    
      /**
       * Get a string from the console
       */
      public String getUserString(String prompt) {
        System.out.println(prompt+" (blank to stop)");
        while (true) { // keep on until input ok
          try {
            return cons.readLine().trim();
          }
          catch (IOException ex) {
            System.err.println("An error occurred");
            ex.printStackTrace();
          }
        }
      }
      /***********************
       * Main (for test)
       */
      public static void main (String [] args) {
        UserInput ui=new UserInput();
    
        int n=ui.getUserInt("Enter a whole number");
        if (n==Integer.MIN_VALUE) System.exit(0);
        System.out.println("You entered: "+n);
    
        double d=ui.getUserDouble("Enter a decimal number");
        if (d==Double.MIN_VALUE) System.exit(0);
        System.out.println("You entered: "+df.format(d));
    
        String s=ui.getUserString("Enter some text");
        if (s.length() > 0) {
          System.out.println("You entered: " + s);
        }
        System.out.println("Bye for now");
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center