Random Array DupNumb Check

Page 1 of 1

13 Replies - 807 Views - Last Post: 07 October 2012 - 10:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294622&amp;s=b921683b4f99a50f4da4d9ba209b1ad3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Random Array DupNumb Check

Posted 07 October 2012 - 08:34 AM

Hi
I'm making a program which generates random numbers in an array. A do-while method is used to check if there is any duplicated numbers while generating random in the arrays.

The problem is: I don't understand why it doesn't compile, I've tried so many times but It still doesn't work
```import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.util.Random;

public class Randomizer
{
public static void main(String args[])
{
int bank[] = new int [65];
Random generator = new Random();
int min = 100;
int max = 1000;
boolean exist;

private boolean exist(int numb){
for(int i = 0; i < bank.length; i++)
if(bank[i] = numb)
return false;
}

public void fill(){
for(int i = 0; i < bank.length; i++)
do{numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb = bank[i];
}
}
```

Tool Output
```C:\Users\Charlie Vo\Desktop\Randomizer.java:16: error: illegal start of expression
private boolean exist(int numb){
^
C:\Users\Charlie Vo\Desktop\Randomizer.java:16: error: ';' expected
private boolean exist(int numb){
^
C:\Users\Charlie Vo\Desktop\Randomizer.java:16: error: ';' expected
private boolean exist(int numb){
^
C:\Users\Charlie Vo\Desktop\Randomizer.java:23: error: illegal start of expression
public void fill(){
^
C:\Users\Charlie Vo\Desktop\Randomizer.java:23: error: illegal start of expression
public void fill(){
^
C:\Users\Charlie Vo\Desktop\Randomizer.java:23: error: ';' expected
public void fill(){
^
C:\Users\Charlie Vo\Desktop\Randomizer.java:29: error: reached end of file while parsing
}
^
7 errors

Tool completed with exit code 1

```

Is This A Good Question/Topic? 0

Replies To: Random Array DupNumb Check

#2 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 08:39 AM

You need to define your two methods fill and exist outside of the main method.

#3 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 08:43 AM

Your main method needs a closing } bracket

All paths in your exist method doesnt return a boolean

You assign bank[i] to numb instead of checking with ==
```if(bank[i] = numb)

```

Do you really want to check every index in the exist method, when only index 0-5 has been filled with values and the array is 10 in length?

```import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.util.Random;

public class Randomizer
{
public static void main(String args[])
{
int bank[] = new int [65];
Random generator = new Random();
int min = 100;
int max = 1000;
boolean exist;
}

private boolean exist(int numb){
for(int i = 0; i < bank.length; i++)
if(bank[i] == numb)
return true;
return false;
}

public void fill(){
for(int i = 0; i < bank.length; i++)
do {
numb = generator.nextInt(max - min +1);
} while(exist(numb));
//Dont you want to assign a value to bank[i] instead?
int numb = bank[i];
}
}

```

This post has been edited by CasiOo: 07 October 2012 - 08:43 AM

#4 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:20 AM

CasiOo, on 07 October 2012 - 08:43 AM, said:

Your main method needs a closing } bracket

All paths in your exist method doesnt return a boolean

You assign bank[i] to numb instead of checking with ==
```if(bank[i] = numb)

```

Do you really want to check every index in the exist method, when only index 0-5 has been filled with values and the array is 10 in length?

```import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.util.Random;

public class Randomizer
{
public static void main(String args[])
{
int bank[] = new int [65];
Random generator = new Random();
int min = 100;
int max = 1000;
boolean exist;
}

private boolean exist(int numb){
for(int i = 0; i < bank.length; i++)
if(bank[i] == numb)
return true;
return false;
}

public void fill(){
for(int i = 0; i < bank.length; i++)
do {
numb = generator.nextInt(max - min +1);
} while(exist(numb));
//Dont you want to assign a value to bank[i] instead?
int numb = bank[i];
}
}

```

Thank you guys for your kindness. Althought I was able to correct most of my flaws, but there is still something I dont quite understand. Why would you want me to assign a fixed value for bank[i] when the "exist" method is there to check the current looped index if the code is equal to any previous indexes?

#5 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:34 AM

I don't quite understand what you are asking. Is it about the int numb = bank[i]; part? You want to fill the array, but the assignment is outside your loop AND you are not putting any value in the array

#6 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:37 AM

CasiOo, on 07 October 2012 - 09:34 AM, said:

I don't quite understand what you are asking. Is it about the int numb = bank[i]; part? You want to fill the array, but the assignment is outside your loop AND you are not putting any value in the array

```	public void fill(){
for(int i = 0; i < bank.length; i++)
do{numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

```

What you are saying is: for this line of code, there is no random index input?

