Question: I need someone to look over my code and tell me how to fix the for loop for taking in the subtitle inputs import java.util.Scanner;
I need someone to look over my code and tell me how to fix the for loop for taking in the subtitle inputs
import java.util.Scanner; public class FilmDemo { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { Film [] filmArray = new Film[10]; int numberOfFilms = 0; int option;
do{ System.out.println("Press 1 to enter all information about a film"); System.out.println("Press 2 to enter all information about a foreign film"); System.out.println("Press 3 to enter all information about a Bollywood film"); System.out.println("Press 4 to view all films entered so far"); System.out.println("Press 5 to exit the program"); option = keyboard.nextInt(); keyboard.nextLine();
if(option == 1){ System.out.println("Enter the name of the film"); String newName = keyboard.nextLine(); System.out.println("Enter the director of the film"); String newDirector = keyboard.nextLine(); System.out.println("Enter the year the film was produced"); int newYear = keyboard.nextInt();
Film newFilm = new Film(); newFilm.setName(newName); newFilm.setDirector(newDirector); newFilm.setYear(newYear); boolean alreadyInTheArray = false; int index = 0; while(alreadyInTheArray == false && index < numberOfFilms) { if(filmArray[index].equals(newFilm)) { alreadyInTheArray = true; } }
if(alreadyInTheArray == false) { filmArray[numberOfFilms] = newFilm; numberOfFilms++; } else { System.out.println("This film already exists"); } }
else if(option == 2){ System.out.println("Enter the name of the foreign film"); String newName = keyboard.nextLine(); System.out.println("Enter the director of the foreign film"); String newDirector = keyboard.nextLine(); System.out.println("Enter the year the foreign film was produced"); int newYear = keyboard.nextInt(); System.out.println("Enter the primary language of the foreign film"); String newLanguage = keyboard.nextLine(); keyboard.nextLine(); System.out.println("Enter the English translation of the foreign film"); String newTranslation = keyboard.nextLine();
ForeignFilm newForeignFilm = new ForeignFilm(); newForeignFilm.setName(newName); newForeignFilm.setDirector(newDirector); newForeignFilm.setYear(newYear); newForeignFilm.setPrimaryLanguage(newLanguage); newForeignFilm.setTranslation(newTranslation);
System.out.println("Enter the number of subtitles in the foreign film"); int numberOfSubtitles = keyboard.nextInt();
for(int index = 0; index < numberOfSubtitles; index++) { System.out.println("Enter the next subtitle"); String newSubtitle = keyboard.nextLine(); keyboard.next(); newForeignFilm.addSubtitles(newSubtitle); } boolean alreadyInTheArray = false; int index = 0; while(alreadyInTheArray == false && index < numberOfFilms) { if(filmArray[index].equals(newForeignFilm)) { alreadyInTheArray = true; } }
if(alreadyInTheArray == false) { filmArray[numberOfFilms] = newForeignFilm; numberOfFilms++; } else { System.out.println("This film already exists"); }
}
else if(option == 3){ System.out.println("Enter the name of the Bollywood film"); String newName = keyboard.nextLine(); System.out.println("Enter the director of the Bollywood film"); String newDirector = keyboard.nextLine(); System.out.println("Enter the year the Bollywood film was produced"); int newYear = keyboard.nextInt(); System.out.println("Enter the primary language of the Bollywood film"); String newLanguage = keyboard.nextLine(); keyboard.nextLine(); System.out.println("Enter the English translation of the Bollywood film"); String newTranslation = keyboard.nextLine(); System.out.println("Enter the number of subtitles in the Bollywood film");
BollywoodFilm newBollywoodFilm = new BollywoodFilm(); newBollywoodFilm.setName(newName); newBollywoodFilm.setDirector(newDirector); newBollywoodFilm.setYear(newYear); newBollywoodFilm.setPrimaryLanguage(newLanguage); newBollywoodFilm.setTranslation(newTranslation);
System.out.println("Enter the number of subtitles in the Bollywood film"); int numberOfSubtitles = keyboard.nextInt();
for(int index = 0; index < numberOfSubtitles; index++) { System.out.println("Enter the next subtitle"); String newSubtitle = keyboard.nextLine(); newBollywoodFilm.addSubtitles(newSubtitle); }
System.out.println("Enter the number of songs in the Bollywood film"); int numOfSongs = keyboard.nextInt(); for(int index = 0; index <= numOfSongs; index++){ System.out.println("Enter the song(s) that are in the Bollywood film"); String newSong = keyboard.nextLine(); newBollywoodFilm.addSong(newSong); }
System.out.println("Enter the number of secondary languages in the Bollywood film"); int numOfSecondaryLanguages = keyboard.nextInt(); for(int index = 0; index <= numOfSecondaryLanguages; index++){ System.out.println("Enter the secondary language(s) in the Bollywood film"); String newSecondaryLanguage = keyboard.nextLine(); newBollywoodFilm.addSecondaryLanguage(newSecondaryLanguage); } boolean alreadyInTheArray = false; int index = 0; while(alreadyInTheArray == false && index < numberOfFilms) { if(filmArray[index].equals(newBollywoodFilm)) { alreadyInTheArray = true; } }
if(alreadyInTheArray == false) { filmArray[numberOfFilms] = newBollywoodFilm; numberOfFilms++; } else { System.out.println("This film already exists"); }
} else if(option == 4) { for(int i = 0; i < numberOfFilms; i++) { System.out.println(filmArray[i]); } } else if(option == 5){ System.out.println("Goodbye!"); }
else{ System.out.println("Error! Please enter a valid input!"); }
}while(option != 5);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
