java - 从扫描仪读取并跳过空格

标签 java parsing split java.util.scanner csv

当我的输入文件不包含初始空白时,该程序运行正常。当输入文件行开头有空格时,我需要它正常工作。这是我的输入示例: 这有效:

  1. 窗口“计算器”(200, 200) 布局流程:

这不起作用:

  1. < 此处有 4 个空格> 窗口“计算器”(200, 200) 布局流程:

我认为这是因为在工作示例中,它将扫描并将单词放入数组位置 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(){

    }

}

最佳答案

trim拆分之前的行。

String[] buff2 =  reader.nextLine().trim().split("\\s+");

关于java - 从扫描仪读取并跳过空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27000406/

相关文章:

mysql - 在 mysql JOIN 查询中拆分字段值

python - 将数据帧拆分为多个数据帧

java - 责任链模式——与后代紧密耦合

java - 返回整数或 double 时如何确保 catch 返回值不同于 try 返回值

java - 解析一个巨大的纯文本文件

基于 PHP 的 LaTeX 解析器——从哪里开始?

python - 将字符串拆分为迭代器

javascript - 请求的资源上不存在 Access-Control-Allow-Origin header

java - Android:将卡片 View 从一个 fragment 发送到另一个 fragment

java - java I/O 的字符串数组逻辑问题