0

I am writing a lexical analyzer. I know it's super simple. It runs but whenever enter an input, the program treats it as invalid characters (even when they are supposed to be valid). What did I do wrong?

import java.util.*;
import java.util.Scanner;

public class LAnalyze{

    public static int i;

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        String s;


        System.out.println("Input something to lexically analyze:  ");
        s = input.next( );
        int j = 1;

        if( s.charAt(i)!='a'||s.charAt(i)!='b'||s.charAt(i)!='c'||s.charAt(i)!='d'||s.charAt(i)!='e'||s.charAt(i)!='f'||
            s.charAt(i)!='g'||s.charAt(i)!='h'||s.charAt(i)!='i'||s.charAt(i)!='j'||s.charAt(i)!='k'||s.charAt(i)!='l'||
            s.charAt(i)!='m'||s.charAt(i)!='n'||s.charAt(i)!='o'||s.charAt(i)!='p'||s.charAt(i)!='q'||s.charAt(i)!='r'||
            s.charAt(i)!='s'||s.charAt(i)!='t'||s.charAt(i)!='u'||s.charAt(i)!='v'||s.charAt(i)!='w'||s.charAt(i)!='x'||
            s.charAt(i)!='y'||s.charAt(i)!='z'||s.charAt(i)!='A'||s.charAt(i)!='B'||s.charAt(i)!='C'||s.charAt(i)!='D'||
            s.charAt(i)!='E'||s.charAt(i)!='F'||s.charAt(i)!='G'||s.charAt(i)!='H'||s.charAt(i)!='I'||s.charAt(i)!='J'||
            s.charAt(i)!='K'||s.charAt(i)!='L'||s.charAt(i)!='M'||s.charAt(i)!='N'||s.charAt(i)!='O'||s.charAt(i)!='P'||
            s.charAt(i)!='Q'||s.charAt(i)!='R'||s.charAt(i)!='S'||s.charAt(i)!='T'||s.charAt(i)!='U'||s.charAt(i)!='V'||
            s.charAt(i)!='W'||s.charAt(i)!='X'||s.charAt(i)!='Y'||s.charAt(i)!='Z'||s.charAt(i)!='0'||s.charAt(i)!='1'||
            s.charAt(i)!='2'||s.charAt(i)!='3'||s.charAt(i)!='4'||s.charAt(i)!='5'||s.charAt(i)!='6'||s.charAt(i)!='7'||
            s.charAt(i)!='8'||s.charAt(i)!='9'||s.charAt(i)!='-'||s.charAt(i)!='_'||s.charAt(i)!=' ') {

            for (int i = 0; i < s.length(); i++) {  
                System.out.println("Token " + j + " = " + (s.charAt(i)));
                j++;
            }
        }
        else {
            System.out.println("Invalid character(s) entered.. Program terminated!\n");
            System.exit(0);
        }

    }
}
user1781192
  • 7
  • 2
  • 2
  • 1
    Haven't you mixed up `i` and `j` variables? – Alexis Pigeon Oct 28 '12 at 21:46
  • 5
    You have two different variables named i. Don't do that. Also, maybe you should consider caching s.charAt(i) into a char variable, and learning the `<` and `>` operators. – JB Nizet Oct 28 '12 at 21:48
  • It worked OK for me. You might want to print out s before you look at it to see just what you are getting as input. Also, you aren't printing out tokens, but characters. So it should be "Character " + j. You should print out the invalid character and it's decimal value. – Scooter Oct 28 '12 at 21:51
  • 2
    Just this condition in isolation: `s.charAt(i)!='a'||s.charAt(i)!='b'` **must always return true**. The rest of your expression will **never get a chance to even be evaluated**. – Marko Topolnik Oct 28 '12 at 22:03

2 Answers2

1

It would seem that it is impossible to get the results you say you are getting from this code. Your if statement is wrong. As it currently stands, it will always be true. A character will always be not equal to some character or not equal to another character. All of the != should be ==. I would also print out the bad character in the else part that reports it:

System.out.println("bad character " + s.charAt(i) +
     " decimal value: " + (int) s.charAt(i));

Scanner does lexing on its own, that is, it returns tokens, not the whole string. I think you should use Console and get everything that was typed:

Console console = System.console();
s = console.readLine("Input something to lexically analyze:  "); 
Scooter
  • 6,802
  • 8
  • 41
  • 64
0
import java.util.*;

public class Main {
    static int i;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = "";
        while (true) {
            System.out.println("Input something to lexically analyze: ");
            s = input.nextLine();
            analize(s);
        }
    }

    public static void analize(String s) {
        String t = "-1234567890_ abcdefjhijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ";
        char[] tt = t.toCharArray();
        char[] cc = s.toCharArray();
        int z = 1, i = 0, j = 0;
        for (i = 0; i < cc.length; i++) {
            for (j = 0; j < tt.length; j++) {
                if (cc[i] == tt[j]) {
                    System.out.println("Token " + z + " = '" + cc[i] + "'");
                    z++;
                    break;
                }
            }
            if (j > tt.length - 1) {
                System.out.println("Invalid character " + (i + 1) + " ('" + cc[i] + "') entered...");
            }
        }
    }
}
Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour) to learn how Stack Overflow works, and also refer to [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). In particular, please don't just add code without an explanation. And also note that the OP asked for an explanation "What did I do wrong?" – only stating code doesn't answer that. Finally also ensure that your code is properly formatted (intendation); I fixed it now in my pending edit. – Ivo Mori Aug 29 '20 at 01:58
  • Да , уважаемый Иво Мори , спасибо за советы и правку кода ... – Filin Filin Aug 29 '20 at 06:42