Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Read a char for a category

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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,


    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;
        }
     
    }

    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

    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


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default 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.

Similar Threads

  1. [SOLVED] Why do i need to take 48? char to int.
    By Scotty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 29th, 2010, 10:23 PM
  2. How to take a char value in scanner
    By andaji in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2010, 02:50 AM
  3. coverting to Char?
    By JavaNoob82 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 16th, 2010, 07:52 AM
  4. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 11:13 PM
  5. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM