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

Thread: Problem with code, not well result loops

  1. #1
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with code, not well result loops

    Hello guys,
    I would like to write a code which look for if elements exist in other table so poszukiwane in dane.
    If three or more elements are the same it will outputs true, else false.

    1. My output count always only once and gives false all the time. How to fix it?

    2. I wanted to make it with public static boolean MyMethod(char[] dane, char[] poszukiwane), but I'm not sure how to make, can you suggest sth?

    public class Testjava {
     
        public static void main(String[] args) {
     
           char[] dane = new char[]{1, 2, 4, 6, 7, 8, 15};
            char[] poszukiwane = new char[]{1, 2, 4, 7, 15};
     
            int count = 0;
            for (int i = 0; i < dane.length; i++) {
                for (int j = 0; i < poszukiwane.length; i++) {
     
                    if (poszukiwane[j] == dane[i]) {
                        count = count + 1;
     
                    } else {
                        ;
                    }
                }
            }
            System.out.println(count);
            if (count >= 3) {
                System.out.println("True");
            } else {
                System.out.println("False");
            }
     
        }
    }

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

    Default Re: Problem with code, not well result loops

    How are you trying to debug the code?
    I suggest adding print statements that show the values of the variables as they are used and changed.
    For example print the values of i, j, count, poszukiwane[j] and dane[i] just before the if statement so you can see what the computer sees when the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with code, not well result loops

    My bad....
    for (int j = 0; j < poszukiwane.length; j++)
    I changed it for j seconds ago..
    Thanks

    Do you have any idea how to implement this with public static boolean?

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

    Default Re: Problem with code, not well result loops

    implement this with public static boolean?
    Please explain what you want to declare with those attributes.
    Why make it public?
    Why make it static?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with code, not well result loops

    Im not sure about why we shoould make like this, it's is my training and excersise to do with using this public static boolean.

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

    Default Re: Problem with code, not well result loops

    Are you asking about creating a method that is public and static and returns a boolean value?
    What is the method supposed to do?
    What arguments does it need to do its job?

    See the tutorial on methods: https://docs.oracle.com/javase/tutor...O/methods.html

    See the Math class for some static method examples.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with code, not well result loops

    Ok, thanks I'll read about that

Similar Threads

  1. can you please run this code and tell me the result?
    By Denissio in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 9th, 2020, 05:53 PM
  2. How do you code to compare result?
    By shin777 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2013, 09:09 AM
  3. problem with my code (FOR LOOPS) neeed help plZ
    By jessy sonita in forum Loops & Control Statements
    Replies: 11
    Last Post: December 12th, 2011, 11:22 AM
  4. Problem of getting result than SQL to JTable
    By MS_Dark in forum Exceptions
    Replies: 1
    Last Post: March 10th, 2009, 06:26 AM