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
190 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
Comment Utility
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
Comment Utility
I understand I will alter the code maybe with do while loop?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now