简体   繁体   中英

Java issue using parseInt with a try catch block

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM