


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Consider the process you perform to read. The first thing your brain does is lexical analysis , which identifies the distinct words in a sentence. Consider the job of a compiler (translator) Source code --> TRANSLATOR --> machine code. The first thing the translator does is also lexical analysis-- It does the computing equivalent to reading. In this case, it identifies the distinct tokens in a program statement or statements. A lexer would take input such as: balance4 = balance4 * .05; and return a list of tokens identified by token type: balance4 identifier = assignment operator balance4 identifier
Java Strings are very similar to Python strings: String s=""; // note double quotes, not single ('). s = "abc"; s = s + "def"; // concatenation Java also has a scalar type called char. A char is a single ASCII (Unicode) symbol. The literal value of a char is denoted using single quotes, e.g. char c = 'a'; if (c == ' ') // is c a space? Underneath the hood, a Java String is a sequence of chars delimited by the end-of-string char ('\0'). What data members do you think the class String has? In Java, you can’t index a string (as in Python). Normal object dot notation is used instead The String class provides the methods: char charAt(int i); int length(); Example: String s = "abcdefg"; char c = s.charAt(3); // value of c is 'd' int length = s.length(); // value of length is 7 Looping through the characters of a String: int i = 0; while (i<s.length()) { char c = s.charAt(i); // do something to c. } How would you ask if a char is a letter? A digit?
The Java library has a Character class which provides some static methods that act on the char data type. They can make your job easier. Its part of java.lang, so remember to import java.lang.*; Some useful methods for lexical analysis of strings are: boolean isDigit(char c) boolean isLetter(char c) boolean isLetterOrDigit(char c) boolean isWhitespace(char c) // includes tabs and ' ' For example, if (Character.isDigit('3')) returns true.