Issues with adding a random numbers to an array - java

I am trying to add random numbers to an empty array 20 numbers 0-99. When I run the code below it prints out 51 numbers and they are all 0.
Can someone please help me figure out what I am doing wrong here.
import java.util.Random;
public class SortedArray
{
int randomValues;
int[] value;
public SortedArray()
{
}
public int getRandom()
{
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues = random.nextInt(100);
}
return randomValues;
}
public int getArray()
{
int result = 0;
value = new int[randomValues];
for(int item : value)
{
System.out.println("The array contains " + item);
}
return result;
}
}
Here is my main method
public class ReturnSortedArray
{
public static void main(String[] args)
{
SortedArray newArray = new SortedArray();
int random = newArray.getRandom();
int array = newArray.getArray();
System.out.println(array);
}
}

In your method getArray
the code
value = new int[randomValues];
is simply creating a new empty int array of size ramdomValues.
As the default value of an int is 0, that is what you are getting
Also in your method getRandom you are setting the same value time and time again
for (...)
randomValues = random.nextInt(100);
try
public int[] getRandomArr()
{
int randomValues [] = new int [20];
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues[j] = random.nextInt(100);
}
return randomValues;
}

I see a few issues, you should probably set the values in your constructor. You could also call it a set method (since it's not actually a get). Also, your getArray() doesn't return an array. So, I think you really wanted something like this,
public class SortedArray {
private Random random = new Random();
private int[] value = new int[20];
public SortedArray() {
super();
setRandomValues();
}
public void setRandomValues() {
for (int j = 0; j < value.length; j++) {
value[j] = random.nextInt(100);
}
}
public int[] getArray() {
return value;
}
}
And then your main method, should be updated like
public static void main(String[] args) {
SortedArray newArray = new SortedArray();
int[] array = newArray.getArray();
System.out.println(Arrays.toString(array));
}

Related

Error in tabs on java

I'm a beginner in Java, I need to do this exercise for school:
Write a method that gets an array of numbers and returns an array with the index of the minimum and maximum values inside the array.
Example: for int[] arr = {70,16,-3,5,90}, the method will return {2,4}
My code is this:
public class MinAndMax {
public static int main (String[] args) {
int[] myArray = {2,7,32,89,32,1,56,73,99,3827,56};
int min = myArray[0];
int max = myArray[0];
for (int i=0; i<myArray.length; i++) {
if (myArray[i]<min) {
min = myArray[i];
}
else if (myArray[i]>max) {
max = myArray[i];
}
}
int[] result = new int[2];
result[0] = min;
result[1] = max;
return result;
}
}
I don't understand the error message, I know I can't transform an int into an int[], but I created a new int[] here, so I don't see the error.
You should not return result array like that. Because you are writing that inside the void main function.
you code should do something like below,
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class MinAndMax
{
public static void main (String[] args) throws java.lang.Exception
{
int[] myArray = {2,7,32,89,32,1,56,73,99,3827,56};
int min = myArray[0];
int max = myArray[0];
for (int i=0; i<myArray.length; i++) {
if (myArray[i]<min) {
min = myArray[i];
}
else if (myArray[i]>max) {
max = myArray[i];
}
}
int[] result = new int[2];
result[0] = min;
result[1] = max;
System.out.println(Arrays.toString(result));
}
}
You get the error incompatible types: int[] cannot be converted to int because the main method has a int return type but you return int[].
To resolve the problem, use the standard main method signature public static void main (String[] args) since this allows the compiler to run the main method when you run the .java file.
To print the min and max values, you could extract the logic for finding min and max values to a separate method (e.g., getMinAndMaxValueIndices) and call it from the main method:
import java.util.Arrays;
class Main {
public static void main (String[] args) {
int[] minAndMaxValues = getMinAndMaxValues();
// print individual elements
int min = minAndMaxValues[0];
int max = minAndMaxValues[1];
System.out.println("min="+min+"\n"); // prints min=1
System.out.println("max="+max+"\n"); // prints max=3827
// print the array
System.out.println(Arrays.toString(minAndMaxValues)); // prints [1, 3827]
}
private static int[] getMinAndMaxValues() {
int[] myArray = {2,7,32,89,32,1,56,73,99,3827,56};
int min = myArray[0];
int max = myArray[0];
for (int i=0; i<myArray.length; i++) {
if (myArray[i]<min) {
min = myArray[i];
}
else if (myArray[i]>max) {
max = myArray[i];
}
}
int[] result = new int[2];
result[0] = min;
result[1] = max;
return result;
}
}

Array Method issue

having a problem with my java program. I am a newbie to Java and just can't figure out what is exactly the issue with it. In short I've declared an array and a variable in main, I've created my method call and would like my array be passed into my method with the variable. I would then like the method to take my array and count the number of times my variable "8" occurs, get rid of the 8 out of the array and return a new smaller array back to main. Here is my code below. I feel as if I am just missing one block code any suggestions?
public class Harrison7b
{
public static void main(String [] args)
{
int[] arrayA = {2,4,8,19,32,17,17,18,25,17,8,3,4,8};
int varB = 8;
// Call with the array and variable you need to find.
int[] result = newSmallerArray(arrayA, varB);
for(int x = 0; x < arrayA.length; x++)
{
System.out.print(arrayA[x] + " ");
}
}
public static int[] newSmallerArray( int[] arrayA, int varB)
{
int count = 0;
for(int x = 0; x < arrayA.length; x++)
{
if(arrayA[x] == varB)
{
count++;
}
}
int [] arrayX = new int[arrayA.length - count];
for(int B = 0; B < arrayA.length; B++)
{
if(arrayA[B] != varB)
{
}
}
return arrayX;
}
}
you do not actually need to return the array because when you pass an array to a method you also pass its memory address meaning its the same address that you change so, it will also change the arraysA of main method because you are just changing the values of the same memory adress
import java.util.*;
public class Help
{
public static void main(String[] args)
{
ArrayList<Integer> arraysA = new ArrayList<Integer>();
arraysA.add(Integer.valueOf(2));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(19));
arraysA.add(Integer.valueOf(32));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(18));
arraysA.add(Integer.valueOf(25));
arraysA.add(Integer.valueOf(17));
arraysA.add(Integer.valueOf(8));
arraysA.add(Integer.valueOf(3));
arraysA.add(Integer.valueOf(4));
arraysA.add(Integer.valueOf(8));
int varB=8;
newSmallerArray(arraysA,varB);
for(Integer i:arraysA)
{
System.out.println(i);
}
}
public static void newSmallerArray(ArrayList<Integer> arraysA,int varB)
{
for(int i=0;i<arraysA.size();++i)
{
if(Integer.valueOf(arraysA.get(i))==varB)
{
arraysA.remove(i);
}
}
}
}
Try this code it will not require for loop:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(arrayA));
list.removeAll(Arrays.asList(8));
arrayA = list.toArray(array);

