java type character_Java Character getType()方法

Java Character getType()方法

java.lang.Character.getType(char ch) 返回值表示一个字符的一般类别。

1 语法

public static int getType(char ch)

2 参数

ch:要测试的字符

3 返回值

此方法返回类型为int表示字符的常规类别的值。

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* Java Character getType()方法

*/

import java.lang.*;

public class CharacterDemo {

public static void main(String[] args) {

// create 2 character primitives ch1, ch2

char ch1, ch2;

// assign values to ch1, ch2

ch1 = 'M';

ch2 = '$';

// create 2 int primitives i1, i2

int i1, i2;

// assign getType values of ch1, ch2 to i1, i2

i1 = Character.getType(ch1);

i2 = Character.getType(ch2);

/**

* value 1 represents UPPERCASE_LETTER

* value 26 represents CURRENCY_SYMBOL

*/

String str1 = "Category of " + ch1 + " is " + i1;

String str2 = "Category of " + ch2 + " is " + i2;

// print i1, i2 values

System.out.println( str1 );

System.out.println( str2 );

}

}

输出结果为:

Category of M is 1

Category of $ is 26

以下是一个用Java编写的手写词法分析器的示例代码: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Lexer { private static final char EOF = (char) -1; private static final char EOL = '\n'; private static final String OPERATORS = "+-*/()"; private static final String DIGITS = "0123456789"; private BufferedReader reader; private char currentChar; public Lexer(String fileName) throws IOException { reader = new BufferedReader(new FileReader(fileName)); currentChar = (char) reader.read(); } public List<Token> tokenize() throws IOException { List<Token> tokens = new ArrayList<>(); while (currentChar != EOF) { if (currentChar == EOL) { consume(); continue; } if (Character.isWhitespace(currentChar)) { consume(); continue; } if (OPERATORS.indexOf(currentChar) != -1) { tokens.add(new Token(Token.Type.OPERATOR, Character.toString(currentChar))); consume(); continue; } if (DIGITS.indexOf(currentChar) != -1) { StringBuilder sb = new StringBuilder(); while (DIGITS.indexOf(currentChar) != -1) { sb.append(currentChar); consume(); } tokens.add(new Token(Token.Type.NUMBER, sb.toString())); continue; } throw new RuntimeException("Invalid character: " + currentChar); } return tokens; } private void consume() throws IOException { currentChar = (char) reader.read(); } } class Token { enum Type { OPERATOR, NUMBER } private Type type; private String value; public Token(Type type, String value) { this.type = type; this.value = value; } public Type getType() { return type; } public String getValue() { return value; } } ``` 这个词法分析器可以读取一个文件并将其分解为令牌。它支持四种类型的令牌:运算符、数字、换行符和文件结束符。它使用一个缓冲区来存储当前正在解析的符号,并使用一个符号类型的数组来存储确定模型的符号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值