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
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

717 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