Reputation: 7
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);
}
}
}
Upvotes: 0
Views: 2962
Reputation: 1
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...");
}
}
}
}
Upvotes: 0
Reputation: 7061
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: ");
Upvotes: 1