Java
Results 1 to 7 of 7

Thread: Java

  1. #1
    Junior Member
    Join Date
    Aug 2002
    Posts
    21

    Post 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]

  2. #2
    Junior Member
    Join Date
    Aug 2002
    Posts
    21

    Post 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]

  3. #3
    Member
    Join Date
    Mar 2003
    Posts
    38

    not sure

    im not sure but i think its with typeof

  4. #4
    Member
    Join Date
    Mar 2003
    Posts
    38

    not sure

    im not sure but i think its with typeof

  5. #5
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    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
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  6. #6
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    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
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  7. #7
    Senior Member
    Join Date
    Apr 2003
    Posts
    103
    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
  •