# Guys, really need your help

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• June 24th, 2012, 12:58 PM
Daler
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 :(
• June 24th, 2012, 01:01 PM
Norm
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().
• June 24th, 2012, 03:46 PM
javapenguin
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) ;
• June 24th, 2012, 06:10 PM
Daler
Re: Guys, really need your help
Code java:

```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(); }   } }```
• June 24th, 2012, 06:12 PM
Norm
Re: Guys, really need your help
Is it working now?

If you get errors please copy and paste the full text here.
• June 24th, 2012, 06:17 PM
Daler
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??
• June 24th, 2012, 06:28 PM
Norm
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 =
Quote:

is it possible to fix this?
What are you trying to do in that statement?
• June 24th, 2012, 06:33 PM
Daler
Re: Guys, really need your help
Quote:

Originally Posted by Norm
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
• June 24th, 2012, 06:37 PM
Norm
Re: Guys, really need your help
Quote:

how to fix this?
You did not say what you are trying to do. The following will compile:
Code :

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

But I don't know what you are trying to do. Please explain.
• June 24th, 2012, 06:49 PM
Daler
Re: Guys, really need your help
Yes! I changed it to

Code java:

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

Code java:

```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
• June 24th, 2012, 06:56 PM
Norm
Re: Guys, really need your help
You need to explain what you are trying to do in this statement:
Code :

`int [] freq = (int) (Math.random() * 10);`

If freq must be an array, what is the purpose of the call to the random() method?
• June 24th, 2012, 06:57 PM
javapenguin
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);

int[] freq = new int[10];

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

That should get rid of those compiler errors you just posted.
• June 24th, 2012, 07:13 PM
Daler
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)
• June 24th, 2012, 08:16 PM
Norm
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
• June 24th, 2012, 09:10 PM
javapenguin
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.

Code java:

```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(); }   } }```
• June 24th, 2012, 10:19 PM
Daler
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]
• June 24th, 2012, 11:54 PM
javapenguin
Re: Guys, really need your help
Quote:

Originally Posted by javapenguin
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.

Code java:

```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.
• June 24th, 2012, 11:57 PM
Daler
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.
• June 25th, 2012, 07:49 AM
KevinWorkman
Re: Guys, really need your help
Quote:

Originally Posted by javapenguin
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.
• June 25th, 2012, 10:46 AM
copeg
Re: Guys, really need your help
Quote:

Originally Posted by javapenguin
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.
• June 25th, 2012, 06:00 PM
Daler
Re: Guys, really need your help
Can anybody explain me why javapenguin ws banned?
• June 25th, 2012, 06:57 PM
copeg
Re: Guys, really need your help
Quote:

Originally Posted by Daler
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.
• June 25th, 2012, 09:27 PM
KevinWorkman
Re: Guys, really need your help
Quote:

Originally Posted by Daler
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.
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12