0

I am currently learning Java. Today I am making a little program where I input numbers and the program shall write if they are sorted or not.

I think I got the logic right, but I got a little error that I can't handle.

package inlamningsuppgift_arSorterad;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class ifSorted {

    public static void main(String[] arg) {
        LinkedList a = new LinkedList();
        Scanner sc = new Scanner(System.in);
        System.out.println("Input your numbers, end with the letter \\n");

        while (sc.hasNextDouble())
            a.add(sc.nextDouble());

        if (ifSorted(a))
            System.out.println("List is sorted");
        else
            System.out.println("List is not sorted");
    }

    private static boolean ifSorted(LinkedList a) {
        Collections.sort(a);

        return a;
    }
}
marstran
  • 26,413
  • 5
  • 61
  • 67
  • 2
    You defined your method with a return type of boolean, but then try to return a LinkedList. – OH GOD SPIDERS Feb 21 '17 at 11:38
  • 1
    Your function `aerSorterat` is wrong. If my swedish is correct, this function should return a boolean indicating whether the list is sorted or not. However, you are sorting the actual list and returning it. – marstran Feb 21 '17 at 11:38
  • @marstran Sorry for the swedish, i corrected it to english now. Do you mean that i return the whole list in the aerSorterat list? –  Feb 21 '17 at 11:41
  • 1
    Did you rename your class to the name the method should have get? If I get everything correct, maybe this post could help: [How to determine if a List is sorted in Java?](http://stackoverflow.com/q/3047051/5649397) – Pascal Schneider Feb 21 '17 at 11:44
  • @KrazyKalle Yeah i did. Thanks for the link, i have a look at it :) –  Feb 21 '17 at 11:45
  • @TheNoob Yep, that's right. – marstran Feb 21 '17 at 11:45

3 Answers3

0

Lets take a look at your aerSorterat method.

    private static boolean aerSorterat(LinkedList a) {
    Collections.sort(a);
    return a;
}

private static boolean means that your method will return a boolean which is either true or false .You inserted list [a] then returned [a] .No boolean .With your code You don't need that method.However you must cut the if else statement out of the loop so that after the user inserts a String for ex: out. It checks your list out side the loop.You can use the method if you want by inserting your if else statement in it and returning true or false then calling that method in the main method using ifsorted.aerSorterat(a);

Omar Boshra
  • 447
  • 7
  • 21
  • First, thanks for the answer. Second, if i understand you right i should put my if- else statement in the public static boolean ifSorted method? :) –  Feb 21 '17 at 12:07
  • Yes you can do that .Or as I said you don't even need the method just extract if-else out of the loop still in main method so when the user inserts something other than a double it breaks out of the loop.Then goes to your if-else statement. – Omar Boshra Feb 21 '17 at 12:09
  • Thanks for the help! :) –  Feb 21 '17 at 12:15
0

make this substitution! it work with Java8:

1- a.stream().sorted().collect(Collectors.toList() create a new sorted list

2- .equal(a) the new list comes compares the current

private static boolean ifSorted(LinkedList a) {
      return a.stream().sorted().collect(Collectors.toList()).equals(a);        
}
Bogojob
  • 135
  • 1
  • 10
0

You are actually trying to sort your list there with Collections.sort(a), but if you want to see if your values are sorted, you can do it e.g. with a loop like this:

private static boolean isSorted(List<Double> list) {
    return ascending(list) || descending(list);
}

If you have to check both ways, you easy do that with both methods connecting with an ||, which is an logical or. The single methods simply check if all values in a list are greater (ascending) oder lesser (descending) then their predecessors.

private static boolean ascending(List<Double> list) {
    for (int i = 1; i < list.size(); i++)
        if (list.get(i - 1) > list.get(i))
            return false;
    return true;
}

private static boolean descending(List<Double> list) {
    for (int i = 1; i < list.size(); i++)
        if (list.get(i - 1) < list.get(i))
            return false;
    return true;
}

If one of the values doesn't fit the premise, it simply returns false. If the loop is completed and left normally, that means that all values are sorted.

But to do that, you have to give the generic parameter <Double> as a parameter to your method, otherwise you cannot compare the values with < and >. Also I use List instead of LinkedList because with that given, you can use any kind of List you want.

If you want to use other objects than numbers, you need to change it a little.

First possability is, the objects you want to use have to implement the Comparable Interface, which provides the compareTo(...) method. Which that given, you can is it e.g. like the following:

public boolean ascending(List<YourObject> list) {
    for (int i = 1; i < list.size(); i++)
        if (list.get(i-1).compareTo(list.get(i)) > 0)
            return false;
    return true;
}

Simply swap the operator for descending. This e.g. can be used with a list of Strings.

Pascal Schneider
  • 405
  • 1
  • 5
  • 19