1

I'm trying to make a program that will read in a string and compare each character in the string to see if it is in alphabetical order.

public class Main
{
    public static void Main ( String[] args)
    {
        System.out.println("#Please enter the string: ");
        String s = BIO.getString();

        while(!s.equals("END")){
            int length = s.length();
            String sLC = s.toLowerCase();
            int count = 0;
            boolean inOrder = true;

            for(int i = 0; i < length - 1 ; i++){
                if(sLC.charAt(i).compareTo(sLC.charAt(i+1)) > 0) {
                    inOrder = false;
                    break;
                }   
            }  

            System.out.println("#Please enter the string: ");  
            s = BIO.getString();
        }
    }
}

I am using blueJ and when I try to compile this it is giving me the error 'char cannot be dereferenced and highlighting the 'compareTo' method in my IF statement?

j0k
  • 22,600
  • 28
  • 79
  • 90
jjharrison
  • 841
  • 4
  • 19
  • 33
  • Special case of [java - compareTo with primitives -> Integer / int - Stack Overflow](https://stackoverflow.com/questions/9150446/compareto-with-primitives-integer-int) – user202729 Feb 19 '22 at 13:21

2 Answers2

6

.charAt() returns a char, which is a primitive. It does not have a .compareTo() method.

char behaves much like a (smaller) int; use the following instead:

if(sLC.charAt(i) > sLC.charAt(i+1)) {
Cat
  • 66,919
  • 24
  • 133
  • 141
  • 1
    I think you want a >, not a <. – Louis Wasserman Nov 07 '12 at 20:43
  • @LouisWasserman Yup, thanks. I initially thought he wanted it to evaluate to `true`, not `false`. – Cat Nov 07 '12 at 20:44
  • ...except this would not be what OP wants if he wanted case insensitivity. For that you must raise back to the `Character` wrapper, if I remember correctly, because there's a spot in the library dedicated to Unicode-aware lexicographical sorting. – Marko Topolnik Nov 07 '12 at 20:47
  • @MarkoTopolnik The OP calls `String sLC = s.toLowerCase();` before he starts the loop. In terms of accents and such, yeah, these would be exceptions. – Cat Nov 07 '12 at 20:48
  • @Eric ah ok thankyou so because I'm using a string method to compare two char types it is giving me this error :) I wasn't aware you could just use the > symbol to compare char types... – jjharrison Nov 07 '12 at 20:49
  • @Eric Indeed you are right. Except if OP would happen to be Georgian, as it just so happens that one must actually convert to **uppercase** in order to reliably conflate all same letters. There's special code that checks it that way to be seen in the `Character` source code, with the comment "this is really needed only for the Georgian characters". – Marko Topolnik Nov 07 '12 at 20:58
  • Correction: actually it's the other way round, `toUpperCase` is the norm and `toLowerCase` is the exceptional case. And it's in the [String class, `equalsIgnoreCase`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/String.java#1401). – Marko Topolnik Nov 07 '12 at 21:02
3

sLC.charAt(i) gives you primitive char. And you cannot invoke compareTo on primitives. You need to wrap it in a Character wrapper object, or just use comparison operator.

if(Character.valueOf(sLC.charAt(i)).compareTo(
   Character.valueOf(sLC.charAt(i+1))) > 0)

or simply: -

if(sLC.charAt(i) > sLC.charAt(i+1)) 
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525