Read a char for a category
i need to allow the user to input a single char instead of the whole word without changing the public class MovieIO,
Code :
import java.util.ArrayList;
import java.util.Scanner;
public class MovieListApp {
static Scanner sc = new Scanner(System.in);
System.out.println(" Welcome to the Movie List Application!\n");
ArrayList<Movie> movies = getMovies(); // fills the array list
System.out.println(" There are " + movies.size() + " movies in the list.");
String category; // used to search for films of a specific category
do {
category = getString(" What category are you interested in? (or (X) to Exit)\n (A)nimated (C)omendy D(o)cumentary\n (D)rama (H)orror (M)usical\n (S)ciFI\n Enter Your Selection -> ");
for (Movie m : movies)
{
if (m.category.equalsIgnoreCase(category))
System.out.println(m.title);
}
System.out.println();
} while (!category.equalsIgnoreCase("X"));
System.out.println("\n Program terminated ");
}
public static ArrayList<Movie> getMovies() {
Movie m;
ArrayList<Movie> movies = new ArrayList<Movie>();
for (int i = 1; i <= 102; i++)
{
m = MovieIO.getMovie(i);
movies.add(m);
}
return movies;
}
public static String getString(String prompt) {
String s = "";
boolean isValid = false;
while (!isValid) // loops until user enters a non-blank line
{
System.out.print(prompt);
s = sc.nextLine();
if (!s.equals(""))
isValid = true;
}
return s;
}
}
Code :
public class Movie {
public String title;
public String category;
public Movie(String title, String category) {
this.title = title;
this.category = category;
} // end CONSTRUCTOR
} // end Movie CLASS
Code :
public class MovieIO {
public static Movie getMovie(int index) {
switch (index) {
case 1:
return new Movie("Citizen Kane", "drama");
case 2:
return new Movie("Casablanca", "drama");
case 3:
return new Movie("The Godfather", "drama");
default:
return new Movie("NO SUCH MOVIE", "");
} // end SWITCH
} // end getMovie METHOD
} // end MovieIO CLASS
Re: Read a char for a category
Um, yea... pretty sure this forum is not going to respond, Please Refer to The Welcome Announcement - General Guidelines Part A - Particularly the words in Bold.