-1

I need to create a list of available television channels (identified by integers). I am thinking that I would begin by creating int [] list = 5,9,12,19,64. Here is the code:

public NotVeryGoodTV(int[] channels) {
    int [] list =5,9,12,19,64; 

but I am getting a syntax error stating that a { is needed after "=". I would like to have a list of tv channels that would be available to the user once they turned on their television.

qqbenq
  • 8,737
  • 3
  • 33
  • 41
mrsW
  • 49
  • 1
  • 1
  • 8

4 Answers4

4

This is syntactically correct (instead of your array declaration):

int[] list = {5, 9, 12, 19, 64};

But it is not random, if you want to create an array with random integers:

Random randGen = new Random(); //random generator: import java.util.Random;
int maxChanNumber=64; //upper bound of channel numbers (inclusive)
int minChanNumber=1; //lower bound of channel numbers (inclusive)
int amountOfChans=5; //number of channels
int[] list = new int[amountOfChans]; //create an array of the right size
for (int k=0;k<amountOfChans;k++) //populate array
    list[k]=minChanNumber+randGen.nextInt(maxChanNumber-minChanNumber+1);

Actually this codes does NOT check if you generate a different channel number (integer) for every item of the array: it is possible that in the array you will find two or more times the same number, but it is not difficult to adapt the code to avoid this, anyway the direction to take to have really random channel numbers is this one.

WoDoSc
  • 2,468
  • 1
  • 10
  • 25
3

Replace:

int [] list =5,9,12,19,64; 

With:

int[] list = { 5,9,12,19,64 };

The brackets tell Java that you are declaring a list.

However the numbers are not random; they are the ssame every time.

Anubian Noob
  • 12,897
  • 5
  • 47
  • 69
0

Yep, you need to encase the list in curly braces like this:

int [] list = {5, 9, 12, 19, 64};

Just proper Java syntax issues.

Zach
  • 467
  • 1
  • 2
  • 18
Jake
  • 11
  • 5
0

replace line 2:

int [] list =5,9,12,19,64;

with this code:

int[] list = {5,9,12,19,64};
Curlip
  • 325
  • 1
  • 2
  • 15