PDA

Click to See Complete Forum and Search --> : Java Code Help


esi1
February 4th, 2004, 12:40 AM
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();
}
}

cgkanchi
February 4th, 2004, 07:39 AM
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:


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

esi1
February 9th, 2004, 11:49 AM
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

l3aDmOnKeY
February 11th, 2004, 12:07 AM
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 :)