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.

Page 2 of 2 FirstFirst 12
Results 26 to 48 of 48

Thread: Guys, really need your help

  1. #26
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    It doesn't show any mistake before I launch it, but after I've entered the number I wanted to generate, it showed me this mistake


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException… -3(sometimes -4, or -6 etc.)
    at pratice.main(pratice.java:16)


    Did you find any mistake in the code? Please, I have to show it tomorrow

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Guys, really need your help

    Do you understand what values the Random class's nextInt() method returns?
    Add a println to print out the values returned by the method so you can see what it returns.

    Why did you chose that class and method? The assignment gave another class and method to use: Math.random().
    If you don't understand my answer, don't ignore it, ask a question.

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

    Daler (June 24th, 2012)

  4. #28
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Guys, really need your help

    The Random's nextInt() could return negatives. However, this might work for you.

    r.nextInt(Integer.MAX_VALUE)%10;

    However, if that generated 1000 and then 100, it would both go to 0 with that mod 10.

    You could just actually use

    r.nextInt(10); which would get numbers between 0 inclusive and 10, exclusive.

    Again, however, you can't control which random number it picks.

    Math.random() returns a double between 0, inclusive I think, and 1, exclusive I am pretty sure.

    So you could have

    (int) (Math.random() * 10) ;

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 24th, 2012)

  6. #29
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    import java.util.*;
    public class xyz
        {
        public static void main(String [] args)
             {
                   Scanner S=new Scanner(System.in);
                   System.out.println("Enter how many random numbers you want to generate");
                   int N=S.nextInt();
                   Random r = new Random();
                   int [] freq = (int) (Math.random() * 10); // <----- [COLOR="#B22222"][COLOR="#B22222"]cannot convert from int [] to int[/COLOR][/COLOR]
     
                   for(int i=0;i<10;i++)freq[i]=0;
     
                   for(int i=0;i<N;i++)
                        {
                          int k=r.nextInt()%10;
                          freq[k]++;
                         }
                   for(int i=0;i<10;i++)
                        { 
                          for(int j=0;j<freq[i];j++)System.out.print("*");
                          System.out.println();
                        }
     
             }
        }

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Guys, really need your help

    Is it working now?

    If you get errors please copy and paste the full text here.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Daler (June 24th, 2012)

  9. #31
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Norm,

    int [] freq = (int) (Math.random() * 10); // cannot convert from int[] to int

    this is the only mistake.. is it possible to fix this??

  10. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Guys, really need your help

    On the left of the = is a definition for an int array
    on the right is a single int value.

    The compiler won't like doing that assignment. It wants the same types on both sides of the =
    is it possible to fix this?
    What are you trying to do in that statement?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #33
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Quote Originally Posted by Norm View Post
    On the left of the = is a definition for an int array
    on the right is a single int value.

    The compiler won't like doing that assignment. It wants the same types on both sides of the =

    What are you trying to do in that statement?
    //Don't you mind to show me how to fix this?
    //the last line was just a comment

  12. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Guys, really need your help

    how to fix this?
    You did not say what you are trying to do. The following will compile:
    int freq = (int) (Math.random() * 10);

    But I don't know what you are trying to do. Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #35
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Yes! I changed it to

    int freq = (int) (Math.random() * 10);

    import java.util.*;
    public class xyz
        {
        public static void main(String [] args)
             {
                   Scanner S=new Scanner(System.in);
                   System.out.println("Enter how many random numbers you want to generate");
                   int N=S.nextInt();
                   Random r = new Random();
                   int freq =  (int) (Math.random() * 10);
     
                   for(int i=0;i<10;i++)freq[i]=0;  // it shows mistake here
     
                   for(int i=0;i<N;i++)
                        {
                          int k=r.nextInt()%10;
                          freq [k]++; // it shows mistake here
                         }
                   for(int i=0;i<10;i++)
                        { 
                          for(int j=0;j<freq[i];j++)System.out.print("*"); // it shows mistake here
                          System.out.println();
                        }
     
             }
        }

    So now it shows me mistakes,

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The type of the expression must be an array type but it resolved to int
    The type of the expression must be an array type but it resolved to int
    The type of the expression must be an array type but it resolved to int

  14. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Guys, really need your help

    You need to explain what you are trying to do in this statement:
    int [] freq = (int) (Math.random() * 10);

    If freq must be an array, what is the purpose of the call to the random() method?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #37
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Guys, really need your help

    Array indexes aren't valid, unless your variable happens to be of type Object, then it could be I think, if you initialized it as such, for regular int type.

    Try using a different variable than freq for that:

    int freq = (int) (Math.random() * 10);

    Try this instead perhaps:
    int[] freq = new int[10];

    int freq2 = (int) (Math.random() * 10);

    That should get rid of those compiler errors you just posted.

  16. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 24th, 2012)

  17. #38
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Now I don't see any mistakes, except after I enter the numbers

    [java=code]freq [k]++; // it shows mistake here[/code]

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -5
    at xyz.main(xyz.java:17)

  18. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,414
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Guys, really need your help

    How does k get the value of -5? You want k to have values from 0 to the size of the array -1
    If you don't understand my answer, don't ignore it, ask a question.

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

    MongooseLover (July 1st, 2012)

  20. #40
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Guys, really need your help

    r.nextInt() will return some random number that is a valid integer. That's from -2^32 +1 to 2^32 -1 I think. However, you could keep it as it but do this

    int k = Math.abs(r.nextInt()) % 10;

    That takes the absolute value of it.

    or you could do

    int k = r.nextInt(10);

    I'm not sure if it's doing what you wanted it to, but this code below will compile and, at least that I'm aware of, has no runtime errors.

    import java.util.*;
    public class xyz
        {
        public static void main(String [] args)
             {
                   Scanner S=new Scanner(System.in);
                   System.out.println("Enter how many random numbers you want to generate");
                   int N=S.nextInt();
                   Random r = new Random();
    					int[] freq = new int[10];
                   int freq2 =  (int) (Math.random() * 10);
     
                   for(int i=0;i<10;i++)freq[i]=0;  // it shows mistake here
     
                   for(int i=0;i<N;i++)
                        {
                          int k=r.nextInt(10);
                          freq [k]++; // it shows mistake here
                         }
                   for(int i=0;i<10;i++)
                        { 
                          for(int j=0;j<freq[i];j++)System.out.print("*"); // it shows mistake here
                          System.out.println();
                        }
     
             }
        }

  21. The Following 2 Users Say Thank You to javapenguin For This Useful Post:

    Daler (June 24th, 2012), MongooseLover (July 1st, 2012)

  22. #41
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    That's awesome, look at the output:

    [java=code]
    Enter how many random numbers you want to generate
    100
    0. ***********
    1. *************
    2. ********
    3. ****
    4. *********
    5. ***************
    6. **********
    7. ******
    8. *****************
    9. *******
    [/code]

  23. #42
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Guys, really need your help

    Quote Originally Posted by javapenguin View Post
    r.nextInt() will return some random number that is a valid integer. That's from -2^32 +1 to 2^32 -1 I think. However, you could keep it as it but do this

    int k = Math.abs(r.nextInt()) % 10;

    That takes the absolute value of it.

    or you could do

    int k = r.nextInt(10);

    I'm not sure if it's doing what you wanted it to, but this code below will compile and, at least that I'm aware of, has no runtime errors.

    import java.util.*;
    public class xyz
        {
        public static void main(String [] args)
             {
                   Scanner S=new Scanner(System.in);
                   System.out.println("Enter how many random numbers you want to generate");
                   int N=S.nextInt();
                   Random r = new Random();
    					int[] freq = new int[10];
                   int freq2 =  (int) (Math.random() * 10);
     
                   for(int i=0;i<10;i++)freq[i]=0;  // it shows mistake here
     
                   for(int i=0;i<N;i++)
                        {
                          int k=r.nextInt(10);
                          freq [k]++; // it shows mistake here
                         }
                   for(int i=0;i<10;i++)
                        { 
                          for(int j=0;j<freq[i];j++)System.out.print("*"); // it shows mistake here
                          System.out.println();
                        }
     
             }
        }
    When I find out when neg repped me like that, they are DEAD!

    I was NOT spoonfeeding. That code was a copy/paste pretty much of their own code a few posts earlier.

    I've reported the incident to the Administrator.

  24. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 24th, 2012)

  25. #43
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Thank you for help javapenguin and Norm! I really appreciate it... I learn a lot from both of you guys.

  26. #44
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Guys, really need your help

    Quote Originally Posted by javapenguin View Post
    When I find out when neg repped me like that, they are DEAD!

    I was NOT spoonfeeding. That code was a copy/paste pretty much of their own code a few posts earlier.

    I've reported the incident to the Administrator.
    I have no idea who "neg repped" you, what that even really means, or why you care so much. But threatening to kill somebody on a public forum is probably a bad idea. I'm not going to go back and read your post, but even copying and pasting somebody's code and fixing it for them could be considered spoonfeeding, so I don't know why you're so surprised and angry at the fact that somebody considered your post to be a case of spoonfeeding an answer without really helping- especially given your history of spoonfeeding incorrect answers.

    Also, there's really no need to use the gigantic font size. I'm pretty sure you got your point across with the death threat.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  27. #45
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Guys, really need your help

    Quote Originally Posted by javapenguin View Post
    When I find out when neg repped me like that, they are DEAD!
    Actually, threatening to kill someone on a public forum gets the said threatener banned for good.

  28. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (June 25th, 2012)

  29. #46
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Guys, really need your help

    Can anybody explain me why javapenguin ws banned?

  30. #47
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Guys, really need your help

    Quote Originally Posted by Daler View Post
    Can anybody explain me why javapenguin ws banned?
    The user has had a history of violating forum rules and conduct, has been warned of such actions, and now has threatened the life of another member - the latter of which alone in any context warrants immediate action.

  31. The Following User Says Thank You to copeg For This Useful Post:

    Daler (June 25th, 2012)

  32. #48
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Guys, really need your help

    Quote Originally Posted by Daler View Post
    Can anybody explain me why javapenguin ws banned?
    Copeg pretty much covered it already, but I'm in full support of banning him- in fact, he should have been banned months ago, but we kept giving him a lot of leeway because he does have a genuine energy for these forums.

    But that energy is oftentimes expressed by giving out incorrect answers, asking questions in a way that isn't really answerable (posting a thousand lines of code and saying "how do I fix this" without actually telling us what's wrong, then ignoring the help that he does receive), handing out spoonfed code that is actually blatantly wrong, containing logic errors and misleading the people trying to get help- I am sure he has resulted in more than a few failed assignments when students hand in his code because novice programmers don't know that his code is wrong.

    So it becomes our job to babysit him, correcting his posts and sending him literally dozens of warnings (all of which he ignores) about this kind of stuff- and the time we spend on that would be much better spent actually helping people, maintaining the forum, answering questions, etc.

    On top of all of that, he just threatened to kill somebody. Whether he actually literally meant it or not is a bit irrelevant- this is a technical forum, so we don't have time for his tantrums, especially those that result in somebody else receiving a death threat. We absolutely aren't going to accept that kind of behavior.

    Like I said, this is a technical forum for exchanging technical information. Death threats have absolutely no place in that environment.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  33. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    copeg (June 25th, 2012), Daler (June 25th, 2012)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. HI guys
    By nchmurali in forum Member Introductions
    Replies: 2
    Last Post: December 15th, 2011, 01:43 AM
  2. hi guys
    By johnyabu@gmail.com in forum Member Introductions
    Replies: 1
    Last Post: February 2nd, 2011, 11:13 AM
  3. Hey guys
    By StarKannon in forum Member Introductions
    Replies: 2
    Last Post: February 2nd, 2011, 03:58 AM
  4. Hi Guys
    By dazzl3r in forum Member Introductions
    Replies: 1
    Last Post: December 7th, 2010, 03:04 PM
  5. Hi guys
    By nextngema in forum Java SE APIs
    Replies: 2
    Last Post: July 17th, 2010, 04:34 PM