简体   繁体   English

使用parseInt和try catch块的Java问题

[英]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. 我正在做一个练习,其中我必须选择电影的类别(流派),然后根据我的选择,程序将从对象的ArrayList返回该类别的电影列表。

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. 但是,我试图使用try catch块来也允许按数字选择类别。

My catch block is working, however my try block is not and returns nothing. 我的catch块正在工作,但是我的try块不工作,什么也不返回。 Can someone help me determine what is wrong with my code? 有人可以帮助我确定我的代码有什么问题吗? I am guessing there is something wrong with my parseInt assignment? 我猜我的parseInt分配有问题吗?

                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. 按照if子句的结构方式,只要numSel不为6,就会调用else子句,用空字符串替换catSel

You may want to add an else after each if block or replace all of them with a switch statement. 您可能希望在每个if块之后添加一个else或将所有它们替换为switch语句。

As @Dragondraikk suggested your if-else clauses are structured in a way which is not as per your expected result . 正如@Dragondraikk所建议的那样, if-else子句的结构与预期结果不同。

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语句的方法

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 注意 :您可以在此周围使用try-catch块

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. if ... else if ...链的问题是可读性,我必须查看每个if条件,以了解程序在做什么。 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 另一个优势是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). 通常将开关编译到跳转表 (一个比较以找出要运行的代码),或者如果不可能,则编译器仍可以对比较进行重新排序,以便在值之间执行二进制搜索(log N)。比较)。 An if-else chain is a linear search . if-else链是线性搜索

Here is more about Switch Statement 这是有关Switch语句的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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