Results 1 to 3 of 3
  1. #1
    benn22 is offline Member
    Join Date
    Feb 2011
    Posts
    17
    Rep Power
    0

    Default Various questions

    Hi, I am making a movie manager program and I have some questions to ask.

    1. Multiple commands

    Java Code:
    		System.out.println("Enter the three main casts of the movie:");
    		movieCast = in.nextLine();
    		for (int i = 1; i<3; i++){
    			System.out.println(movieCast);
    		}
    I want to have three main casts for the movie, but this input will only take one name and repeat it three times. For example, if I type "cast1", it will print:

    Java Code:
    cast1
    cast1
    cast1
    2. Skipping an input (2 Parts)

    Java Code:
                    System.out.println("Enter the genre of the movie:");
    		System.out.println("Action/Drama/Science Fiction/Horror/Martial Arts");
    		System.out.println("Note: Press 'Enter' to cancel this option.");
    		movieGenre = in.nextLine();
    		movieGenre = movieGenre.toLowerCase();
    		movieGenreChar = movieGenre.charAt(0);
    		switch (movieGenreChar){
    			case 'a':
    				movieGenre = "Action";
    				break;
    			case 'd':
    				movieGenre = "Drama";
    				break;
    			case 's':
    				movieGenre = "Science Fiction";
    				break;
    			case 'c':
    				movieGenre = "Comedy";
    				break;
    			case 'h':
    				movieGenre = "Horror";
    				break;
    			case 'm':
    				movieGenre = "Martial Arts";
    				break;
    			case 'o':
    				movieGenre = "Other";
    				break;
    		}
    Here, I would like the user to skip this input by pressing the 'Enter' key. I've tried various methods with a do-while loop, but none of them seemed to work well for me. Similarily:

    Java Code:
    		System.out.println("Enter the rating of the movie (1-5 stars):");
    		System.out.println("Note: Enter '-1' to cancel this option.");
    		do{
    			try{
    				movieRating = in.nextInt();
    			}catch (InputMismatchException e){
    				System.out.println("Please enter an integer from 1-5:");
    				in.next();
    			}
    		}while (movieRating < 1 || movieRating > 5);
    In this case, the user should just type '-1' to skip this input. However, even if the user types '89', for instance, the program will just keep on asking for inputs (with white-space).
    i.e.
    Java Code:
    89
    
    67
    23
    sadas
    Please enter an integer from 1-5:
    sa
    Please enter an integer from 1-5:
    34
    
    67
    etc.

    Thank you.
    ben

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok for the first one you are telling it to input one name, and then print that name three times. To fix that move the input inside the for loop. Like this:
    Note: i < 3; changes to i <= 3 because we want the loop to execute three times not 2
    Java Code:
    for(int i = 1; i <= 3 i ++)
    {
        System.out.println("Enter the three main casts of the movie:");
        movieCast = in.nextLine();
        System.out.println(movieCast);
    }

    For the second one what does it do after you press ENTER? The code looks like it should work.

    For the third I think that this might work better for the while part of your do-while loop:
    Java Code:
    while(movieRating >= 1 && movieRating <= 5)

    This above method allows the input to be any int from 1 to 5, and it will exit the loop if it is anything else. Also InputMismatchException is only thrown if the type of the input is wrong, so if you put in an 88 it will just exit the loop and go on. You could do something like this to catch numbers that are greater than 5:
    Java Code:
    System.out.println("Enter the rating of the movie (1-5 stars):");
    System.out.println("Note: Enter '-1' to cancel this option.");
    do
    {
        try
        {
            movieRating = in.nextInt();
        }
        catch (InputMismatchException e)
        {
            System.out.println("Please enter an integer from 1-5:");
    	in.next();
        }
        if(moveRating > 5)
        {
            System.out.println("Please enter an integer from 1-5:");
            in.next();
        }
    }while (movieRating >= 1);
    Last edited by Zman3359; 02-11-2011 at 02:55 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    1. Look closely at your code. I''l pseudocode it for you.

    Prompt user
    Get input
    Loop three times (actually your code only loops twice i = 1 and i = 2)l

    2. a) If user enters nothing then you input will be a zero length string. As such this line will generate an IndexOutOfBoundsException.
    Java Code:
    movieGenreChar = movieGenre.charAt(0);
    Perhaps you should check if user input was nothing before doing anything.

    2. b) Check the condition in your while loop. Since 89 satisfies the second condition the loop will continue.

Similar Threads

  1. questions
    By amaliutz in forum New To Java
    Replies: 13
    Last Post: 02-02-2011, 06:13 PM
  2. Some Questions
    By MuslimCoder in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 04:01 PM
  3. questions for 1yr exp
    By rahaman.athiq in forum Java Servlet
    Replies: 2
    Last Post: 11-26-2008, 01:13 AM
  4. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  5. Questions About JSP?
    By mtz1406 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-19-2008, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •