stratofortress stratofortress - 5 months ago 17
Java Question

input a list of numbers and 'void' type not allowed here

Below is my preliminary script.

public class Insertion
{
public static void sort(double[] a)
{
int N = a.length;
for (int i=1; i<N; i++)
{
for (int j = i; j>0; j--)
{
if (a[j-1] > a[j])
exch(a, j-1, j);
else break;
}
}
}

public static void exch(double[] a, int i, int j)
{
double swap = a[i];
a[i] = a[j];
a[j] = swap;
}

public static void main(String[] args)
{
double[] a = {5, 2, 4, 1}; //eventually wanna be able to input this in interactions
System.out.println(a[1]); //just trying to figure out what's going on here
System.out.println(a.length);
System.out.println(sort(a));
}
}


If i run it, i get the following error message for
System.out.println(sort(a));


Error: 'void' type not allowed here


So I have two questions:


  1. what do they mean by void not allowed and how do you resolve this issue?

  2. As you may have noticed, i'm hand typing in
    double []a
    because I still havent been able to figure out how to input a list of numbers when executing a java script.






Edit:



this is after I made the changes recommended to me

public class Insertion
{
public static double[] sort(double[] a)
{
int N = a.length;
for (int i=1; i<N; i++)
{
for (int j = i; j>0; j--)
{
if (a[j-1] > a[j])
exch(a, j-1, j);
else break;
}
}
return a;
}
public static void exch(double[] a, int i, int j)
{
double swap = a[i];
a[i] = a[j];
a[j] = swap;
}
public static void main(String[] args)
{
double[] a = {5, 2, 4, 1};
System.out.println(a);
//System.out.println(sort(a));
// sort(a);
System.out.println(sort(a));
}
}


As output of the script as written above, i get the following
[D@3e9af3ee

Answer

Your method doesn't have a return stataement public static void sort(double[] a) for that you cant print System.out.println(sort(a));.

Try to change it in something like this:

   public static double[] sort(double[] a){
   ...
   return a;
   }

In main method do this to iterate the array:

  for(double db: sort(a)){
   System.out.println(db);
  }