Results 1 to 7 of 7

Thread: Parameter

  1. #1
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Parameter

    I got half credit on this problem for not using parameters. I have no clue on what parameters are and how to use them.

    Problem: Write a Java method that uses a for loop to print "X" any number of times. Uses an input parameter N to control the number of times "X" is printed. Parameter N should Default to 20.
    Last edited by Dinomite07; 12-18-2015 at 09:20 AM.

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Parameter

    You first. Show us what you have done so far? We're not going to do your homework for you, but we can help if you tell us what you've done so far and tell us what part you're having trouble with just now.

    Side note: The fact that you're on a short deadline is your concern and your concern only. Asking for people to 'respond ASAP' will often put people off answering at all. We're all volunteers here and help people because we enjoy it. As soon as there's any kind of actual or perceived pressure then the fun is sucked right out of it. My advice is to be patient, and don't leave it so late next time to seek advice.

  3. #3
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: Parameter

    Java Code:
    import java.util.Scanner;
    
    public class PrintX {
    
    	public static void main(String[] args) {
    		
    		Scanner scan = new Scanner(System.in);
    		int input = scan.nextInt();
    		scan.close();
    		if (input > 20) {
    			input = 20;
    		}
    		for (int i = 0; i < input; i++) {
    		System.out.println("X");
    		}
    	
    	}
    }
    Last edited by Dinomite07; 12-18-2015 at 09:52 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Parameter

    When posting code please wrap it in [code] tags [/code].

    As for the question itself, I think when it talks of an input parameter I think it might be talking about supplying an argument (it's a bit vague), and not getting input from the console.
    I'm thinking that because the "(p)arameter N should Default to 20" makes no sense otherwise.

    So args[0] should have a number in it. If it doesn't then 'n' shoudl default to 20, otherwise use the number given.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Dinomite07 is offline Member
    Join Date
    Nov 2015
    Posts
    24
    Rep Power
    0

    Default Re: Parameter

    Quote Originally Posted by Tolls View Post
    When posting code please wrap it in [code] tags [/code].

    As for the question itself, I think when it talks of an input parameter I think it might be talking about supplying an argument (it's a bit vague), and not getting input from the console.
    I'm thinking that because the "(p)arameter N should Default to 20" makes no sense otherwise.

    So args[0] should have a number in it. If it doesn't then 'n' shoudl default to 20, otherwise use the number given.
    Is it possible for you to write the code out for me? I have no idea on how to write it. I plan on studying *reverse engineer* what you did.

  6. #6
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Parameter

    Tolls is talking about taking a parameter from the command line when you start your program. For example, currently when you start your application you do so with:
    Java Code:
    java PrintX
    where you are passing no parameters and 'args' in the main method parameter list is an empty array. But if you pass a parameter with:
    Java Code:
    java PrintX 45
    then 'args' will be an array with a single item in it at index 0 containing the value 45.

    I think that's most likely what the assignment is asking for.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Parameter

    We don't supply coded solutions here.

    The key bit you want is something like:
    Java Code:
    if (args.length ==0) {
    // set n to 20
    } else {
    // set n to Integer.parseInt(args[0])
    }
    after that you'd do the looping thing.

    But, and this is how all your coding should work, get that bit working first.
    Don't worry about your loop until you have the code for setting 'n' working.
    In fact you can check that by sticking:
    Java Code:
    System.out.println(n);
    as the last line.

    When that is outputting the correct value you can then replace that line with the code for doing the loop part.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Parameter help?
    By glockner in forum New To Java
    Replies: 2
    Last Post: 04-13-2015, 10:14 AM
  2. How to get the parameter from a JSP?
    By kosmos890 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-12-2014, 07:52 PM
  3. I think my parameter is wrong?
    By Calaminh in forum New To Java
    Replies: 4
    Last Post: 11-14-2011, 06:26 PM
  4. Ireport x parameter SQL
    By Reginaldo in forum Advanced Java
    Replies: 0
    Last Post: 10-21-2011, 02:03 PM
  5. Using ArrayList as a parameter
    By Crypts in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 06:56 AM

Posting Permissions

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