Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: how to send a message to receiver with if else statement

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default 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:

     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.
    Last edited by humdinger; December 2nd, 2009 at 03:55 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  3. #3
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to send a message to receiver with if else statement

    Do you mean somthing like this?
     public void countChickens(int chickens)
          {
              if  (chickens >6)
              {
                     this.chickens("chickens" + Integer.toString(chickens));
               }
           }

  5. #5
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

    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

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  8. #8
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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
    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.
    Last edited by kyuss; December 7th, 2009 at 06:13 AM.

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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:
    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

  10. The Following User Says Thank You to Freaky Chris For This Useful Post:

    kyuss (December 4th, 2009)

  11. #10
    Junior Member
    Join Date
    Dec 2009
    Posts
    13
    My Mood
    Confused
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default 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.

    So far we've covered: Object concepts, variables,representations, basic methods, selection and iteration.

  12. #11
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: how to send a message to receiver with if else statement

    In that case, you still have the funxtions hard coded so....

    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

Similar Threads

  1. Replies: 1
    Last Post: November 1st, 2009, 09:23 PM
  2. Problem on sending vectors from Java server to C# client
    By MS_Dark in forum Java Networking
    Replies: 2
    Last Post: July 7th, 2009, 02:35 PM
  3. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  4. UDP server sends thread application
    By Koren3 in forum Threads
    Replies: 2
    Last Post: April 20th, 2009, 11:46 AM
  5. SOAP Message Factory response error
    By Buglish in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 6th, 2008, 01:42 PM