#7 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:39 AM

charlievo22, on 07 October 2012 - 04:37 PM, said:

CasiOo, on 07 October 2012 - 09:34 AM, said:

I don't quite understand what you are asking. Is it about the int numb = bank[i]; part? You want to fill the array, but the assignment is outside your loop AND you are not putting any value in the array

```	public void fill(){
for(int i = 0; i < bank.length; i++)
do{numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

```

What you are saying is: for this line of code, there is no random index input?

It won't even compile because of your use of bank[i] outside your loop
With proper indention it becomes quite clear

This post has been edited by CasiOo: 07 October 2012 - 09:39 AM

#8 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:51 AM

CasiOo, on 07 October 2012 - 09:39 AM, said:

charlievo22, on 07 October 2012 - 04:37 PM, said:

CasiOo, on 07 October 2012 - 09:34 AM, said:

I don't quite understand what you are asking. Is it about the int numb = bank[i]; part? You want to fill the array, but the assignment is outside your loop AND you are not putting any value in the array

```	public void fill(){
for(int i = 0; i < bank.length; i++)
do{numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

```

What you are saying is: for this line of code, there is no random index input?

It won't even compile because of your use of bank[i] outside your loop
With proper indention it becomes quite clear

Do you mean like this?

```		public void fill(){
for(int i = 0; i < bank.length ; i++){
do{
numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

}
```

I dont understand what the compiler means with
C:\Users\Charlie Vo\Desktop\Randomizer.java:30: error: ';' expected
int numb == bank[i];

Where is the ";" exspected

#9 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:53 AM

Not really too sure whats changed there.

Think about whether or not you need to use two ='s in this scenario.

#10 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:13 AM

Ryano121, on 07 October 2012 - 09:53 AM, said:

Not really too sure whats changed there.

Think about whether or not you need to use two ='s in this scenario.

can I ask you a question?

*Why do I use == instead of =? wouldn't it be the same as the "numb" value is set to bank[i] either way?

#11 Ryano121

• D.I.C Lover

Reputation: 1461
• Posts: 3,289
• Joined: 30-January 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:15 AM

No it's very different.

One = is the assignment operator so a = b would assign b to a.

Two == is the equality operator. It's used in if statements and the like to check if something equals something else

Subtle syntax difference, big overall difference

#12 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:21 AM

Ryano121, on 07 October 2012 - 10:15 AM, said:

No it's very different.

One = is the assignment operator so a = b would assign b to a.

Two == is the equality operator. It's used in if statements and the like to check if something equals something else

Subtle syntax difference, big overall difference

could you give me another clue on my fill method?

#13 charlievo22

Reputation: 0
• Posts: 12
• Joined: 12-September 12

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:37 AM

CasiOo, on 07 October 2012 - 04:39 PM, said:

charlievo22, on 07 October 2012 - 04:37 PM, said:

CasiOo, on 07 October 2012 - 09:34 AM, said:

I don't quite understand what you are asking. Is it about the int numb = bank[i]; part? You want to fill the array, but the assignment is outside your loop AND you are not putting any value in the array

```	public void fill(){
for(int i = 0; i < bank.length; i++)
do{numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

```

What you are saying is: for this line of code, there is no random index input?

It won't even compile because of your use of bank[i] outside your loop
With proper indention it becomes quite clear

Oops, requested wrong guy :1

Could you give me another clue on my "fill" method?

#14 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:45 AM

charlievo22, on 07 October 2012 - 04:51 PM, said:

CasiOo, on 07 October 2012 - 09:39 AM, said:

charlievo22, on 07 October 2012 - 04:37 PM, said:

CasiOo, on 07 October 2012 - 09:34 AM, said:

I don't quite understand what you are asking. Is it about the int numb = bank[i]; part? You want to fill the array, but the assignment is outside your loop AND you are not putting any value in the array

```	public void fill(){
for(int i = 0; i < bank.length; i++)
do{numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

```

What you are saying is: for this line of code, there is no random index input?

It won't even compile because of your use of bank[i] outside your loop
With proper indention it becomes quite clear

Do you mean like this?

```		public void fill(){
for(int i = 0; i < bank.length ; i++){
do{
numb = generator.nextInt(max - min +1);
}while(exist(numb));
int numb == bank[i];

}
```

I dont understand what the compiler means with
C:\Users\Charlie Vo\Desktop\Randomizer.java:30: error: ';' expected
int numb == bank[i];

Where is the ";" exspected

You added a starting bracket to your for-loop but you forgot the closing bracket
The closing bracket should be after the int numb == bank[i]; line

It was it in your exist method that I changed the = to ==, I was not telling you to do it in your fill method =o

Remember that you are assigning the value to the variable on the left of the = sign