how to send a message to receiver with if else statement
Hi all
I am currently working on a if else statement within a method and I need it to send a message to the receiver with value of the argument from the method in the message. to clarify if the value of the argument is 1, I need to make the method send the message chicken1(). The code is below:
Code :
public void countChickens(int chickens)
{
if (chickens >6)
{
this.chickens();
}
}
I have left the else part out as that is working fine. Also, this.chickens(); is my attempt to make this work but ive been working on this for ages and I cant seem to resolve the problem.
any help is very appreciated.
Re: how to send a message to receiver with if else statement
I'm not really sure what you are trying to do to be honest. Do you want to pass the number of chickens on to some other method?
// Json
Re: how to send a message to receiver with if else statement
No I dont need it to pass it on I just need it to send a message to the receiver with the value of the argument in the message. so if the value of the argument is 1 I need it to send a message to the receiver saying "chickens1()" and if the value of the argument is 2 it should be "chickens2" and so on.
hope this makes it more clear for you.
Re: how to send a message to receiver with if else statement
Do you mean somthing like this?
Code :
public void countChickens(int chickens)
{
if (chickens >6)
{
this.chickens("chickens" + Integer.toString(chickens));
}
}
Re: how to send a message to receiver with if else statement
Hi that looks like something that would work, however when i tried this the compiler came up with the error "cannot find symbol - method chickens(java.lang.string"
what I think this error is saying is that the compilere thinks chickens is a method??
hope someone can help me further. Thankyou for the help so far. I will be sure to thank everyone properly when the problem is resolved.
Re: how to send a message to receiver with if else statement
Yes, the compiler (as well as myself) thought chickens was a method, so in that case there should be a method like
Code :
public void chickens( String s ){
//do something
}
Of course whether or not there should be a chickens() method - and if so what it does - is dependent upon the behavior you want, and that I'm a bit unclear of so if you could describe what you want your code to do a bit further it may help
Re: how to send a message to receiver with if else statement
Yeah this is sort of what I meant, you could just put the number of chickens into the this.chickens method which would then print whatever message is needed.
// Json
Re: how to send a message to receiver with if else statement
Hi there, judging by humdingers other posts it looks like we're working on the same problem.
Which is the same program as http://www.javaprogrammingforums.com...tion-help.html
So far I have managed to come up with this:
I have a couple of problems with it this. First off, when i try to run it I get the following error
Quote:
Semantic error: Message ( int ) not understood by class '
Could you throw some light on what's happening here? And the best methods for tacking the problem.
Many thanks for any advice you can give.
Re: how to send a message to receiver with if else statement
Assuming that both face1() or whatever you decide to call it, and face2() etc all have the same medthod headers then this problem can be easily solved using Function objects, since Java doesn't support Function pointers.
consider the following code:
Code :
import java.util.Scanner;
public class FP {
public static void main(String[] args) {
FuncO[] y = new FuncO[2];
y[0] = new FuncO(){
@Override
public int override(int x) {
return x*x;
}
};
y[1] = new FuncO(){
@Override
public int override(int x) {
return x+x;
}
};
System.out.print("Enter 0 to square a number, 1 to add the number to its self:> ");
Scanner s = new Scanner(System.in);
System.out.println(y[s.nextInt()].override(s.nextInt()));
}
}
interface FuncO{
public int override(int x);
}
Chris
Re: how to send a message to receiver with if else statement
Hi Chris,
Thanks for taking the time to respond. Unfortunately, function objects have not been covered yet in the course, so I wouldn't be able to use them even if I knew how.:o
So far we've covered: Object concepts, variables,representations, basic methods, selection and iteration.
Re: how to send a message to receiver with if else statement
In that case, you still have the funxtions hard coded so....
Code :
switch(x){
case 1: this.chickens1(); break;
case 2: this.chickens2(); break;
case 3: this.chickens3(); break;
case 4: this.chickens4(); break;
case 5: this.chickens5(); break;
case 6: this.chickens6(); break;
default: break;
}
Chris