Java error in declaring array in random - java

import java.util.Random;
class random
{
public static void main(String[] args)
{
int n[];
Random rand=new Random();
for(int i=0;i<=10;i++)
{
int n[i]=rand.nextInt();
System.out.println("The random number is::"+n[i]);
}
}
}
This is the error i get while compiling:
I have no idea can anyone please help.

// first you need to declare the size for the n array
int n[] = new int [11]; // needs to be 11
Random rand=new Random();
for(int i=0;i<=10;i++)
{
n[i]=rand.nextInt(); // and then just assign
System.out.println("The random number is::"+n[i]);
}
but to be honest you do not even need this array in this code as it is not being re-used
More simpler would be
Random rand=new Random();
for(int i=0;i<=10;i++)
{
System.out.println("The random number is::" + rand.nextInt());
}

You need to initialize your array. See link for an example.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
n = new int[11];
before the for loop should do it.
Thanks for the catch wombat

try this thing...
import java.util.Random;
class random
{
public static void main(String[] args)
{
int[] n= {1,2,3,4,5,6,7,8,9,10,11};
for(int i=0;i<=10;i++)
{
Random rand=new Random();
n[i]=rand.nextInt();
System.out.println("The random number is::"+n[i]);
}
}
}

First of all, suppose we want to use array we can use in two ways:
As a variable where you need to define fixed size first otherwise you will get ArrayIndexOutOfBoundsException:
int[] id = new int[size fo array];
example : -
`
import java.util.Random;
class random
{ public static void main(String[] args)
{
// here first you need to initilized the array with fixed sizea
int n[] = new int[11];
Random rand=new Random();
for(int i=0;i<=10;i++)
{
n[i]=rand.nextInt();
System.out.println("The random number is::"+n[i]);
}
}
}
`
As an Argument of the method where you need not define the fixed size:
the best example is a command line argument
For execution below code, you need to pass command line argument at runtime.
`
import java.util.Random;
class random
{
public static void main(String[] args)
{
System.out.println(args[0] + args[1]);
}
}
`

Related

User inputs specific set of number and random number from the set is outputted

I have a java code that randomizes a number from a specific set, i want to be able to have the user inputs the specific set such as: {1,6,400,500} and the output is randomized from these numbers, how would i do that?, here is the code i have:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Randomizer {
public static void main(String[] args)
{
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(24);
list.add(77);
list.add(90);
list.add(80);
list.add(1790);
Randomizer obj = new Randomizer();
int boundIndex = 3;
System.out.println(
obj.getRandomElement(list, boundIndex));
}
public int getRandomElement(List<Integer> list,
int bound)
{
return list.get(
ThreadLocalRandom.current().nextInt(list.size())
% bound);
}
}
You can use the Random java util:
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(24);
list.add(77);
list.add(90);
list.add(80);
list.add(1790);
Random random = new Random();
int randomInt = list.get(random.nextInt(list.size()));
System.out.println("Random int from list = " + randomInt);
}

why am I getting the cannot find symbol when passing an array into a method

can someone please tell me why I am getting this error when compiling?
import java.util.*;
import java.io.*;
public class StatsCalculator
{
public static void main (String[]args)
{
programHeader();
randomNo(random);
printArray(random);
}
public static void programHeader()//writes program header
{
System.out.println("****************");
System.out.println("Stats calculator");
System.out.println("****************");
}
public static int[] randomNo(int[] random)// fills an array with 10 random numbers
{
random = new int[10];
for (int i=0; i< random.length; i++){
int randomNumber= (int) (Math.random()*10)+1;
random[i] = randomNumber;
}
return random;
}
public static int[] printArray (int[] random)//prints array
{
System.out.println("Your ten random values are: ");
for (int i=0; i<random.length; i++){
System.out.print(Arrays.toString(random));
}
return random;
}
}
I am writing a simple program to fill and array with 10 random numbers 1-10 and then calculate the sum, mean, mode and median of all the random numbers but i can get the methods to work just to fill the array and to print the array.
any help is appreciated.
You should get return value of randomNo() then pass it to next method. This may help you:
import java.util.Arrays;
public class StatsCalculator {
public static void main(String[] args) {
programHeader();
int[] random = randomNo();
printArray(random);
}
public static void programHeader()//writes program header
{
System.out.println("****************");
System.out.println("Stats calculator");
System.out.println("****************");
}
public static int[] randomNo()// fills an array with 10 random numbers
{
int[] random = new int[10];
for (int i = 0; i < random.length; i++) {
int randomNumber = (int) (Math.random() * 10) + 1;
random[i] = randomNumber;
}
return random;
}
public static int[] printArray(int[] random)//prints array
{
System.out.println("Your ten random values are: ");
for (int i = 0; i < random.length; i++) {
System.out.print(Arrays.toString(random));
}
return random;
}
}

calculate number of occurrences in array in java

