-1

I've been working on this from quite some time now and am getting frustrated with the errors I am getting I hope someone out there can help me fix at least some of these or put me in the right direction. My errors start with **//. I tried to look up my questions already and could find nothing that truly helped me but I know i'll find someone on here who can this is why i'm posting.

import java.util.*;
import java.lang.Math;

public class TestBoxBiggest
{
 public static void main(String[]args)
 {
  //10 random Integers
   Random n=new Random();
   Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**
   for (int i=0;i<10;i++)
   {
    x[i]=n.nextInt(100); 
    System.out.println("The Integers are :\n"+x[i]);
   }
   SelectionSort.sort(x,10);
   System.out.println("The greatest Integer is:" +x);

   //10 random Doubles\

   Double[] d=new Double(10.0);**//cannot convert from java.lang.Double to  java.lang.Double[]**
   for (double k=0.0;k<10.0;k++)
   {
    d[k]=Math.random(1.0);**//cannot convert from double to int & the method random() in the type java.lang.Math is not applicable for the arguments (double)**
    System.out.println("The Doubles are:\n"+d[k]);**//cannot convert from double to int**
   }
   SelectionSort.sort(d,10);
   System.out.println("The greatest Double is:" +d);



  //5 Random box objects
   Random r=new Random();
   Box[]b=new Box[5];
   for (int i=0;i<5;i++)
   {
    int length=r.nextInt(30)+1; 
    int width=r.nextInt(30)+1;
    int height=r.nextInt(30)+1;
    b[i]=new Box(length,width,height);
    System.out.println("The boxes are: "+b[i]);
   }
   SelectionSort.sort(b,5);
   System.out.println("Boxes sorted by volume:\n");
   for (int i=0;i<5;i++)
   {
    System.out.println(b[i]+ "Volume: "+b[i].volume()); 
   }

   //5 String names
   String[] names={"Bobby","Freddie","John","Ralph","Usman"};
   System.out.println("The Strings are: "+names);

   Biggest.sort(names,names.length);//String implements comparable  **//the method sort(java.lang.String[],int) is undefined for the type Biggest**

   System.out.println("Sorted Strings\n");
   for(int i=0;i<names.length;i++)
   {
    System.out.println(names[i]); 
   }
 }
}```
  • 1
    Does this answer your question? [How to initialize an array in Java?](https://stackoverflow.com/questions/1938101/how-to-initialize-an-array-in-java) – Eldar B. Mar 19 '20 at 22:03
  • new Integer(10) creates one Integer object with a value of 10. new Integer[10] creates an array of 10 Integer objects. – NomadMaker Mar 19 '20 at 22:03

2 Answers2

0

Your errors occur because you didn't understand the concept of arrays in Java and your syntax of initializing them is wrong. Read this post to learn how to initialize an array.

Eldar B.
  • 1,097
  • 9
  • 22
0

Problem 1.

Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**

Here problem is that you are trying to assign an object of non-array type new Integer(10) to a variable of array type Integer[].

A correct ways to do is as follows:

Integer[] x = new Integer[] { 10 };
Integer[] y = { 10 };
int[] z = { 10 };

Problem 2.

Same applies to the next error

Double[] d=new Double(10.0);**//cannot convert from java.lang.Double to  java.lang.Double[]**

You need

Double[] d = { 10.0 };

Problem 3.

d[k]=Math.random(1.0);**//cannot convert from double to int & the method random() in the type java.lang.Math is not applicable for the arguments (double)**
System.out.println("The Doubles are:\n"+d[k]);**//cannot convert from double to int**

Here variable k is of type double. Arrays expect int types as an index. Simply change the type of k from double to int

Another problem is that Math.random() does not expect any parameters. Simply call it without passing any parameters and it will return a random value between 0 and 1

Problem 4.

Biggest.sort(names,names.length);//String implements comparable  **//the method sort(java.lang.String[],int) is undefined for the type Biggest**

Here hard to say what's the exact problem without knowing the signature of Biggest.sort method

tsobe
  • 179
  • 4
  • 14