Results 1 to 5 of 5

Thread: Program

  1. #1
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default 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

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default

    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!

  4. #4
    dbaby7 is offline Member
    Join Date
    Aug 2009
    Posts
    10
    Rep Power
    0

    Default

    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!!

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Figured as much. You'd think they'd try to answer it themselves and not have it work, but whatever.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Execute A program from a Program!
    By Moncleared in forum Advanced Java
    Replies: 2
    Last Post: 02-22-2009, 05:17 PM
  2. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 09:24 AM
  3. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM

Tags for this Thread

Posting Permissions

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