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

Thread: New Method array to String or just print as Array?

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New Method array to String or just print as Array?

    Hey guys,

    i'm sure u're able to help me. I just tried to fill an array with some numbers, calculated by a other function.
    I just tried to print this array as array, but it doesnt work.
    Maybe its just about the main method.

    public static void main(String[] args) {
        ggT(5);
        }
     
        public static int ggT(int a, int b) {
     
            while(a!=b){
                if(a>b) {
                    a=a-b;
                } else {
                    b=b-a;
                }
            }
            return a;
     
        }
     
        public static void ggT(int a) {
            int[][] array = new int[1000][2];
            for(int i=0; i<=1000; i++) {
                array[i][0] = i;
                array[i][1] = ggT(a,i);  
            } 
            for(int j=0; j<=1000; j++) {
                for(int z=0; z>=2; z++) {
                    System.out.println(array[j][z]);
     
                }
            }
     
        }

    Sry about the bad post, i just didnt find, how i can post source code properly.

    Greetings Necator


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: New Method array to String or just print as Array?

    how i can post source code properly
    Please read Announcements - What's Wrong With My Code?

    but it doesnt work.
    What 'doesn't work'? Does it compile? Exceptions? Misbehave? Being as descriptive as possible helps others pinpoint problems

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    it compiles, but it doesnt print anything. I just let it calculate for around 2 minutes. Should be enough time for this little work.

    Thx for the link to the announcement, just edited it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    A suggestion: For testing use smaller sizes like 10 vs 1000.

    Does the while loop end? Add some debug logic inside the loop to keep it from executing too many times.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    its the same result if u just take 10.
    So u dont see any problems by checking it?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    Did you try this:
    Does the while loop end? Add some debug logic inside the loop to keep it from executing too many times.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    if i just ggT(5,23), it works properly, so i dont think the while loop is the problem.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    Time to do some debugging. I use println()s. Add lots of println() statements to the code that print out the values of variables as the code executes and shows where the execution flow is going. The printout will help you find where the code is spending its time.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    ok, i tested it, the problem seems to be in this part

    for(int i=0; i<=1000; i++) {
                array[i][0] = i;
                array[i][1] = ggT(a,i);
    any suggestions?
    the ggT parts seems to calculate forever, anyone got an idea?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    ggT parts seems to calculate forever, anyone got an idea?
    Is that where the while() loop is I asked about earlier?

    Continue debugging by adding println() statements to the code in the ggT() method to see what is happening there.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    i just put some in there aswell, it seemed to compile quite right. Also if i test ggT(2,23) (with 2 parameters), so it should take the method named the same (ggT) with 2 parameters, with the while loop, it works.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    it works.
    Does that mean you have solved the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    nah, just the while loop itself works, so i guess the problem is within the call of the method.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    What did the debug println()s print out? Where did you put them in the code? Were any inside the loop?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    public static int ggT(int a, int b) {
            System.out.println(3);
            while(a!=b){
                if(a>b) {
                    a=a-b;
                } else {
                    b=b-a;
                }
            }System.out.println(4);
            return a;
     
        }
    Both worked. So the while loop should work, if not there shouldnt be the 4 printed.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    What prints out when the program is executed?
    If there is an infinite loop,
    either the printouts will go forever and what is printed will show you where the loop is
    or the prints will stop and the last thing printed will be before where the infinite loop is
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    If i just put ggT(5) in, theres printed nothing,
    if i put ggT(5,23) in (2 parameters) then both prints come once so in this case it works perfectly.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    it works perfectly.
    Does that mean your program now works?
    If not, why aren't you testing the program to see what is printed out?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Feb 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New Method array to String or just print as Array?

    i got it, i just put i=0 in the for loop, so he calculated 1-0 endless in the while loop.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: New Method array to String or just print as Array?

    So a is always > 0 and a - 0 doesn't change a
    Glad you got it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. [SOLVED] Which sort method to use for a String Array to output the index?
    By DANGEROUSSCION in forum Algorithms & Recursion
    Replies: 11
    Last Post: December 8th, 2012, 09:25 PM
  3. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 04:50 PM
  4. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 12:32 PM
  5. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM