write a public class called IfElseDemo. Create a Method called go that takes the args String array from the main method. in that method create an if/else block that looks at the first element of the array and uses the string equals method to determine the output. if it contains true print out "ok", if false output "not ok". if it contains a string other than true or false output "invalid command parameter". use a single sequence of if-else-if ladder statements.

help on how am supposed to create the method that takes the args string array from the main method..how exactly am i supposed to write it???

Recommended Answers

All 13 Replies

The main method's syntax is as follows:
The main method's syntax is as follows:

public static void main(String args[])
{
   // ...
}

The args[] variable is simply an array of String, and can be passed to other methods like every other variable. When typing in the command line parameters in addition to the name of the program, i.e.

ProgramName param1 param2 param3

Then the args[] array will be filled as follows:

args[0] = param1;
args[1] = param2;
args[2] = param3;

This can be done is 2 ways.

1. one way is to copy the args values to local array variable and pass this new array to method.

2. Second way is to directly pass the args to method

public class IfElseDemo
 {

public go()//method go
 {
//how am i suppose to write this part

so how am i suppose to phrase my next line

args []= ???

As you stated, the method go needs to accept the parameter args[] from the main method, so you cannot right just go(). The syntax for a method in Java is:

<visibility> <return type> <method name> (<optional parameters>)

In our case you want the method go to be public, to return nothing (since it only needs to print on the screen) so the return type will be void and have the args array as a parameter:

public void go(String args[])
{
   //....
}

Now - how would you call the method go from the main method, and how would you use the parameters inside the go method (refer to my previous post regarding the second part)

am seriosly blank on the second part buh

from the main method

public class IfElseDemo
 {
public void go(string args[])
  {
   //..help..
  }

public static void main (String args[])
  {

  IfElseDemo myCode = new IfElseDemo(); 
  myCode.go(<argument>);
  }
 }

whats confusing is the part where i have to
initialise args[0]=param;
and use this in an if-else block
if i had simply declared

string s1 = "java is something";
string s2 = "java is something";

then i would have

if (s1.equals(s2))

buh here we are using args[0]=param1;
i have no idea on how to compare,its not making sense right now...still a begginner with this..hope u understand

args[0] is a String, just like s1. Assuming you have two parameters in the command line, such that args[0]=param1 and args[1]=param2, you can compare between them with:

if(args[0].equals(args[1]))

ahh cool thanks alot makes sense now

public class IfElseDemo 
 {
  public void go(string args[])  
  {  
   args[0]=param1;
   args[1]=param2;
 

    if (args[0].equals(args[1]))
    {
    System.out.println("ok");
    }
    else if (args[0].compareTo(args[1]))
    {
    System.out.println("Not Ok");
    }

  
  else
   {
   System.out.println("invalid command parameter");
   }

}
 
public static void main (String args[])  

{
  
 IfElseDemo myCode = new IfElseDemo(); 
  myCode.go(<argument>); 

 }
 }

thanks alot..

one last question what do i put

myCode.go(<argument>);

inside the parantheses

Member Avatar for coil

Your method header asks for an array of Strings as the argument, so you must pass an array of Strings. For example:

String[] s=new String[2];
//supply values for s[0] and s[1]
myCode.go(s);

By the way, in line 3 (your method header), String is spelt with a capital S. Java is case-sensitive, so the way you've spelt it now will cause errors.

Let's review what your code does so far:

IfElseDemo myCode = new IfElseDemo(); 
myCode.go(<argument>);

Which means that the method go is called with the variable <argument> - not what you want right? You need to pass the variable that you want to be sent to go.

public void go(string args[])  
{  
   args[0]=param1;
   args[1]=param2;

The method go indeed accepts a String array, as it should, called args - but then for some reason instead of using the input that is stored in args[0] and args[1] it simply put values called param1 and param2 in them - again not what you need. The variable args[] is given to your method, and your method can use it like any other String array - it can check the value of its cells, check the number of the cells etc'

Try to rewrite your code - pass the correct argument to go from main, and use the variables given to the go method properly.

public class IfElseDemo
{
 public void go(String args[])
  {
    String [] s = new String [2];

    s[0] = ("first");  //first element has first
    s[1] = ("firsty");  // second element has firsty
   
    if (s[0].equals(s[1])) 
     {
     System.out.println("ok");
     }
    else if (!(s[0].equals(s[1])))
     {
     System.out.println("not ok");
     }
    else
     {
     System.out.println("invalid command parameter.");
     }
  }

 public static void main(String []args)
  {
  IfElseDemo myCode = new IfElseDemo();
  myCode.go(s);  //calls myCode's go method..here i need you to dumb it up for me
                 //its not compiling,what argument am i supposed to pass method go 
                 //so that the method performs its task?
  }
}

still not working out... :(

Member Avatar for coil

Ask yourself, what exactly is the method go(...) supposed to do?

Then ask yourself, in order for the method to perform its task correctly, what information does it need? The answer to this question should reflect the parameters.

Finally, ask yourself, why are you calling this method at line 28; that is, what is the method call supposed to achieve?

Then you should be able to pass an appropriate argument.

---

For example, say you have this method:

public int sum(int number1, int number2) {
    return number1+number2;
}

1. What is the method supposed to do? Find the sum of 2 numbers.
2. What information does it need? The two numbers.
3. Why are you calling this method? To find the sum of two ints (say, 'a' and 'b')

Therefore, your arguments are 'a' and 'b', and you should call sum(a, b) . I hope that makes sense.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.