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;
}