重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
System.out.println(' '==32);结果是true
成都创新互联拥有网站维护技术和项目管理团队,建立的售前、实施和售后服务体系,为客户提供定制化的网站设计制作、成都网站设计、网站维护、资阳主机托管解决方案。为客户网站安全和日常运维提供整体管家式外包优质服务。我们的网站维护服务覆盖集团企业、上市公司、外企网站、商城网站开发、政府网站等各类型客户群体,为全球数千家企业提供全方位网站维护、服务器维护解决方案。
这样来判断就好了.char类型可以直接跟int类型进行比较.char类型的空格换成int是32.
我觉得用正则表达式来写,感觉会比较简单
\s匹配任意的空白符,包括空格,制表符(Tab),换行符,中文全角空格
+代表的是表达式至少出现1次
*代表任意字符串。
假设代码为str,则:
String str1 = "^*\\s+$";
if(str.equals(str1)){
System.out.println("该代码后面有空格");
}else{
System.out.println("该代码后面无空格");
}
获取输入的字符串,假设这个字符串名字叫 input
那么 input.equals(" ")当输入为空格时返回true,反之为false
******************************************************************
新建类SpaceErrorCount.java,代码如下:
******************************************************************
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
/**
* @author Godwin
* @version 2010-05-19
*/
public class SpaceErrorCount extends JFrame {
JTextArea text;
JTextArea result;
public SpaceErrorCount() {
this.setTitle("空格的规范度");
// 文本框
text = new JTextArea(6, 50);
text.setLineWrap(true);
JScrollPane textScroll = new JScrollPane(text);
text.setBorder(BorderFactory.createBevelBorder(1));
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.setBorder(BorderFactory.createTitledBorder("输入或导入的文本"));
textPanel.add(textScroll);
// 结果框
result = new JTextArea(6, 50);
result.setLineWrap(true);
JScrollPane resultScroll = new JScrollPane(result);
result.setBorder(BorderFactory.createBevelBorder(1));
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.setBorder(BorderFactory.createTitledBorder("空格的规范度结果"));
resultPanel.add(resultScroll);
// 导入文本和结果框
JPanel allPanel = new JPanel();
allPanel.setLayout(new GridLayout(2, 1));
allPanel.add(textPanel);
allPanel.add(resultPanel);
// 按钮
JButton impButton = new JButton("导入文本");
JButton calcButton = new JButton("计算规范度");
JButton outputButton = new JButton("导出结果");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(impButton);
buttonPanel.add(calcButton);
buttonPanel.add(outputButton);
// 添加
this.add(allPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
// this.setSize(400, 300);
this.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 导入文本
impButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText();
}
});
// 截取
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] inputs = text.getText().trim().split("\n");
TreeSetInteger currentLineErrorPositions = new TreeSetInteger();
StringBuffer lineOutput = new StringBuffer();
for (int lines = 0; lines inputs.length; lines++) {
currentLineErrorPositions.clear();
byte[] lineBytes = inputs[lines].getBytes();
for (int i = 1; i lineBytes.length - 1; i++) {
if (lineBytes[i] == ' ') {
if ((!(lineBytes[i - 1] == ','))
(!(lineBytes[i - 1] == ';'))
(!(lineBytes[i + 1] == ';'))) {
currentLineErrorPositions.add(i);
}
} else if (lineBytes[i] == ',') {
if (lineBytes[i - 1] == ' ') {
currentLineErrorPositions.add(i - 1);
}
if (!(lineBytes[i + 1] == ' ')) {
currentLineErrorPositions.add(i + 1);
}
} else if (lineBytes[i] == ';') {
if (!(lineBytes[i - 1] == ' ')) {
currentLineErrorPositions.add(i);
}
if (!(lineBytes[i + 1] == ' ')) {
currentLineErrorPositions.add(i + 1);
}
}
}
// 添加此行错误位置和本行文本
if (currentLineErrorPositions.size() 0) {
lineOutput.append((lines + 1) + ": [");
IteratorInteger it = currentLineErrorPositions
.iterator();
int i = 0;
while (it.hasNext()) {
lineOutput.append(it.next());
i++;
if (i currentLineErrorPositions.size()) {
lineOutput.append(", ");
}
}
lineOutput.append("] ");
lineOutput.append(inputs[lines]);
lineOutput.append("\n");
}
}
if (lineOutput.length() == 0) {
result.setText("全部正确!");
} else {
result.setText(lineOutput.toString().trim());
}
}
});
outputButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
outputText();
}
});
}
// 导入文本
public void inputText() {
try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
File file = chooser.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String s;
text.setText("");
while ((s = br.readLine()) != null) {
text.append(s + "\n");
}
text.setText(text.getText().trim());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
public void outputText() {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(this);
File file = chooser.getSelectedFile();
String fileName = file.getName();
if (fileName != null fileName.trim().length() 0) {
if (!(fileName.endsWith(".txt"))) {
fileName = fileName.concat(".txt");
}
}
file = new File(file.getParent().concat(File.separator)
.concat(fileName));
if (file.exists()) {
int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
if (i != JOptionPane.YES_OPTION) {
return;
}
}
try {
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(result.getText());
fw.flush();
fw.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",
JOptionPane.WARNING_MESSAGE);
return;
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new SpaceErrorCount();
}
}
******************************************************************
运行结果如下:
******************************************************************
String s ="hello world";
s..contains(" ");
或者
int i = s.replace(" ","");
if(i 0)
System.out.println("有空格");