Reputation: 11
I am newbie here I have the following code it goes well but when i give a special characters (@,%,* etc) as input it has to throw the exception , so how can i able to do that can somebody help me coz am novice programmer
/* code for checking the value*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CheckVal
{
public static void main(String args[])
{
int i=0;
double x=0;
System.out.println("Enter your angle");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(i);
x=Math.sin(Math.toRadians(i));
System.out.println(x);
if(x>=0 && x<=0.5)
{
ButtonBackground frame = new ButtonBackground("green");
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setResizable(false);
frame.setVisible(true);
}
else{
ButtonBackground frame = new ButtonBackground("red");
//frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setResizable(false);
frame.setVisible(true);
}
}
}
/code for buttonbackground/
import java.awt.*;
import javax.swing.*;
public class ButtonBackground extends JFrame
{
public ButtonBackground(String x)
{
setLayout( new FlowLayout() );
//JButton normal = new JButton(" ");
// add(normal);
if(x.equals("green")) {
JButton test1 = new JButton(" ")
{
@Override
public void paintComponent(Graphics g)
{
g.setColor( Color.GREEN );
g.fillRect(0, 0, getSize().width, getSize().height);
super.paintComponent(g);
}
};
test1.setContentAreaFilled(false);
test1.setBackground(Color.GREEN);
add(test1);
}
else
{
JButton test1 = new JButton(" ")
{
@Override
public void paintComponent(Graphics g)
{
g.setColor( Color.RED );
g.fillRect(0, 0, getSize().width, getSize().height);
super.paintComponent(g);
}
};
test1.setContentAreaFilled(false);
test1.setBackground(Color.RED);
add(test1);
}
}
}
Upvotes: 0
Views: 7789
Reputation: 10055
As far as I can see, it already does but you are catching it. As a suggestion, try to avoid the Pokémon try-catch approach ("Gotta catch 'em all!" by catching a generic Exception
).
By doing
try {
//your code
} catch (Exception e) {
}
You're catching every kind of exception that the code inside the try can throw, without having the chance to know what went wrong. This becomes a nightmare later if your application fails because it does not "break" but it does not do what you want it to do.
In particular you want to get an integer and, if you get special characters, you want to throw an exception. The IllegalArgumentException seems to suit your particular needs since the special characters are, after all, an illegal argument. I suggest that instead of this kind of input reading:
System.out.println("Enter your angle");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
You try with something like this:
System.out.println("Enter your angle");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(br.readLine());
}
catch(NumberFormatException e)
{
throw new IllegalArgumentException();
}
If I'm not mistaken, that's what you want.
As a side note, remember to catch that IllegalArgumentException so it does not propagate way up. To have a better management of this, try making a funcion that reads an input and returns an int or, given the case, throws an exception.
private int readEntry() throws IllegalArgumentException {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
i=Integer.parseInt(br.readLine());
} catch(NumberFormatException e) {
throw new IllegalArgumentException();
}
}
Then you can call it in your main function and do whatever you think neccesary. This also improves readability.
int input = readEntry(); //Surround this with a try-catch and catch the IllegalArgumentException if you want to control it.
Upvotes: 1