-2

Can someone tell me whats wrong with this code-> how to print out this boolean after passing it into main? The code is correct but I do not get how to print true or false.

Write a program accepts an array of doubles as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise.

For example, if arrays named list1 and list2 store {16.1, 12.3, 22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls isSorted(list1) and isSorted(list2) should return false and true respectively.

Assume the array has at least one element. A one-element array is considered to be sorted.

public static boolean isSorted (int n, Scanner console)
{

    double[]a= new double[(int) n];
    for (int i=0; i< n;i++)
    {
        a[i]=console.nextInt();
    }
    for (int i=1; i<a.length;i++)
    {
        if (a[(n-1)]>a[n])
        {
            return false;
        }
    }

    return true;
}

The main method:

public class sevenfour { 
    public static void main(String[] args){ 
        Scanner console= new Scanner(System.in); 
        int n= console.nextInt(); 
        System.out.println(isSorted); 
}
Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
  • sorry my main method is public class sevenfour { public static void main(String[] args){ Scanner console= new Scanner(System.in); int n= console.nextInt(); System.out.println(isSorted); } how can i print true+false values?? – food monsta Nov 11 '16 at 23:28
  • thanks for adding that extra information, please click the [edit](http://stackoverflow.com/posts/40557793/edit) button under your question to add that code into your question. thanks! – chickity china chinese chicken Nov 11 '16 at 23:33
  • *"how to print out this boolean"* Have you *tried* just printing the boolean value? Java will print `true` or `false` for you. – Andreas Nov 11 '16 at 23:35

2 Answers2

0

You need to pass in the int and Scanner arguments required for your isSorted method.

System.out.println(isSorted(n, console));
LiXie
  • 136
  • 2
  • 14
0

To print the result, you need to use System.out.println(isSorted(n, console));. Assuming n and console are defined.


Update:

The if condition inside the second for loop will throw a java.lang.ArrayIndexOutOfBoundsException as you are trying to access a[n] where you should use a[n-1]

Tanmay Baid
  • 459
  • 4
  • 19