-
March 14th, 2003, 09:40 PM
#1
Junior Member
Java
I didn't really know where this would should go at so I put it here.
How do you figure out how to determine whether the input data is an integer or a floating-point data type in Java?
[glowpurple]VENOM[/glowpurple] [gloworange]FROGG[/gloworange]
-
March 14th, 2003, 09:40 PM
#2
Junior Member
Java
I didn't really know where this would should go at so I put it here.
How do you figure out how to determine whether the input data is an integer or a floating-point data type in Java?
[glowpurple]VENOM[/glowpurple] [gloworange]FROGG[/gloworange]
-
March 15th, 2003, 03:00 PM
#3
Member
not sure
i´m not sure but i think it´s with typeof
-
March 15th, 2003, 03:00 PM
#4
Member
not sure
i´m not sure but i think it´s with typeof
-
March 15th, 2003, 05:17 PM
#5
I tried it and typeof doesn't work (phew, I thought that java had a keyword that I didn't know of). If you want to determine what kind of value has been input in to a textbox or other string, then do a String.indexOf(".") if any value except -1 is returned, then the value is a float, otherwise, it's an integer. Also, after determining the type of datatype, you might want to do and Integer.parseInt() and a Float.parseFloat() in the appropriate if condition and catch a NumberFormatException, this will prevent users from inputing text.
Cheers,
cgkanchi
-
March 15th, 2003, 05:17 PM
#6
I tried it and typeof doesn't work (phew, I thought that java had a keyword that I didn't know of). If you want to determine what kind of value has been input in to a textbox or other string, then do a String.indexOf(".") if any value except -1 is returned, then the value is a float, otherwise, it's an integer. Also, after determining the type of datatype, you might want to do and Integer.parseInt() and a Float.parseFloat() in the appropriate if condition and catch a NumberFormatException, this will prevent users from inputing text.
Cheers,
cgkanchi
-
May 6th, 2003, 02:08 AM
#7
Senior Member
This is one possible solution, if I understand the question correctly.
Widgets, such as JTextField, read in data as strings. So you can parse it for a decimal point to check for a floating point value. An example might be...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class findtype extends JFrame
{
private JTextField text1, text2;
private JButton testTypeBtn, clearBtn;
public findtype()
{
super("findtype test");
Container c = getContentPane();
c.setLayout(new FlowLayout());
text1 = new JTextField(10);
text2 = new JTextField(30);
testTypeBtn = new JButton("Test");
clearBtn = new JButton("Clear");
testTypeBtn.addActionListener(new BtnEvnt());
testTypeBtn.addActionListener(new BtnEvnt());
c.add(text1);
c.add(text2);
c.add(testTypeBtn);
c.add(clearBtn);
setBounds(0,0,400,200);
show();
}
public static void main(String args[])
{
new findtype();
}
private class BtnEvnt implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source == testTypeBtn)
{
if( text1.getText().toString().indexOf(".") == -1 )
text2.setText("possbily a plain old integer.");
else
text2.setText("possibly a floating point number.");
}
else
{
text1.setText("");
text2.setText("");
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|