此类错误关注这个错误的字符——’\u200b’
'\u200b’表示为zero-width-space,以Unicode编码。因此,你使用的Editor对文件(源文件)的编码方式是UTF-8。
在我使用过程中,我遇到这类错误发生的情况有:
下面给出错误源代码,只需要关注错误行,使用简单点的Editor就可以纠正错误了。
CalcGUI源代码
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import gbc.GBC;
public class CalcGUI
{
private Frame f = new Frame("Calculator");
private TextField text = new TextField("0");
private final String[] btStrs = {"MC", "MR", "MS", "M+", "M-", "←", "CE",
"C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*",
"1/x", "1", "2", "3", "-", "=", "0", ".", "+"};
private Button[] bts = new Button[28];
private Panel p1 = new Panel();
private GridBagLayout gbl = new GridBagLayout();
private boolean firstNumPhrase = false;
private boolean secondNumPhrase = false;
private String firstNum = "";
private String secondNum = "";
public void init()
{
firstNumPhrase = true;
//初始化按钮数组
{
int i = 0;
for(String str : btStrs)
{
bts[i++] = new Button(str);
}
}
//按钮添加监听事件
ActionListener al = e -> {
String cmd = e.getActionCommand();
if(firstNumPhrase == true)
{
if(cmd.charAt(0) <= '9' && cmd.charAt(0) >= '0')
{
System.out.print(cmd.charAt(0));
firstNum += cmd;
}
else if(cmd.equals("+") || cmd.equals("-") ||
cmd.equals("*") || cmd.equals("/"))
{
System.out.print('\n');
System.out.println(cmd.charAt(0));
secondNumPhrase = true;
firstNumPhrase = false;
}
}
else if(secondNumPhrase == true)
{
if(cmd.charAt(0) <= '9' && cmd.charAt(0) >= '0')
{
System.out.print(cmd.charAt(0));
secondNum += cmd;
}
else if(cmd.equals("="))
{
System.out.print('\n');
System.out.println(cmd.charAt(0));
secondNumPhrase = false;
firstNumPhrase = true;
}
}
};
//关闭窗口监听事件注册到Frame对象
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//注册监听事件到按钮
for(int i = 0; i < 28; i++)
{
bts[i].addActionListener(al);
}
text.setFont(new Font("Century Schoolbook", Font.PLAIN, 14)); //TextField text添加到Panel p1
text.setEditable(false); //不可编辑的TextField text
p1.setLayout(gbl); //GridBagLayout作为Panel p1的布局管理器
gbl.setConstraints(text, new GBC(0, 0, 5, 1).setIpad(150, 30).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH));
p1.add(text);
//Button添加到Panel p1
for(int i = 0; i < 4 * 5 + 4; i++)
{
addButton(bts[i], new GBC(i%5, i/5+1, 1, 1).setIpad(30, 30).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH));
}
addButton(bts[24], new GBC(4, 5, 1, 2).setIpad(30, 30).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH)); //=
addButton(bts[25], new GBC(0, 6, 2, 1).setIpad(30, 30).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH)); //0
addButton(bts[26], new GBC(2, 6, 1, 1).setIpad(30, 30).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH)); //.
addButton(bts[27], new GBC(3, 6, 1, 1).setIpad(30, 30).setWeight(0.5, 0.5).setFill(GridBagConstraints.BOTH)); //+
f.add(p1); //Panel p1添加到Frame f
f.pack(); //窗口最佳大小
f.setVisible(true); //窗口可见
}
void addButton(String name, GridBagConstraints c)
{
Button button = new Button(name);
gbl.setConstraints(button, c);
p1.add(button);
}
void addButton(Button bt, GridBagConstraints c)
{
gbl.setConstraints(bt, c);
p1.add(bt);
}
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
我选择把源代码的74-82行复制下来,
这时候一定要选择一个简单的编辑器(Editor),比如Window自带的notepad,
效果图如下
然后保存,随便选择文件名,我选择保存在桌面,文件名为2.txt,然后会弹出提示框,如下图
接下来,使用notepad++打开或者你原来的那个UTF-8的编辑器打开,
然后就会发现怎么莫名其妙多出了一个问号,如下图
接下来把?删除,全选复制(Ctrl + A, Ctrl + C),然后定位到错误代码范围把74-82行替换掉就不会编译器报错了。
你也可以选择参考下面这篇文章,本文也是基于此链接进行解决的。
stackoverflow