Results 1 to 3 of 3
Thread: Various questions
- 02-11-2011, 02:29 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 17
- Rep Power
- 0
Various questions
Hi, I am making a movie manager program and I have some questions to ask.
1. Multiple commands
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: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); }
2. Skipping an input (2 Parts)Java Code:cast1 cast1 cast1
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 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; }
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).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);
i.e.
etc.Java Code:89 67 23 sadas Please enter an integer from 1-5: sa Please enter an integer from 1-5: 34 67
Thank you.
ben
- 02-11-2011, 02:52 AM #2
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.
- 02-11-2011, 02:56 AM #3
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.
Perhaps you should check if user input was nothing before doing anything.Java Code:movieGenreChar = movieGenre.charAt(0);
2. b) Check the condition in your while loop. Since 89 satisfies the second condition the loop will continue.
Similar Threads
-
questions
By amaliutz in forum New To JavaReplies: 13Last Post: 02-02-2011, 06:13 PM -
Some Questions
By MuslimCoder in forum New To JavaReplies: 2Last Post: 02-25-2009, 04:01 PM -
questions for 1yr exp
By rahaman.athiq in forum Java ServletReplies: 2Last Post: 11-26-2008, 01:13 AM -
I have Questions -_-
By ChazZeromus in forum New To JavaReplies: 5Last Post: 09-13-2008, 08:08 PM -
Questions About JSP?
By mtz1406 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-19-2008, 07:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks