Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Java Methods help!

  1. #1
    Join Date
    May 2015
    Posts
    1
    Rep Power
    0

    Default Java Methods help!

    I am having trouble with methods.

    What I want to do is be able to create 4 types of strings under the same method, but only draw one of them at a time.

    i.e

    UI.initialise;
    UI.addButton ("pipe", this::drain);

    public void drain(){

    this.pipe ("pipe1");
    this.pipe ("pipe2");
    this.pipe ("pipe3");
    this.pipe ("pipe4");

    }

    static void pipe(string pipe) {

    ui.printf ("s, \n", (pipe));

    }

    When I press the button "drain" it will print=
    pipe1
    pipe2
    pipe3
    pipe4

    Im having alot of difficulty just printing one out after each time I press drain.
    "drain"
    pipe1
    "drain"
    pipe2 etc..


    Sorry If this is a rookie question. Still learning

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java Methods help!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Java Methods help!

    Hi,

    I'm not particularly experienced myself so I apologize if my solution doesn't make sense :)

    why not try adding a counter and have the method print "pipe"+counter, then add 1 to the counter.
    When the counter is at 4 before printing, reset it to 1.

    So step by step:
    1. make a counter int that's initialized at 1.
    2. when you press the button have it check if the counter is 4.
    3. if the counter is not 4, have the method print "pipe"+counter.
    4. then add 1 to the counter.

    if the counter is 4 at the first step, make the method set it back to 1 before printing.

    hope that helps :)

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java Methods help!

    Quote Originally Posted by slimjimwittywoo View Post
    UI.initialise;
    UI.addButton ("pipe", this::drain);
    What is this, C# code?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Java Methods help!

    Quote Originally Posted by gimbal2 View Post
    What is this, C# code?
    Method references were added in Java 8.
    Method References (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java Methods help!

    Why not store each instance of a pipe in an array or list? Then use the button to modify the index to access the particular pipe instance.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Java Methods help!

    Quote Originally Posted by DarrylBurke View Post
    Darnit, I really need to commit all the new features to memory.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  5. Replies: 1
    Last Post: 10-23-2011, 12:37 PM

Posting Permissions

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