-1

I am working on an exercise, where I have to select a category(genre) of movie and based on my selection, the program will return a list of movies in that category from an ArrayList of objects.

My program works when typing out a category in string format. However I am trying to use a try catch block to also allow category selection by number.

My catch block is working, however my try block is not and returns nothing. Can someone help me determine what is wrong with my code? I am guessing there is something wrong with my parseInt assignment?

                System.out.print("What category are you interested in?");
                String catSel = sc.next();

                try //Check category for Integer, otherwise catch
                    {   
                     int numSel = Integer.parseInt(catSel);
                        if(numSel == 1)
                        {catSel = "animated" ;}
                        if(numSel == 2)
                        {catSel = "drama";}
                        if(numSel == 3)
                        {catSel = "horror";}
                        if(numSel == 4)
                        {catSel = "scifi";}
                        if(numSel == 5)
                        {catSel = "musical";}
                        if(numSel == 6)
                        {catSel = "comedy";}
                        else catSel = "";

                      //Check each movie for chosen category
                      for(int x = 0; x < list.size() - 1; x++)
                      {
                        if(catSel.equals(list.get(x).category))
                        System.out.println(list.get(x).movie);
                      }
                    }
                catch (NumberFormatException e)
                    {
                      //Check each movie for chosen category
                      for(int x = 0; x < list.size() - 1; x++)
                      {
                        if(catSel.equals(list.get(x).category))
                        System.out.println(list.get(x).movie);
                      }
                    }
drawflush
  • 3
  • 4
  • so, what's not working here, could you clarify? – Mysterion Jan 28 '15 at 12:48
  • has catSel a value or is always the else called in your try-block? – Christian Jan 28 '15 at 12:49
  • When you enter a numerical value for category (eg 1, 2, 3 etc ) it should check the string for integer and then set the category accordingly and return all movies in that category. Which it does not. However if I type the category " drama " it works fine. – drawflush Jan 28 '15 at 12:52

2 Answers2

1

the way your if-clauses are structured, the else clause will be called whenever numSel is not 6, replacing catSel with the empty string.

You may want to add an else after each if block or replace all of them with a switch statement.

Dragondraikk
  • 1,659
  • 11
  • 21
  • Dragondraikk, yes of course! I need 'else if' instead of just 'if'. That fixed my problem. Silly newbie mistake :) Thank you. – drawflush Jan 28 '15 at 13:01
1

As @Dragondraikk suggested your if-else clauses are structured in a way which is not as per your expected result .

So either use in this way :

if(someCondition){
}
else if(someCondition){
}
...........................
 do whatever you want to do 
...........................
else{

}

Below is the way to use Switch Statement

switch(Integer.parseInt(catSel)){

   case 1 :
           do Something....
           break;
   case 2 :
           do Something....
           break;
   case 3 :
           do Something....
           break;
   case 4 :
           do Something....
           break;
   case 5 :
           do Something....
           break;
   case 6 :
           do Something....
           break;
   default :
           catSel="";
           break;
}

Note : You can use try-catch block around this

Update

Advantage of Using Switch over If else

The problem with the if...else if... chain is readability , I have to look at every single if condition to understand what the program is doing. For example, you might have something like this:

if (a == 1) {
    // stuff
} else if (a == 2) {
    // stuff
} else if (a == 3) {
    // stuff
} else if (b == 1) {
    // stuff
} else if (b == 2) {
    // stuff
}

(obviously, for a small number of statements like this, it's not so bad)

but I'd have no way of knowing that you changed condition variable half-way through without reading every single statement. However, because a switch limits you to a single condition variable only, I can see at a glance what's happening.

Another advantage is JumpTable

A switch is often compiled to a jump-table (one comparison to find out which code to run), or if that is not possible, the compiler may still reorder the comparisons, so as to perform a binary search among the values (log N comparisons). An if-else chain is a linear search .

Here is more about Switch Statement

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Is the Switch Statement a generally preferred way of doing this? What would be the advantages or disadvantages of each way? Does one way have any noticeable performance benefit over the other? – drawflush Jan 28 '15 at 13:09
  • 1
    Switch is the correct way with constants and enums. if-else are the correct way with variables and mutable objects because switch requires the data at compile time. if-else are more flexible but lengthier and harder to read. Performance is the same, because switches are basically eye candy for if-else – Angelo Alvisi Jan 28 '15 at 13:29
  • @AngeloAlvisi I agree with you in all points but can you please look into this for the clarification on Performace point : [If vs. Switch Speed](http://stackoverflow.com/questions/445067/if-vs-switch-speed) – Neeraj Jain Jan 28 '15 at 13:50