Results 1 to 4 of 4

Thread: Java Code Help

  1. #1

    Java Code Help

    Hi

    Ive got the following code in java and would like to modify it so that the user can define how many balls are displayed in the Balls window.

    Anyone know how i do this ??

    thanks


    import BallsLib.*;
    public class OneBall
    {
    public static void main (String args[])
    {
    int maxNumBalls = 1 ;
    BallsFrame f;
    f = new BallsFrame (maxNumBalls) ;
    f.addBalls();
    }
    }

  2. #2
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    Um.... it would be nice if you were to post the classes with source, or at least some api documentation.

    EDIT: Sorry, I didn't look at your code properly. Yes, it should be possible. Since this seems to be a command line app, just do this:

    Code:
    import BallsLib.*;
    public class OneBall
    {
        public static void main (String args[])
        {
            if(args.length==1)
                int maxNumBalls = Integer.parseInt(args[0]);
            else
                int maxNumBalls = 1;
            BallsFrame f;
            f = new BallsFrame (maxNumBalls) ;
            f.addBalls();
         }
    }
    Then the program would be compiled with javac OneBall.java and run with java OneBall <number of balls>. For example, java OneBall 20

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  3. #3
    Is there not a way to modify the code i posted so that the user is asked to input the max number of balls they want into the console window, and then what they enter is used to display the number of balls they want ?

    thanks

  4. #4
    Senior Member
    Join Date
    Sep 2001
    Posts
    193
    import BallsLib.*;
    public class OneBall
    {
    public static void main (String args[])
    {
    int maxNumBalls = 1 ;
    BallsFrame f;
    f = new BallsFrame (maxNumBalls) ;
    f.addBalls();
    }
    }
    I am a little rusty in my java but here goes.

    import javax.swing.*;
    import BallsLib.*;
    public class OneBall
    {
    public static void main (String args[])
    {
    int amount;

    amount=Integer.parseInt( JOptionPane.showInputDialog("Enter number of balls: "));


    BallsFrame f;
    f = new BallsFrame (amount) ;
    f.addBalls();
    }
    }
    Now it is a user defined amount of balls. Hoped that helped
    [shadow]l3aDmOnKeY[/shadow]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •