当我的输入文件不包含初始空格时,此程序正常工作。当输入文件行在开头有空格时,我需要它才能正常工作。这是我输入的一个例子: 这有效:
这不起作用:
我认为这是因为在工作样本中,它将扫描并将字放在字符串中的数字位置0.第二个不起作用我认为因为它在数组位置0处放置空格。我使用了拆分\\ s +以避免我的代码中的空格,但它似乎没有解决这个问题。如何使此扫描器跳过所有初始空白区域并跳过输入文件中单词之间的空白区域,同时将第一个单词放在数组空间0中;
代码:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Parser {
static JFrame frame;
static JPanel panel = new JPanel();
public static void main(String[] args){
int c = 0;
boolean pane = false;
File input = new File("input.txt");
try{
Scanner reader = new Scanner(input);
do{
String[] buff2 = reader.nextLine().split("\\s+");
for (int i = 0; i<1; i++){
System.out.println(buff2[0]);
if(buff2[0].equalsIgnoreCase("end")){
c = 4;
frame.setVisible(true);
}
if(! reader.hasNextLine() ){
c = 4;
frame.setVisible(true);
}
if(buff2[0].equalsIgnoreCase("window")){
frame = new JFrame(buff2[1].substring(1, buff2[1].length()-1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(Integer.parseInt(buff2[2].substring(1, buff2[2].length()-1)),Integer.parseInt( buff2[3].substring(0, buff2[3].length()-1)));
// frame.setVisible(true);
if (buff2[4].equalsIgnoreCase("Layout")){
if (buff2[5].subSequence(0, buff2[5].length()-1).equals("Flow")){
frame.setLayout(new FlowLayout());
}
else if (buff2[5].equalsIgnoreCase("grid")){
frame.setLayout(new GridLayout(Integer.parseInt(buff2[6].substring(1, buff2[6].length()-1)),Integer.parseInt(buff2[7].substring(0, buff2[7].length()-1))
,Integer.parseInt(buff2[8].substring(0, buff2[8].length()-1)),Integer.parseInt(buff2[9].substring(0, buff2[9].length()-2))));
}
}
}
if (buff2[0].equalsIgnoreCase("Label")){
if (pane == true) {
panel.add(new JLabel(buff2[1].substring(1, buff2[1].length()-2)));
}
else frame.add(new JLabel(buff2[1].substring(1, buff2[1].length()-2)));
}
if (buff2[0].equalsIgnoreCase("button")){
if (pane == true) {
panel.add(new JButton(buff2[1].substring(1, buff2[1].length()-2)));
}
else frame.add(new JButton(buff2[1].substring(1, buff2[1].length()-2)));
}
if (buff2[0].equalsIgnoreCase("Textfield")){
if (pane == true) {
panel.add(new JTextField(Integer.parseInt( buff2[1].substring(0, buff2[1].length()-1))));
}
else frame.add(new JTextField(Integer.parseInt( buff2[1].substring(0, buff2[1].length()-1))));
}
if (buff2[0].equalsIgnoreCase("panel")){
pane = true;
frame.add(panel);
String layout = (String) buff2[2].subSequence(0, buff2[2].length()-3);
if (layout.equalsIgnoreCase("Flow")){
panel.setLayout(new FlowLayout());
}
else if (layout.equalsIgnoreCase("grid")){
System.out.println("hi");
panel.setLayout(new GridLayout(Integer.parseInt(buff2[2].substring(5, buff2[2].length()-1)),Integer.parseInt(buff2[3].substring(0, buff2[3].length()-1))
,Integer.parseInt(buff2[4].substring(0, buff2[4].length()-1)),Integer.parseInt(buff2[5].substring(0, buff2[5].length()-2))));
}
}
if (buff2[0].equalsIgnoreCase("Label")){
if (pane == true) {
panel.add(new JLabel(buff2[1].substring(1, buff2[1].length()-2)));
}
else frame.add(new JLabel(buff2[1].substring(1, buff2[1].length()-2)));
}
}
}
while (c !=4);
}
catch(FileNotFoundException e){
System.out.println("invalid file");
}
}
void group(){
}
}
答案 0 :(得分:3)
trim
行。
String[] buff2 = reader.nextLine().trim().split("\\s+");
答案 1 :(得分:1)
使用类String中的方法trim。 修剪删除任何前导(和尾随)空格。
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html