Solved

I have a java code that is not preforming what i expect to, i am very new to java.

Posted on 2014-12-09
2
210 Views
Last Modified: 2015-01-25
I have the below code and I needed to add a try catch to insure proper data, the below doesn't work at all. It prompts for the blower value but if I type in an improper value it still will not "catch" the error?  Please help if possible I am very new to java.

      String test = " ";
                boolean badInput = true;
                    if(race.getClass()== Hotrod.class)
                    {
                          Hotrod h =(Hotrod)race;
                            //test = (getInput("Blower equipped: Y/N"));
                                  
                            if (test.equalsIgnoreCase("y"))
                          {
                            h.setBlower(true);      
                          }// end if
                          else if (test.equalsIgnoreCase("n"))
                          {
                                h.setBlower(false);            
                          } //end else if
                          else
                          {
                                  
                              do{
                                  try
                                  {
                                        test = (getInput("Blower equipped: Y/N"));
                                        if (!test.equalsIgnoreCase("y")|| !test.equalsIgnoreCase("n"))
                                        {
                                        badInput = false;
                                        }
                                  }
                                  
                                  catch (Exception ex)
                                  {
                                        JOptionPane.showMessageDialog(null,test  + "Please enter the proper value","Error",JOptionPane.PLAIN_MESSAGE);
                                  }
                             } while(badInput);
                             }//end else
                    } //end if(race.getClass()== Hotrod.class)
0
Comment
Question by:spoye
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40489391
In order for an exception to be caught, the exception first needs to be thrown.
http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
In this case, there seems to be no need to use the exception mechanism when you can just check  badInput.
0
 

Author Comment

by:spoye
ID: 40489441
I understand I will alter the code maybe with do while loop?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 72
Facing this issue for maven proxy setting 2 27
Eclipse with various Java releases 7 48
Tomcat 9 + java 8 error while trying to deploy a war file 2 715
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question