13 Replies - 374 Views - Last Post: 07 October 2012 - 10:45 AM Rate Topic: -----

#1 charlievo22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 charlievo22  Icon User is offline

  • New D.I.C Head

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

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:20 AM

View PostCasiOo, 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?
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 charlievo22  Icon User is offline

  • New D.I.C Head

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

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:37 AM

View PostCasiOo, 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?
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:39 AM

View Postcharlievo22, on 07 October 2012 - 04:37 PM, said:

View PostCasiOo, 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

Was This Post Helpful? 0
  • +
  • -

#8 charlievo22  Icon User is offline

  • New D.I.C Head

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

Re: Random Array DupNumb Check

Posted 07 October 2012 - 09:51 AM

View PostCasiOo, on 07 October 2012 - 09:39 AM, said:

View Postcharlievo22, on 07 October 2012 - 04:37 PM, said:

View PostCasiOo, 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];

		}


in addition:
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
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 charlievo22  Icon User is offline

  • New D.I.C Head

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

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:13 AM

View PostRyano121, 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?
Was This Post Helpful? 0
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • 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
Was This Post Helpful? 1
  • +
  • -

#12 charlievo22  Icon User is offline

  • New D.I.C Head

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

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:21 AM

View PostRyano121, 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? :)
Was This Post Helpful? 0
  • +
  • -

#13 charlievo22  Icon User is offline

  • New D.I.C Head

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

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:37 AM

View PostCasiOo, on 07 October 2012 - 04:39 PM, said:

View Postcharlievo22, on 07 October 2012 - 04:37 PM, said:

View PostCasiOo, 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? >.<
Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1390
  • View blog
  • Posts: 3,075
  • Joined: 05-April 11

Re: Random Array DupNumb Check

Posted 07 October 2012 - 10:45 AM

View Postcharlievo22, on 07 October 2012 - 04:51 PM, said:

View PostCasiOo, on 07 October 2012 - 09:39 AM, said:

View Postcharlievo22, on 07 October 2012 - 04:37 PM, said:

View PostCasiOo, 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];

		}


in addition:
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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1