0
\$\begingroup\$

I am learning to use parameters and arguments in Java to make my coding efficient. I am open to suggestion if I've done something wrong with this code of mine.

import java.util.Scanner;

public class paramsAndArgs {

    static String name, feeling;

    public static void main(String[] args) {
        doesSomething(name, feeling);
    }

    private static void doesSomething(String name, String feeling) {
        Scanner scan = new Scanner(System.in);

        System.out.println("What is your name?");
        name = scan.nextLine();
        System.out.println("How are you today?");
        feeling = scan.nextLine();

        System.out.println("Hello, " + name + ". I am feeling " + feeling + " too.");
    }
}
```
\$\endgroup\$
  • 2
    \$\begingroup\$ There honestly does not seem to be enough code to get much out of a review. LGTM? I'd say post when you have a project with a little bigger scope. \$\endgroup\$ – spyr03 Jul 10 at 1:26
  • \$\begingroup\$ How about this? pastebin.com/WfGrQtdV It works but it feels kinda wrong to me. \$\endgroup\$ – fosionef Jul 10 at 1:46
  • \$\begingroup\$ We can only review the code that is in embedded in the question. You need to show the class and function where this gets used. \$\endgroup\$ – pacmaninbw Jul 10 at 15:05
4
\$\begingroup\$

Even with this simple example, there are a few things I can comment on.

  1. Class names start with a capital letter. ParamsAndArgs, not paramsAndArgs.

  2. When parameters are passed to a method, those parameters should have values. In your code, you're passing the doSomething method two parameters that have no values and aren't used in the doSomething method. They're set in the doSomething method.

  3. While naming a method doSomething is okay when you're not sure what the method does, you should change the name to something meaningful, like getInput, once you know what the method does. Generally methods are named with a verb, noun combination (get, Input).

Here's your code, reworked to be a more accurate example of parameters and arguments.

import java.util.Scanner;

public class ParmsAndArgs {

    static String name, feeling;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        getInput(scan);
        System.out.println("Hello, " + name + ". I am feeling " +
                feeling + " too.");
        scan.close();
    }

    private static void getInput(Scanner scan) {
        System.out.println("What is your name?");
        name = scan.nextLine();
        System.out.println("How are you today?");
        feeling = scan.nextLine();
    }

}
| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.