I want to calculate the number of occurences of a specific number I decide in my main method. Here is what I have to do :
• A function that fills the array with random numbers.
• A function that calculates the number of occurrences,this function may not do any input or output.
• The main function that asks the user for the number and present the result on the screen.
Here is my code in java :
import java.util.Random;
import java.util.Scanner;
public class Code {
public void calculate(int value) {
Code c = new Code();
int count=0;
for (int n=0; n<array.length; n++) { // the code does not recognize array
if (array[n]==value) { // the code does not recognize array
count++;
}
}
System.out.println(count);
}
public void addToArray() {
int k =0;
int [] array = new int[10];
int min=0;
int max=10;
int diff = max-min;
while (k<10) {
Random r = new Random();
int x = r.nextInt(diff);
array[k]=x;
k=k+1;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("give your number");
Code c = new Code();
Scanner s = new Scanner(System.in);
int value = s.nextInt();
c.addToArray();
c.calculate(value);
}
}
The only thing I need help with is the calculate method ,, eclipse does not recognize array in the calculate method above ..
How to correct the calculate method to make it recognize array ?
thank you
Your array is limited to the scope of the method addToArray(). So, it will not be visible to other methods. You need to make it global. Then it will work.
Instead of declaring array in the method addToArray, declare it in your class, like:
public class Code {
int [] array = new int[10];
...
if using Java 8 you could update your calculate methode like this :
public void calculate(int value) {
System.out.println(Arrays.stream(array)
.filter(i -> i == value)
.count());
}
By adding this imports to your class :
import java.util.Arrays;
And like mentionned HackerDarshi declaring the attribut array as a member of your class

Random number and array search

I am attempting to generate an ADDITIONAL single random number and create another method that will have three parameters = the integer array, the size of the array, and the value it will be searching for in the array. It will search through the array and count how many times the value was found.It will then either print out how many times the value was found or that the value was not found. I am a bit lost and this is what I have so far which generates the random array and prints it/ asking the user if they want to restart. Thanks in advance
My code:
import java.util.Scanner;
import java.util.Random;
class Main{
public static final Random RND_GEN = new Random();
public void createNum(int[] randomNumbers) {
for (int i = 0; i < randomNumbers.length; i++) {
randomNumbers[i] = RND_GEN.nextInt(10) + 1;
}
}
public void printNum(int[] randomNumbers){
for (int i = 0; i < randomNumbers.length; i++) {
System.out.println("Number " + i + " : " + randomNumbers[i]);
}
}
public void run() {
Scanner inputReader = new Scanner(System.in);
int x = 1;
do {
int[] number = new int[20];
createNum(number);
printNum(number);
System.out.print("Restart Program?, Enter 1 for YES, 2 for NO: ");
x = inputReader.nextInt();
} while (x == 1);
}
public static void main(String[] args) {
Main go = new Main();
go.run();
}
}
I should not be telling you the whole code, since this looks like a homework question.
So here are the hints:
Creating an additional random number - Not so difficult, as you have RND_GEN
int rn = RND_GEN.nextInt(10)+1; //exactly as you have been doing it.
Creating a method that takes three parameters.... Here is the method header:
void search(int[] array, int size, int val)
Body of the method? You have various search algorithms out there. Some of the most popular ones are:
linear search
binary search
So just go on, research, learn and do it yourself.

Can't print a Random integer

I have just started learning Java. I was learning about "Randomization" from the "Kilobolt" tutorials. When I ran this code:
import java.util.Random;
public class Randomization {
public static void main (String[] args) {
Random rand = new Random();
rand.nextInt(11);
System.out.println(rand);
}
}
The console displayed:
java.util.Random#1888759
Is this supposed to happen? Or are there errors in my code?
( Sorry if i used any wrong terms in this question, I'm new to the website)
You're printing a reference to the object rand. You can either print out the random number like below:
public class Randomization {
public static void main (String[] args) {
Random rand = new Random();
System.out.println(rand.nextInt(11));
}
}
Or you can store it in an int before printing:
public class Randomization {
public static void main (String[] args) {
Random rand = new Random();
int randomNumber = rand.nextInt(11);
System.out.println(randomNumber);
}
}
Either should work fine.
It should be:
public class Randomization {
public static void main (String[] args) {
Random rand = new Random();
int randomNumber = rand.nextInt(11);
System.out.println(randomNumber);
}
}
Because you need to print the number. not the object of Random.
you are printing the object System.out.println(rand);
so you are getting this java.util.Random#1888759
try this way
System.out.println(rand.nextInt(11));
Full working code
Random rand = new Random();
this variable has been declared as a Object of Random.
however I'm pretty sure your intentions were to show the number that will be produced randomly.
To show it as a number, you have to declare the variable as an object of int.
to do that you can do :
Random rand = new Random();
int randomNumber = rand.nextInt(11);
hope this helps.

Categories

Resources