0

I have a task in my class which is to use a linked list and a method returning a boolean saying either true or false. The method is supposed to check if the list is sorted or not (sorted being in chronological order ex. 1, 2, 7, 14).

When I run this program I do not get any answer whether or not my list is sorted. Am I using the scanner wrong or what's going on?

package Kap18;

import java.util.*;
import javax.swing.*;

public class Lista {

public static void main(String[] args) {
        boolean sorted;
        
        System.out.println("Write a few numbers");
        Scanner sc = new Scanner(System.in);
        
        LinkedList<Integer> tab = new LinkedList<Integer>();
        
        while (sc.hasNextInt())
            tab.add(sc.nextInt());
        
        isSorted(tab);
        if (sorted = false){
            System.out.println("The list is not sorted");
        }
        else if (sorted = true){
            System.out.println("The list is sorted");
        }
        }
        
    public static boolean isSorted(LinkedList tab){
        boolean sorted = true;
        int i = 0;
        int num1 = (Integer)tab.get(i);
        int num2;
        for(i = 1; i <= tab.size() - 1; i++){
            num2 = (Integer)tab.get(i);
            if(num1 < num2){
                num1 = num2;
            }
            else{
                i = tab.size();
                sorted = false;
            }
        }
        return sorted;
    }
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Gabi
  • 3
  • 1
  • 2
    It looks like you are setting sorted equal to true/false in the if checks of your main method. `=` sets values, `==` checks equality. Since sorted is a boolean, you don't technically need to use `==` either. `if(sorted) { /* print is sorted */ } else { /* print not sorted */}` – darkmnemic Apr 16 '21 at 15:08

2 Answers2

0

There are a few issues here. First, you ignore the return value of isSorted. Second, you assign false and then true to the sorted variable in the if statements by using the = operator instead of ==. In the case of booleans, the easiest way to avoid such mistakes is to check the variable directly instead of comparing it to true or false:

sorted = isSorted(tab);
if (sorted) {
    System.out.println("The list is sorted");
}
else if (sorted = true){
    System.out.println("The list is not sorted");        
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

Your problem is that sorted = false is assigning false to sorted, and then using false for the test. So the test is always going to say that the list is sorted.

You should use == rather than =.

However, many people (including myself) think that variable == true and variable == false is Bad Style; e.g. see https://stackoverflow.com/a/2661150/139985. And the reasoning is that using == leaves open the possibility of the one character typo that just bit you; i.e. mistyping == as =.

(It is also redundant to use == and != in boolean expressions in most cases. But not all.)

So, I would say that the best way to write that is:

    if (!sorted) {
        System.out.println("The list is not sorted");
    }
    else {
        System.out.println("The list is sorted");
    }

or maybe the other way around so that you don't need to negate the sorted variable.


A second problem is that sorted is not set in the first place, since you don't assign the result of the isSorted call to anything.

Normally you would get a compilation error for using sorted before it is assigned. But in this case, the if statement is assigning a value to sorted ... due to the previous mistake.


Finally, there is a bug in isSorted in the case where the list is empty. (Try it and see ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216