How do you take a negative in through...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How do you take a negative in through...

  1. #1
    Join Date
    Nov 2004
    Posts
    13

    How do you take a negative in through...

    I am trying to write a program that takes in a double (x)and an integer (n) and returns x^n. The problem I run into is input a negative number, either for x or n. Is this code sufficient? (assuming I have imported the necessary packages)

    String userInput = JOptionPane.showInputDialog("Please enter x");
    double x = Double.parseDouble(userInput);

    userInput = JOptionPane.showInputDialog("Please enter n");
    int n = Integer.parseInt(userInput);

    If there is an easier way of taking in a negative using JOptionPane, I would LOVE to here it.

    Thanks for your time.

    Parin

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    There isn't. You could use the version of JInput Dialog
    that takes, and returns an Object (Double in your case).
    If it's simpler is another question though
    eschew obfuscation

  3. #3
    Join Date
    Nov 2004
    Posts
    13
    Ahh that may very well work. Thank ya kindly =)

  4. #4
    Join Date
    Nov 2004
    Posts
    13
    Actually, after running many-a-tests I have concluded that it does indeed take in the negative and parses it.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Of course but the issue was simplification, wasn't it ?
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center