I'm trying to make a java array code

i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?
this is my code
import java.util.*;
public class prova1{
public int min;
public void tabele(){
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
System.out.println(e[i]);
}
}
public void emin(){
Random r = new Random();
int d;
int e[]=new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
if(e[i]<min){
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
prova.tabele();
prova.emin();
}
}
Return the array from tabele to a local variable and send it as parameter to emin
import java.util.*;
public class prova1 {
public int min;
public int[] tabele() {
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(99) + 1;
e[i]=d;
System.out.println(e[i]);
}
return e;
}
public void emin(int[] e) {
Random r = new Random();
int d;
for(int i=0; i<e.length;i++) {
if(e[i]<min) {
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
int[] arr = prova.tabele();
prova.emin(arr);
}
}

Displaying a shuffled integer array in java

sorry I'm really new to all of this. I know this is a stupid/easy question, but how would I display the shuffled array after i've set it all up. I have my code below and made the class that creates the array and has the algorithm for shuffling the integers inside the array. But I can't figure out how to display the shuffled array. Heres my code below:
My main:
package lab4b;
import java.util.Scanner;
public class Lab4B {
public static void main(String[] args) {
Shuffler test = new Shuffler(15);
test.Shuffle();
test.display();
}
}
and my Shuffle class:
package lab4b;
import java.security.SecureRandom;
public class Shuffler {
private static final SecureRandom randomNumbers = new SecureRandom();
private int [] data;
public Shuffler (int size){
data = new int [size];
for(int i = 0; i<data.length;i++){
data[i]= i+1;
}
}
public void Shuffle(){
int temp;
for(int first = 0; first<data.length; first++){
int second = randomNumbers.nextInt(data.length);
temp = data[first];
data[first] = data[second];
data[second] = temp;
}
}
public void display()
{
for(int counter =0; counter<data.length; counter++ ){
System.out.print(data[counter]+ " ");
}
System.out.println();
}
}
In this loop you are reset the value of the dataarray
for(int counter =0; counter<data.length; counter++ ){
// data[counter] = counter + 1; - do not do this
System.out.print(data[counter]+ " ");
}

How to initialize an Array object?

I'm having trouble initializing an Array and don't really understand how to do it.
public class Array {
int[] array;
public Array(int[] array) {
this.array = array;
}
public int sum() {
int sum = 0;
for (int i = 0; i < this.array.length; i++) {
sum = sum + array[i];
}
return sum;
}
public double average() {
double av = this.sum() / this.array.length;
return av;
}
public static void main(String[] args) {
Array a = new Array[3];
}
}
I keep getting an error that says required: Array found: array[]
I want to make a scanner and have user input on the array but I don't even know how to initialize it in the first place
You have to change
Array a = new Array[3];
To
Array a = new Array(new int[3]);
Constructor of Array is taking int[] as input arguments.

Categories