Results 1 to 13 of 13
  1. #1
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Exclamation Wrong Output (Java Program)

    hi guys... this is my program and i dont know what is wrong:S
    The problem is in the main i guess..Can you guys help me please? thanks for everything

    public class Xiu {
    public static void main (String []args)
    {
    int max = 50;
    System.out.print(enigma(max)+ " ");
    }
    public static boolean[] enigma(int max) {
    boolean[] res = new boolean[max+1];
    for (int i = 0; i < 2; i++) res[i] = false;
    for (int i = 2; i < res.length; i++) res[i] = true;
    for (int i = 2; i < res.length; i++)
    if (res[i])
    for (int j = i; i * j < res.length; j++)
    res[i * j] = false;
    return res;
    }
    }

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    What are you expecting it to do? What is it doing differently to that?

    Also, it would help if you used the [code] tags to format your code.

  3. #3
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    i dont know to use code tags for now :S What i want to do is put the method enigma working :S It should give false in 0 and 1 index and then false in i x i index (ex: 2 x 2, 3 x 3, ...) until max + 1. The rest index are true.. I know the method is right, the problem is how to put it working in the main :S

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You have to iterate through every element and print it out. What you're probably getting as output is something like this:
    @245ae567b76
    That's the memory location of the array.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    yes moonchile im getting that output...i put a for () but still not working

    Java Code:
    public class Xiu {
    public static void main (String []args)
    {
    int max = 50;
    for (int i = 0; i < max+1; i++)
    System.out.print(enigma(max)+ " ");
    }
    public static boolean[] enigma(int max) {
    boolean[] res = new boolean[max+1];
    for (int i = 0; i < 2; i++) res[i] = false;
    for (int i = 2; i < res.length; i++) res[i] = true;
    for (int i = 2; i < res.length; i++)
    if (res[i])
    for (int j = i; i * j < res.length; j++)
    res[i * j] = false;
    return res;
    }
    }
    Last edited by Fubarable; 11-27-2010 at 03:07 PM. Reason: Moderator Edit: code tags added

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by poupas View Post
    i dont know to use code tags for now...
    Many of us here will wait for you to learn. To use them, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've added tags to your post above, but the code in your post wasn't formatted to begin with, and tags won't magically make unformatted code formatted. Also, you need to tell us more, since "it still doesn't work" doesn't tell us much about how it's currently not working.

  8. #8
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    ok guys i got it to work.. Thanks for everything.

    The progrmam should be like this:


    public class Xiu {
    public static void main (String []args)
    {
    int max = 50;
    for (int i = 0;i< max+1; ++i)
    {
    System.out.println ((enigma(max)[i]));
    }
    }
    public static boolean[] enigma(int max)
    {
    boolean[] res = new boolean[max+1];
    for (int i = 0; i < 2; i++) res[i] = false;
    for (int i = 2; i < res.length; i++) res[i] = true;
    for (int i = 2; i < res.length; i++)
    if (res[i])
    for (int j = i; i * j < res.length; j++)
    res[i * j] = false;
    return res;
    }
    }

    OutPut: Prime Numbers (True) until max;

  9. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by poupas View Post
    ok guys i got it to work.. Thanks for everything.

    The progrmam should be like this:


    public class Xiu {
    public static void main (String []args)
    {
    int max = 50;
    for (int i = 0;i< max+1; ++i)
    {
    System.out.println ((enigma(max)[i]));
    }
    }
    public static boolean[] enigma(int max)
    {
    boolean[] res = new boolean[max+1];
    for (int i = 0; i < 2; i++) res[i] = false;
    for (int i = 2; i < res.length; i++) res[i] = true;
    for (int i = 2; i < res.length; i++)
    if (res[i])
    for (int j = i; i * j < res.length; j++)
    res[i * j] = false;
    return res;
    }
    }

    OutPut: Prime Numbers (True) until max;
    Why don't you save the result array of the enigma method? The way you've written your method, you call the enigma method max+1 times, which is horribly inefficient.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Fubarable View Post
    I've added tags to your post above, but the code in your post wasn't formatted to begin with, and tags won't magically make unformatted code formatted. Also, you need to tell us more, since "it still doesn't work" doesn't tell us much about how it's currently not working.
    The OP is trying to implement a Sieve of Erastosthenes; that works, just the printing of the boolean array fails. hint for the OP: use Arrays.toString(res) and print that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    Why don't you save the result array of the enigma method? The way you've written your method, you call the enigma method max+1 times, which is horribly inefficient.
    I cant change my method because the exercise im doing told me that i must not change the method :X sry for my english xP

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by poupas View Post
    I cant change my method because the exercise im doing told me that i must not change the method :X sry for my english xP
    Still then ... why don't you simply do this:

    Java Code:
    boolean[] result= enigma(max); // calculate the result once
    for (int i= 0; i < result.length; i++)
       System.out.println(result[i]);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    poupas is offline Member
    Join Date
    Nov 2010
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Still then ... why don't you simply do this:

    Java Code:
    boolean[] result= enigma(max); // calculate the result once
    for (int i= 0; i < result.length; i++)
       System.out.println(result[i]);
    oh yeah, you re right ^^
    much easier :X Thanks ^^

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. Redirecting output of java program to file
    By dewitrydan in forum New To Java
    Replies: 4
    Last Post: 11-17-2010, 07:58 AM
  3. Whats Wrong I m nt Getting output -please please help
    By divakantdinesh in forum New To Java
    Replies: 3
    Last Post: 10-16-2010, 10:21 PM
  4. the explanation of output of simple java program
    By amol84 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 06:06 PM
  5. what is wrong with this program ?
    By Poor Bee in forum New To Java
    Replies: 1
    Last Post: 05-07-2008, 08:23 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
  •