Need some help please!

Can someone tell me a nice way of changing the code below so the size of the array, N, can be easily changed through the input from the user.

import java.util.Random;

/**

This class contains utility methods for array

manipulation.

*/

publicclass ArrayUtil

{

/**

Creates an array filled with random values.

@param length the length of the array

@param n the number of possible random values

@return an array filled with length numbers between

0 and n-1

*/

publicstaticint[] randomIntArray(int length,int n)

{int[] a =newint[length];

Random generator =new Random();

for (int i = 0; i < a.length; i++)

a[i] = generator.nextInt(n);

return a;

}

/**

Prints all elements in an array.

@param a the array to print

*/

publicstaticvoid print(int[] a)

{

for (int i = 0; i < a.length; i++)

System.out.print(a[i] +" ");

System.out.println();

}

}

[2021 byte] By [Princea] at [2007-10-2 5:45:19]
# 1
Anyone have an idea?
Princea at 2007-7-16 1:55:13 > top of Java-index,Other Topics,Algorithms...
# 2

> Anyone have an idea?

I must say you made good progress since your last visit here. These two methods look OK. Test them with this main method (you must have JDK 1.5 for this example):

/**

* main method

*/

public static void main(String[] args) {

java.util.Scanner keyboard = new java.util.Scanner(System.in);

System.out.print("Enter the length of the array: ");

int length = keyboard.nextInt();

System.out.print("Enter the maximal value: ");

int maximal = keyboard.nextInt();

int[] array = randomIntArray(length, maximal);

print(array);

}

Good luck.

prometheuzza at 2007-7-16 1:55:13 > top of Java-index,Other Topics,Algorithms...
# 3

import java.util.Arrays;

import javax.swing.JOptionPane;

/**

This program tests the binary search algorithm.

*/

public class BinarySearchTest

{

public static void main(String[] args)

{

// construct random array

int[] a = ArrayUtil.randomIntArray(20, 100); /** I need that 20 part to change according to user input. */

Arrays.sort(a);

ArrayUtil.print(a);

BinarySearcher searcher = new BinarySearcher(a);

boolean done = false;

while (!done)

{

String input = JOptionPane.showInputDialog(

"Enter number to search for, Cancel to quit:");

if (input == null)

done = true;

else

{

int n = Integer.parseInt(input);

int pos = searcher.search(n);

System.out.println("Found in position " + pos);

}

}

System.exit(0);

}

}

I dont have version 1.5 so it didnt work. I produced the code above to go with the code i posted earlier. I dont know how to incorporate "enter the array size" method with the coding. Any ideas how to add it?

String input = JOptionPane.showInputDialog(

"Enter array size:");

int n = Integer.parseInt(input);

This is what I have but im not sure if it works because i dont know how to add it to the coding properly.

Princea at 2007-7-16 1:55:13 > top of Java-index,Other Topics,Algorithms...
# 4
Anyone here to help?
Princea at 2007-7-16 1:55:13 > top of Java-index,Other Topics,Algorithms...
# 5

> I dont have version 1.5 so it didnt work. I

> produced the code above to go with the code i posted

> earlier. I dont know how to incorporate "enter the

> array size" method with the coding. Any ideas how to

> add it?

Then you could do something like this:

/**

* main method

*/

public static void main(String[] args) {

String input;

int length, maximal;

input = javax.swing.JOptionPane.showInputDialog("Enter the length of the array: ");

length = Integer.parseInt(input);

input = javax.swing.JOptionPane.showInputDialog("Enter the maximal value: ");

maximal = Integer.parseInt(input);

int[] array = randomIntArray(length, maximal);

print(array);

}

prometheuzza at 2007-7-16 1:55:13 > top of Java-index,Other Topics,Algorithms...