Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Asking for user input in the main method?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Asking for user input in the main method?

    I've created two classes, a TestDrive and the one I use to perform a single calculation, which is add or subtract 2 numbers. If I take away the userinput option, I can compile the files correctly, but I can't figure out why my JOptionPane input technique isn't working. Should I create a separate class for input? The instructions said to keep it in the TestDrive class, but does this make sense? I didn't think that that was the purpose of a testDrive class.


    public class NumbersTestDrive
    {
    public static void main(String[] args)
    {

    //create the object
    Numbers number1 = new Numbers();//reference variable for an object...Number1 is created from the Numbers class
    //

    number1.name = JOptionPane.showInputDialog(null, "Enter a name: ");

    number1.grade = JOptionPane.showInputDialog(null, "Enter a grade: ");

    enterednum1string = JOptionPane.showInputDialog(null, "Enter in a number:");
    number1.enterednum1 = Double.parseDouble(enterednum1string);

    enterednum2string = JOptionPane.showInputDialog(null, "Enter in another number:");
    number1.enterednum2 = Double.parseDouble(enterednum2string);

    operatorstring = JOptionPane.showInputDialog(null, "Enter 1 for subtraction\nEnter 2 for addition:");
    number1.operator = Int.parseInteger(operatorstring);

    //call the method
    number1.addorsubtract(number1.enterednum1, number1.enterednum2, number1.operator);
    double whatever = number1.addorsubtract(number1.enterednum1, number1.enterednum2, number1.operator);

    JOptionPane.showMessageDialog(null,"Student's Name " + number1.name + "\nStudent's Grade" + number1.grade + "\nSolution " + whatever);
    }


    }


    public class Numbers
    {
    String name;//create instance variable
    String grade;//create instance variable
    int operator;//create instance variable
    double enterednum1;//create instance variable
    double enterednum2;//create instance variable

    public double addorsubtract(double x, double y, int z)//parameters - parameter list
    {
    if(z == 1)
    {
    double result = x - y;
    return result;
    }
    else
    {
    double result = x + y;
    return result;
    }

    }


    }


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Asking for user input in the main method?

    What specific error are your getting? And when you post code, post it between a
      [ code] *code here* [ /code] (no space before the "/" or the "c")
    to make it easier to read.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Asking for user input in the main method?

    I took another look, and it was just basic syntax errors. It works, but am I right in saying that it's bad usage to have the testdrive class do anything more than "test" other classes?

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Asking for user input in the main method?

    A class can do whatever you want it to do. If that's what you want your TestDrive class to do, then so be it. When it comes to testing classes, I've come to learn that it works just as well to create a main method in the class itself, and test it in the same class, because then you can test your private methods along with the public ones. If testing in another class is what you want, then go for it. What you're doing here, creating a test class for every other class you make (did I get that right?) is sort of needless. It works just fine if you create a single class you can use for testing other classes. Something like this works great.

    package testing;
     
    // import JOptionPane, BigInteger, ArrayList, Scanner, Math, and any classes you are testing
     
    public class testing {
          public static void main(String[] args) {
                long start = System.currentTimeMillis();
     
                /**
                  *Code to test goes here
                **/
     
                long end = System.currentTimeMillis();
     
                System.out.println("Elapsed time: " + (end-start) + " ms."); //prints the amount of time the program takes to complete is milliseconds, useful for algorithm designing. 
          }
    }

    That is just a generic testing class that you can use to test most things.

Similar Threads

  1. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 01:48 PM
  2. User Input with a Do Loop
    By RadiantChaos in forum Loops & Control Statements
    Replies: 4
    Last Post: March 13th, 2012, 08:14 PM
  3. Replies: 3
    Last Post: October 31st, 2011, 01:42 AM
  4. Replies: 5
    Last Post: August 11th, 2011, 01:39 PM
  5. [SOLVED] Help with getting user input for my factorial method
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 02:24 PM