1

Counting in Lojban, an artificial language developed over the last forty years, is easier than in most languages The numbers from zero to nine are:

0  no
1  pa
2  re
3  ci
4  vo
5  mk
6  xa
7  ze
8  bi
9  so

Larger numbers are created by gluing the digit together. For example, 123 is "pareci". Write a program that reads in a lojban string (representing a no. less than or equal to 1,000,000) and output it in numbers.

I don't know what happened; it seems that my programme is not checking the if statement if (str.substring(i-2,i) == q[ale]). The rest of the loop is working fine. Any advice?

public class apple {
    public static void main(String[] args) {
        String [] q =
           new String[10];//"no","pa","re","ci","vo","mk","xa","ze","bi","so"];
        q[0] = "no";
        q[1] = "pa";
        q[2] = "re";
        q[3] = "so";
        q[4] = "ci";
        q[5] = "vo";
        q[6] = "mk";
        q[7] = "xa";
        q[8] = "ze";
        q[9] = "bi";

        //q[0] = "so";
        int ln;

        String str = "nocipa";
        ln = str.length();

        if (ln % 2 != 0)
        {
            System.out.println("Invalid number");
        }
        else
        { 
            for (int i = 2; i <=ln-2; i = i + 2)
            {
                for (int ale = 0; ale < 9; ale++)
                {
                    if (str.substring(i-2,i) == q[ale])
                    {
                        System.out.print("hello" ); 
                    }
                }
            }
        }
    }
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
nitin
  • 11
  • 1
  • 2
    Also, for what it's worth, testing that the length of the string is even is *not* a sufficient test for whether it's a valid number or not. Necessary, but not sufficient. – John Oct 05 '13 at 00:18
  • And there is an inconsistency in the declared list of numbers: 3 is "ci" (in your explanation) or "so" (in your code)? – Matthieu Oct 05 '13 at 00:24

1 Answers1

1

As @Makoto stated, and as that question explains using == is not the way to compare strings. Instead use

str.substring(i-2,i).equals(q[ale]);

== operator will return true if both sides refer to the same object. Not if the objects are equal.

Community
  • 1
  • 1
Matthieu
  • 2,736
  • 4
  • 57
  • 87