-
Program
Hi guys
I was wondering if somebody could help me with this. The question I have been asked to do is:
Write a Java program which prints a string a multiple number of times, where the string and the multiple are supplied at the command line. For example, invoking java dog 3 should result in dogdogdog being printed.
What I have at the moment is:
class question1c
{
public static void main(String[] args)
{
String word = args[0];
int amount = Integer.parseInt(args[1]);
for (int i = 0 ; i < amount ; i++ )
{
System.out.print(word);
}
}
}
However this does not fully cover the question because in order to run it I would have to enter "java Question1c dog 3" which is different to what is being asked. Is there any way to complete this question? I tried putting class args[0] but that doesn't work.
Any help would be greatly appreciated.
Thanks
-
Yours is the way to do it. You cant invoke a java program without the class name. This is the answer, and if your teacher tells you otherwise, ask him how its possible, because I'm darn sure its not.
-
Hi thanks! I've emailed my lecturer but I dont know if he will reply! I thought it was just me! Ill let you know if he comes up with a strange solution!!
Thanks!
-
Hi Singing Boyo
Just heard back that the question was written wrong! And that was in a past paper! Way to throw you off!!
Thanks for your help!!
-
Figured as much. You'd think they'd try to answer it themselves and not have it work, but whatever.