# Put a random number from one vector to another

• October 4th, 2013, 03:47 PM
JohnJohnson123
Put a random number from one vector to another
I have a problem with this code. The program is supposed to read a vector with Standard Input and choose randomly from that vector, put those numbers in another array and then print that array out. How large the first array is depends on N, from args, the size of the sample vector is M. Vector M is not supposed to choose the same number twice.
It seems that the calling method does'nt work from main readInt and to static readInt method and I don't know how to fix it.

Code Java:

```public class Permexamp{   //Reading the vector with StdIn public static int [] readInt(int N) { int [] vectora = new int [N]; for (int i = 0;i<N;i++){ vectora [i] = StdIn.readInt(); } return vectora;   }   //Building a now vector chosen randomly from vectora, not choosing the same number 2 public static int[] randomchoose(int [] a, int M) { int [] vectorb = new int [M]; for (int i = 0; i < M; i++) { int r = i + (int) (Math.random() * (M-i)); int t = vectorb[r]; vectorb[r] = vectorb[i]; vectorb[i] = t; } return vectorb; } //Main call's the other methods and prints out vectorb public static void main(String args[]) { int N = Integer.parseInt(args[0]); int M = Integer.parseInt(args[1]); int[] vectora = readInt(N); int[] vectorb = randomchoose(vectora,M);   for (int i = 0; i < M ; i++) StdOut.println(vectorb[i]); } }```

When I compile it there are no errors. But the program seems to be in infenite loop. I do ctr Z to stop the program I get this error:

^Z
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Permexamp.main(Permexamp.java:29)
• October 4th, 2013, 03:59 PM
GregBrannon
Re: Put a random number from one vector to another
Give us example values for M and N.

Are you sure the program isn't just waiting for data entry from the keyboard? Have you tried entering values after running? It might be helpful to add a prompt to the readInt() method.

Also, it's not a great practice to name your own methods the same as existing methods in the libraries you're using, even if the signatures are not the same. It's confusing, and the confusion is unnecessary.
• October 4th, 2013, 05:52 PM
JohnJohnson123
Re: Put a random number from one vector to another
Example values: Well the Vectora is supposed to be larger, because that is the array wich M is suppose to read from. So N>M, hopefully. I tried with 5 and 2 from the command line, nothing happened, I had the same error as before. Perhaps I should try other values...I think it has something to do with exactly that line you mentioned.

--- Update ---

