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 23 of 23

Thread: Java access to another method

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Lightbulb Java access to another method

    String[] = Questions{};
    String[] = Answers1{};
    String[] = correctAnswers{};
     
    public void onGuildMessageReceived(GuildMessageReceivedEvent event){
            String[] args = event.getMessage().getContentRaw().split("\\s+");
     
                if (args[0].equalsIgnoreCase(Quiz.prefix + "Question")){
     
                    Random rnd = new Random();
                    int randomnumber = rnd.nextInt(Questions.length);
     
     
     
                    EmbedBuilder Question = new EmbedBuilder();
                    Question.setTitle("📋 " + Questions[randomnumber]);
                    Question.setDescription(Answers[randomnumber]);
                    Question.setColor(0x00ff99);
     
                    event.getChannel().sendTyping().queue();
                    Message msg=event.getChannel().sendMessage(Question.build()).complete();
                    msg.addReaction("🥇").queue();
                    msg.addReaction("🥈").queue();
                    msg.addReaction("🥉").queue();
                    Question.clear();
     
     
            }
        }
     
    public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
         if(Answers1[randomnumber].equals(correctAnswers[randomnumber]){}
    }
    Hi i want to ask how to use int 'randomnumber' from method 'onGuildMessageReceived' to method 'onGuildMessageReactionAdd'
    thanks for any answers
    Last edited by Adam5981; November 25th, 2020 at 09:14 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    how to use int 'randomnumber'
    Either declare it at the class level so all methods can see it
    or pass it as an argument to the method.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    i just edited post

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    i just edited post
    What did you change? I do not see the code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    i edited code tags

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    Ok. That looks better.
    Did you try one of the solutions I suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    Idk how to do it

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    Which one do you want to do?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    Class level

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    Make a declaration for that variable that is outside of any method but within the definition for the class.
    Remove the variable's declaration from inside of the 'onGuildMessageReceived' method. The code in the 'onGuildMessageReceived' method should use the class level variable instead of a local variable (One defined inside of the method).
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    ye but im changing vaule of this integer in first method and want use same value which is generated there in another method, is it possible?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    want use same value which is generated there in another method, is it possible?
    Yes.
    If a method stores a value in a class level variable, that value will be available to any method in the class until some method changes it to a new value.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

     public int gene(){
            Random rrr = new Random();
            int number = rrr.nextInt(otazky.length);
            return number;
        }
    i did this in class so i can use it in all methods but when i use it in 2 differrent methods it dont have same value

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    That method has only local variables which means no other method can see their values. rrr and number are locally defined in the gene method.
    Their values are not accessible outside of the gene method.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    Quote Originally Posted by Norm View Post
    That method has only local variables which means no other method can see their values. rrr and number are locally defined in the gene method.
    Their values are not accessible outside of the gene method.
    but when i use gene() it will return me int number but when i call it again it wont be same value , can i do it somehow?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    when i use gene() it will return me int number but when i call it again it wont be same value
    Yes, that is how it is coded. Each call will get a new number. That is what the nextInt method does.

    can i do it somehow?
    What do you want the gene method to do? Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    Quote Originally Posted by Adam5981 View Post
    String[] = Questions{};
    String[] = Answers1{};
    String[] = Answers2{};
    String[] = Answers3{};
    String[] = correctAnswers{};
     
    public void onGuildMessageReceived(GuildMessageReceivedEvent event){
            String[] args = event.getMessage().getContentRaw().split("\\s+");
     
                if (args[0].equalsIgnoreCase(Quiz.prefix + "Question")){
     
                    Random rnd = new Random();
                    int randomnumber = rnd.nextInt(Questions.length);
     
     
     
                    EmbedBuilder Question = new EmbedBuilder();
                    Question.setTitle("📋 " + Questions[randomnumber]);
                    Question.setDescription(Answers[randomnumber]);
                    Question.setColor(0x00ff99);
     
                    event.getChannel().sendTyping().queue();
                    Message msg=event.getChannel().sendMessage(Question.build()).complete();
                    msg.addReaction("🥇").queue();
                    msg.addReaction("🥈").queue();
                    msg.addReaction("🥉").queue();
                    Question.clear();
     
     
            }
        }
     
    public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
            public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
     
            if (event.getReactionEmote().getName().equals("🥇") && !event.getMember().getUser().equals(event.getJDA().getSelfUser())) {
                if (event.getMember().getUser().equals(event.getChannel().retrieveMessageById(event.getMessageId()).complete().getAuthor())) {
     
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().delete().queue();
     
                } else if(Answers1[randomnumber].equals(correctAnswers[randomnumber])) {
     
                    EmbedBuilder info = new EmbedBuilder();
                    info.setTitle("Correct");
                    info.setColor(0x03fc0f);
     
                    event.getChannel().sendTyping().queue();
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().getChannel().sendMessage(info.build()).queue();
                    info.clear();
     
                }
            }else if (event.getReactionEmote().getName().equals("🥈") && !event.getMember().getUser().equals(event.getJDA().getSelfUser())) {
                if (event.getMember().getUser().equals(event.getChannel().retrieveMessageById(event.getMessageId()).complete().getAuthor())) {
     
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().delete().queue();
     
                } else if(Answers2[randomnumber].equals(correctAnswers[randomnumber])){
                    EmbedBuilder info = new EmbedBuilder();
                    info.setTitle("Correct");
                    info.setColor(0x03fc00);
     
                    event.getChannel().sendTyping().queue();
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().getChannel().sendMessage(info.build()).queue();
                    info.clear();
     
                }
            }else if (event.getReactionEmote().getName().equals("🥉") && !event.getMember().getUser().equals(event.getJDA().getSelfUser())) {
                if (event.getMember().getUser().equals(event.getChannel().retrieveMessageById(event.getMessageId()).complete().getAuthor())) {
     
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().delete().queue();
     
                } else if(Answers3[randomnumber].equals(correctAnswers[randomnumber])){
                    EmbedBuilder info = new EmbedBuilder();
                    info.setTitle("Správne");
                    info.setColor(0x03fc01);
     
                    event.getChannel().sendTyping().queue();
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().getChannel().sendMessage(info.build()).queue();
                    info.clear();
     
                }
            }
    }
    Hi i want to ask how to use int 'randomnumber' from method 'onGuildMessageReceived' to method 'onGuildMessageReactionAdd'
    thanks for any answers
    if you look to my program you see that i want to compare 2 arrays but i cant cuz i need index which is used in first method as randomnumber but java dont allow it to me.

    this project should be QUIZ
    Last edited by Adam5981; November 25th, 2020 at 11:03 AM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    Here is an example of how to declare a variable at the class level:
    class Testing {
       int aClassLevelVar;   // declare a class level variable
     
      public void someMethod() {
         int aLocalVar = 1;           // declare local variable
         aClassLevelVar = 12; // assign a value to a class level variable
        ...
      }
      public void anotherMethod() {
         int aLocalVar = 2;   // declare a new local variable, nothing to do with other by same name
         ...
         int anotherLocalVar = aClassLevelVar + 1;  // use the class level variable's value
       }
    }

    Try declaring randomnumber as a class level variable.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    i just updated my code

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    i just updated my code
    Where? Did you post the new version?

    Does it work now?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    String[] = Questions{};
    String[] = Answers1{};
    String[] = Answers2{};
    String[] = Answers3{};
    String[] = correctAnswers{};
     
    public class Question extends ListenerAdapter{
     
    public void onGuildMessageReceived(GuildMessageReceivedEvent event){
            String[] args = event.getMessage().getContentRaw().split("\\s+");
     
                if (args[0].equalsIgnoreCase(Quiz.prefix + "Question")){
     
                    Random rnd = new Random();
                    int randomnumber = rnd.nextInt(Questions.length);
     
     
     
                    EmbedBuilder Question = new EmbedBuilder();
                    Question.setTitle("📋 " + Questions[randomnumber]);
                    Question.setDescription(Answers[randomnumber]);
                    Question.setColor(0x00ff99);
     
                    event.getChannel().sendTyping().queue();
                    Message msg=event.getChannel().sendMessage(Question.build()).complete();
                    msg.addReaction("🥇").queue();
                    msg.addReaction("🥈").queue();
                    msg.addReaction("🥉").queue();
                    Question.clear();
     
     
            }
        }
     
    public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
            public void onGuildMessageReactionAdd(GuildMessageReactionAddEvent event) {
     
            if (event.getReactionEmote().getName().equals("🥇") && !event.getMember().getUser().equals(event.getJDA().getSelfUser())) {
                if (event.getMember().getUser().equals(event.getChannel().retrieveMessageById(event.getMessageId()).complete().getAuthor())) {
     
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().delete().queue();
     
                } else if(Answers1[randomnumber].equals(correctAnswers[randomnumber])) {
     
                    EmbedBuilder info = new EmbedBuilder();
                    info.setTitle("Correct");
                    info.setColor(0x03fc0f);
     
                    event.getChannel().sendTyping().queue();
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().getChannel().sendMessage(info.build()).queue();
                    info.clear();
     
                }
            }else if (event.getReactionEmote().getName().equals("🥈") && !event.getMember().getUser().equals(event.getJDA().getSelfUser())) {
                if (event.getMember().getUser().equals(event.getChannel().retrieveMessageById(event.getMessageId()).complete().getAuthor())) {
     
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().delete().queue();
     
                } else if(Answers2[randomnumber].equals(correctAnswers[randomnumber])){
                    EmbedBuilder info = new EmbedBuilder();
                    info.setTitle("Correct");
                    info.setColor(0x03fc00);
     
                    event.getChannel().sendTyping().queue();
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().getChannel().sendMessage(info.build()).queue();
                    info.clear();
     
                }
            }else if (event.getReactionEmote().getName().equals("🥉") && !event.getMember().getUser().equals(event.getJDA().getSelfUser())) {
                if (event.getMember().getUser().equals(event.getChannel().retrieveMessageById(event.getMessageId()).complete().getAuthor())) {
     
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().delete().queue();
     
                } else if(Answers3[randomnumber].equals(correctAnswers[randomnumber])){
                    EmbedBuilder info = new EmbedBuilder();
                    info.setTitle("Správne");
                    info.setColor(0x03fc01);
     
                    event.getChannel().sendTyping().queue();
                    event.getChannel().retrieveMessageById(event.getMessageId()).complete().getChannel().sendMessage(info.build()).queue();
                    info.clear();
     
                }
            }
       }
    }

    only int randomnumber dont work which is used in 2 method

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java access to another method

    int randomnumber dont work
    Look at my post#18 to see how to code and use a class level variable.
    Your code still declares randomnumber as local to the onGuildMessageReceived method.

    Also see the tutorial: https://docs.oracle.com/javase/tutor...variables.html
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    Adam5981 (November 25th, 2020)

  24. #23
    Junior Member
    Join Date
    Nov 2020
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java access to another method

    i will try something but thanks for now

Similar Threads

  1. Run() method of runnable function has no access to data members?
    By ineedahero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 9th, 2013, 05:31 PM
  2. [SOLVED] How can I access my secondary interface method in my for loop?
    By bdennin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 12th, 2013, 06:29 PM
  3. Replies: 2
    Last Post: February 12th, 2012, 07:35 AM
  4. Access Method
    By CompScienceStudent1 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 21st, 2011, 08:56 AM
  5. Method access problem
    By hello_world in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 20th, 2011, 03:39 PM