4 Replies - 190 Views - Last Post: 01 March 2013 - 05:48 PM Rate Topic: -----

#1 Spenny13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 13

Testing if variable holds a decimal point

Posted 01 March 2013 - 05:13 PM

value1 = txtDisplay.getText(); //value 1 declared as a double.
if(String.valueOf(value).contains(".")){

          //then do something.........

}






Hi there, just trying to find a way to test if a variable (value1) holds a decimal from user input.
This doesn't compile, just tried converting the double (value1) into a string then test it using .contains method.
Is This A Good Question/Topic? 0
  • +

Replies To: Testing if variable holds a decimal point

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Testing if variable holds a decimal point

Posted 01 March 2013 - 05:17 PM

Your'e on the right track, but use variables correctly typed.

What type does the method getText() return? Don't assign a variable returned by getText() to a variable of type double.
Was This Post Helpful? 0
  • +
  • -

#3 Spenny13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 13

Re: Testing if variable holds a decimal point

Posted 01 March 2013 - 05:32 PM

Hi Greg, Yeah this is just a Factorial button for a calculator.
  value1 = Double.parseDouble(txtDisplay.getText());

                            if(value1< 0 || value1 > 20){
                                    msgExcept.showMessageDialog(this, "ERROR: Must be positive Integer between 0 and 20", "Message", javax.swing.JOptionPane.ERROR_MESSAGE);
                            }
                            else if (String.valueOf(value1).contains(".")){
                                    msgExcept.showMessageDialog(this, "ERROR: Must be positive Integer between 0 and 20", "Message", javax.swing.JOptionPane.ERROR_MESSAGE);
                            }
                            else{
                            answer =  model.factorial(value1);
                            txtDisplay.setText(String.valueOf(answer));
                            }        
}                                       








I really cant see anything wrong with this
Was This Post Helpful? 0
  • +
  • -

#4 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Testing if variable holds a decimal point

Posted 01 March 2013 - 05:39 PM

Check code

double dd=123.000009;
		if(dd-(int)dd ==0.)System.out.println(dd+"   "+ " holds no point or the number after dot is a 0 ");
		else System.out.println((dd+" holds a dot "))


This post has been edited by burakaltr: 01 March 2013 - 05:44 PM

Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,644
  • Joined: 20-September 08

Re: Testing if variable holds a decimal point

Posted 01 March 2013 - 05:48 PM

Clue: Integer.parseInt fails for anything non-integral
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1