K
kt
Hello folks,
I tried to do get so far with my code by using isDigit()
method but don't know how to use it for float. This is my code. Also i
am
not exposed to exceptions at all. So is there a method that i could use
without using exceptions. The following code works fine if i type an
integer number, or a character but if type something like "7A" it gets
messed up instead of saying "NAN". I don't know how to check for float
and
invalid float like " 1.3.6". Any help greatly appreciated.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CheckNum extends Applet
implements ActionListener
{
private TextField inputTF;
private String inputVal;
private boolean isInteger;
public void init()
{
Label textLbl = new Label("A program to input a string and
display the
type of the number");
inputTF = new TextField(7);
add(textLbl);
add(inputTF);
inputTF.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == inputTF)
inputVal = inputTF.getText().trim();
repaint();
}
public void paint(Graphics g)
{
char [] letters;
letters = inputVal.toCharArray();
for(int x = 0; x < letters.length; x++)
{
if(Character.isDigit(letters[x]) == true)
g.drawString("Integer", 50, 100);
else
g.drawString("NAN", 50, 100);
}
}
}
I tried to do get so far with my code by using isDigit()
method but don't know how to use it for float. This is my code. Also i
am
not exposed to exceptions at all. So is there a method that i could use
without using exceptions. The following code works fine if i type an
integer number, or a character but if type something like "7A" it gets
messed up instead of saying "NAN". I don't know how to check for float
and
invalid float like " 1.3.6". Any help greatly appreciated.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class CheckNum extends Applet
implements ActionListener
{
private TextField inputTF;
private String inputVal;
private boolean isInteger;
public void init()
{
Label textLbl = new Label("A program to input a string and
display the
type of the number");
inputTF = new TextField(7);
add(textLbl);
add(inputTF);
inputTF.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == inputTF)
inputVal = inputTF.getText().trim();
repaint();
}
public void paint(Graphics g)
{
char [] letters;
letters = inputVal.toCharArray();
for(int x = 0; x < letters.length; x++)
{
if(Character.isDigit(letters[x]) == true)
g.drawString("Integer", 50, 100);
else
g.drawString("NAN", 50, 100);
}
}
}