update:
line 14:public static int[] randomchoose(int [] vectora, int M) {

--- Update ---

This is where I'm at now. My problem is in line 8 and I dont know how to fix it...

Code Java:

```  public class Permexamp{   //Reading the vector with StdIn public static int [] readfromargs(int N) { int [] vectora = new int [N]; for (int i = 0;i<N;i++){ vectora [i] = Integer.parseInt(args[i]);//StdIn.readInt(); } return vectora; }   //Building a now vector chosen randomly from vectora, not choosing the same number 2 public static int[] randomchoose(int [] vectora, int M) { int [] vectorb = new int [M]; for (int i = 0; i < M; i++) { int r = i + (int) (Math.random() * (M-i)); int t = vectorb[r]; vectorb[r] = vectorb[i]; vectorb[i] = t; } return vectorb; } //Main call's the other methods and prints out vectorb public static void main(String args[]) { int N = Integer.parseInt(args[0]); int M = Integer.parseInt(args[1]); int[] vectora = readfromargs(N); int[] vectorb = randomchoose(vectora,M);   for (int i = 0; i < M ; i++) System.out.println(vectorb[i]); //StdOut.println(vectorb[i]); } }```

error message:

Permexamp.java:8: error: cannot find symbol
^
symbol: variable args
location: class Permexamp
• October 4th, 2013, 06:04 PM
GregBrannon
Re: Put a random number from one vector to another
The array args[] is local to the main() method, not within the scope of other methods even in the same class. Any values in args[] (or args itself) that are needed should be stored in other variables and passed to methods as needed.
• October 4th, 2013, 06:17 PM
JohnJohnson123
Re: Put a random number from one vector to another
I don't get it. How do I make it public? (Do you put it like that?)
• October 4th, 2013, 06:27 PM
GregBrannon
Re: Put a random number from one vector to another
As I said pretty clearly, the necessary values or the whole array itself could be passed to the method:

Change the call in main() to:
int[] vectora = readfromargs( N, args );

And change the method signature to:
public static int [] readfromargs(int N, String[] args )

BTW, to comply with Java's naming conventions, your method names should be: readFromArgs() and randomChoose().
• October 4th, 2013, 06:42 PM
JohnJohnson123
Re: Put a random number from one vector to another
Thanks man.

That goes a long way.

I still get this error message. I'll look in tomorrow.

>java Permexamp 5 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Permexamp.main(Permexamp.java:32)
• October 5th, 2013, 03:36 AM
GregBrannon
Re: Put a random number from one vector to another
Okay, but I'm not sure it's the right way. If args[] only contains two values M and N, and they've already been extracted and parsed to the desired values, I'm not sure why you'd pass the args[] array around. Pass M and N around and leave args[] out of it. Further, if args[] contains fewer elements than vectors[], they can't be compared element-by-element as is being done in readFromArgs();
• October 5th, 2013, 03:54 AM
JohnJohnson123
Re: Put a random number from one vector to another
I think the problem is that I'm not thinking this correctly. If I put for example 5 and 2 in command line the vectora.length(N) is 5. Then the program starts to parse(move) 5 and 2 into vectora but as string.args length (not sure if that is the correct saying...) is only two (M) the array is ''out of bounds''.

Now...if N=4 and M=2, vectora is like{1,2,3,3} how do I create M from the command line?
• October 5th, 2013, 04:02 AM
GregBrannon
Re: Put a random number from one vector to another
You've analyzed the source of the error correctly: can't cram 10 pounds into a 5 pound sack. I also think the two arrays hold different types of data, and that's a problem. And I agree with your realization that the design needs to be modified. What changes are you considering?
• October 5th, 2013, 04:49 AM
JohnJohnson123
Re: Put a random number from one vector to another
I'm thinking...two things perhaps I can ''ask'' first for N and then do system out print to ask for M. Or keep the code that I've got[ain't that just the code of my dreams...hahah] and create N numbers with math.random....

update:

The N is supposed to come from command line, just some normal vector like {1,2,3,3}....
• October 5th, 2013, 05:12 AM
GregBrannon
Re: Put a random number from one vector to another
I suggest falling back and regrouping with a minor rethink, starting with the requirements. Sometimes we get set on a programming course not recognizing that we've either set out in the wrong direction to begin with or we've taken a wrong turn along the way, forgetting entirely where we're headed but pushing ahead undaunted.

1. read a vector with Standard Input of size N which is obtained from args[]
2. choose randomly from that vector
3. put those numbers (from the chosen vector) in another array of size M from args[], M <= N
4. print that (the other) array out.
5. the second vector M is not supposed to choose the same number twice (not contain duplicate numbers.)

General comment: The assignment specifically states that a Vector is to be used which is a specific collection in Java, yet you're using arrays as vectors. Why is that? args[] is not and cannot be a Vector.

Step 1: Obtain N and M from args[] and ask the user to provide N values to fill the first Vector. The readFromArgs() method is misguided.
Steps 2, 3, and 5: choose M values randomly from the first vector and store those in a second vector, ensuring no duplicates are chosen.
Step 4: Print the resulting second array.

Now apply the rethink to some code changes.
• October 5th, 2013, 08:05 AM
JohnJohnson123
Re: Put a random number from one vector to another
I tried some changes at the bottom, I tried changing the sequence of the creation of the arrays. With the input {2,2,2,2} the program prints out:0
0

Code Java:

```public class Permexamp{   //Reading the vector with StdIn public static int [] readFromArgs(int N, String[] args) { //int N = args.lenght; int [] vectora = new int [N]; for (int i = 0;i<vectora.length;i++){   vectora [i] = Integer.parseInt(args [i]);//StdIn.readInt(); } return vectora; }   //Building a now vector chosen randomly from vectora, not choosing the same number 2 public static int[] randomChoose(int [] vectora, int M) { int [] vectorb = new int [M]; for (int i = 0; i < M; i++) { int r = i + (int) (Math.random() * (M-i)); int t = vectorb[r]; vectorb[r] = vectorb[i]; vectorb[i] = t; } return vectorb; } //Main call's the other methods and prints out vectorb public static void main(String args[]) { int N = Integer.parseInt(args[0]); //int M = Integer.parseInt(args[1]); int[] vectora = readFromArgs( N,args); int M = Integer.parseInt(args[1]); int[] vectorb = randomChoose(vectora,M);   for (int i = 0; i < M ; i++) System.out.println(vectorb[i]); //StdOut.println(vectorb[i]); } }```

I was just seeing your last post. Yeas I'll sit down and think this over a bit.

--- Update ---

You're absolutely correct.

--- Update ---

So it should perhaps be something like this? I'm...having trouble with step 1. I need N and M [to bad it's not M&M] and N-values/vector?

Code Java:

```public static void main(String args[]) { int M = Integer.parseInt(args[0]);//Choose from these numbers int N = Integer.parseInt(args[1]);//This many numbers int[] vectora = readFromArgs( N,args ); int[] vectorb = randomChoose(vectora,M);   for (int i = 0; i < M ; i++) System.out.println(vectorb[i]); //StdOut.println(vectorb[i]); } }```

--- Update ---

Could it be like N= vectora and the length of that vector=how many numbers to choose from?
• October 5th, 2013, 08:16 AM
GregBrannon
Re: Put a random number from one vector to another
Something more like this:
Code java:

```public static void main(String args[]) { // i think you have M and N backwards, but I may not understand your comments int M = Integer.parseInt(args[0]);//Choose from these numbers int N = Integer.parseInt(args[1]);//This many numbers   // fills vectora with N values supplied by the user int[] vectora = getUserInput( N );   int[] vectorb = randomChoose( vectora, M );   // output the resulting vector } }```
I'm getting the impression you think the values of vectora come from args[], but I think the user enters those values later.
• October 5th, 2013, 09:06 AM
JohnJohnson123
Re: Put a random number from one vector to another
I'm lost...

Code Java:

```public class Permexamp1{   //Reading the vector with StdIn public static int [] getUserInput(int N) { //int N = args.lenght; int [] vectora = new int [N]; for (int i = 0;i<vectora.length;i++){   vectora [i] =Integer.parseInt(args[i]);// StdIn.readInt(); } return vectora; }   //Building a now vector chosen randomly from vectora, not choosing the same number 2 public static int[] randomChoose(int [] vectora, int N) { int [] vectorb = new int [M]; for (int i = 0; i < N; i++) { int r = i + (int) (Math.random() * (N-i)); int t = vectorb[r]; vectorb[r] = vectorb[i]; vectorb[i] = t; } return vectorb; } //Main call's the other methods and prints out vectorb public static void main(String args[]) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); int[] vectora = getUserInput(N ); int[] vectorb = randomChoose(vectora,M);   for (int i = 0; i < M ; i++) System.out.println(vectorb[i]); //StdOut.println(vectorb[i]); } }```

Compile it in Jeliot. Input: 5 5

Error:Execution Error
Line 12, Column 51:
Undefined class or field 'args'.
• October 5th, 2013, 09:22 AM
GregBrannon
Re: Put a random number from one vector to another
You are where you were several posts ago. Same code, same errors, different method names. Short of writing this for you, I'm not sure how to reboot you so that you start rethinking what you're doing, because this isn't hacking it. args[] has nothing to do with getting user input.

getUserInput() should look something like this:
Code java:

```//Reading the vector with StdIn public static int[] getUserInput( int N ) { Scanner input = new Scanner( System.in );   //int N = args.lenght; int[] vectora = new int[N];   for (int i = 0 ; i < vectora.length ; i++ ) { System.out.print( "Enter element " + ( i + 1 ) + ": " ); vectora[i] = input.nextInt();// StdIn.readInt();   // read the linefeed and throw it away (flush the buffer) input.nextLine();   // add a linefeed between prompts System.out.println(); }   input.close();   return vectora; }```
That's all I'll do, and it may not be exactly right. Hope it helps.
• October 5th, 2013, 09:23 AM
newbie
Re: Put a random number from one vector to another
args is an array limited in scope to the main method. You're trying to use it inside your getUserInput(), but as far as that method is concerned, the args array does not exist.

Code java:

``` //Reading the vector with StdIn public static int [] getUserInput(int N) { //int N = args.lenght; int [] vectora = new int [N]; for (int i = 0;i<vectora.length;i++){   vectora [i] =Integer.parseInt(args[i]); //WHAT IS ARGS[I]?????? } return vectora; }```
• October 5th, 2013, 05:13 PM
JohnJohnson123
Re: Put a random number from one vector to another
I'm wondering. Is this something to build on...

I'll have to ''build'' the methods again...but than again the other stuff I got is trash...

Code Java:

```public class Sample1{   //public class Sample1 { public static void main(String[] args) { int M = Integer.parseInt(args[0]); // choose this many elements int N = Integer.parseInt(args[1]); // from 0, 1, ..., N-1   // create permutation 0, 1, ..., N-1 int[] perm = {8,1,2,3,5,6,7,8}; //new int[N]; for (int i = 0; i < N; i++) perm[i] = i;   // create random sample in perm[0], perm[1], ..., perm[M-1] for (int i = 0; i < M; i++) {   // random integer between i and N-1 int r = i + (int) (Math.random() * (N-i));   // swap elements at indices i and r int t = perm[r]; perm[r] = perm[i]; perm[i] = t; }   // print results for (int i = 0; i < M; i++) System.out.print(perm[i] + " "); System.out.println(); } }```
• October 6th, 2013, 06:36 PM
JohnJohnson123
Re: Put a random number from one vector to another
It seems that #13 was correct. Thanks for your help and patiance.:)