HOW TO - assign a main method string to an int.

S

stevek

I want to input one or more numbers parameters at program start. One
would be 'number of iterations' = cycles. As my test programs shows I am
stumped at how to assign the string args[0] to a int variable.

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

System.out.println(" i " + " round-random" + "
round-ramdom");
int cycles = args[0];

for(int i = 0; i < cycles - 1; ++i) {
if(i==0) continue;
System.out.println(i + " " + Math.round(Math.random()*i) + " " +
Math.round(Math.random()*i));

} // for ends
} // main ends
} // Random ends


E:\javastuff\dev\examples>javac Random.java
Random.java:5: incompatible types
found : java.lang.String
required: int
int cycles = args[0];
^
1 error
 
J

Joona I Palaste

stevek said:
I want to input one or more numbers parameters at program start. One
would be 'number of iterations' = cycles. As my test programs shows I am
stumped at how to assign the string args[0] to a int variable.
public class Random {
public static void main(String[] args) {

System.out.println(" i " + " round-random" + "
round-ramdom");
int cycles = args[0];

Check out Integer.parseInt().
 
S

stevek

Thank you. I should have found that.
I want to input one or more numbers parameters at program start. One
would be 'number of iterations' = cycles. As my test programs shows I am
stumped at how to assign the string args[0] to a int variable.

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

System.out.println(" i " + " round-random" + "
round-ramdom");
int cycles = args[0];


Check out Integer.parseInt().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,789
Messages
2,569,634
Members
45,342
Latest member
Sicuro

Latest Threads

Top