现在的位置: 首页 > 综合 > 正文

java字符串检查菜单 (第一个真正属于自己的程序)

2013年10月01日 ⁄ 综合 ⁄ 共 13374字 ⁄ 字号 评论关闭

整整一个星期都在搞这个东西,直到今天终于做完了,眼泪哗哗的。。。。。。

这个项目的最大问题有两个,首先是数组越界问题,始终没有解决好,导致每次离成功还有一两步的时候程序就倒了,然后改来改去,最终就改成了另外一种版本的了。

由此,引出了另一个重大问题,就是设计的问题。何雷曾经给我们讲过,一个项目首先应该有一个清晰的思路,比较详细的设计,然后根据这个设计搭一个框架出来,然后逐步去实现框架内的东西。而我的小项目基本上就是脑子里大概有个总体的想法,然后一步一步去实现,但是开始的时候没有统筹好,导致后来出现了思路混乱,数组越界等诸多难以解决的问题。

最大的收获时发现自己的差距和不足,希望能尽快改进,加强学习。

下面是程序的需求:

1 实现一个SWT窗口

2 在窗口中输入文件,判断文件是否符合规则。

3 文件格式如下:  

pdu 信号名{

数据#   注释

}

其中: 数据分两部分:16进制数和字符串。如果是字符串,必须以" "标示,引号中字符必须是az AZ 09 . : ; @

,并且,注释一直识别到行尾。

以下是代码:

 

/**
 *  a SWT programme ,check the input 
 *     whether fit the rule
 * 
@author  tianrenliang
 * 
@version 1.0
 
*/

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;

public class checkMenu {

    private static Shell sShell = null// @jve:decl-index=0:visual-constraint="10,10"

    
private static Button buttonCheck = null;

    private static Text textArea = null;

    private static Label labelMessage = null;

    private static Text text = null;    
    
    
static String start ="pdu ";  //  @jve:decl-index=0:
    
    
static char signalBegin = '{';
    
    
static char signalEnd ='}';
    
    
static char remarkTag = '#';
    
    
static char stringTag ='"';
    
    
static char lineChangeTag =' ';
    
    
static char lineEndTag =' ';
    
    
static String signLib=";:.@";
    
    
private static void createSShell() {
        
        
/*shell initializes*/
        sShell 
= new Shell();
        sShell.setText(
"CheckInput");
        sShell.setSize(
new Point(425360));
        sShell.setLayout(
null);
        
        
/*button initializes*/
        buttonCheck 
= new Button(sShell, SWT.NONE);
        buttonCheck.setBounds(
new Rectangle(3502876527));
        buttonCheck.setText(
"CHECK");
        
        
/*label initializes*/
        labelMessage 
= new Label(sShell, SWT.NONE);
        labelMessage.setBounds(
new Rectangle(326710217));
        labelMessage.setText(
"Information:");
        
        
/*textArea initializes*/
        textArea 
= new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
        textArea.setBounds(
new Rectangle(016348239));
    
        
/*text initializes*/
        text 
= new Text(sShell, SWT.BORDER);
        text.setBounds(
new Rectangle(228634533));
        text.setEditable(
false);
        
        
/*Mouse click action*/
        buttonCheck.addSelectionListener(
new SelectionListener() {
            
public void widgetSelected(SelectionEvent event) {
                
if(check())
                    text.setText(
"Right input");
                
else 
                    text.setText(
"Wrong input");
            }
            
public void widgetDefaultSelected(SelectionEvent event)
            {
                text.setText(
"Need to Click");
            }
        });    
    }

    /**
     * check all the rules
     * 
@return if match the rule return true ,else return false
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/    
     
private static boolean check(){
         String stringCheck
=textArea.getText();
         String contentPart 
=getContent(stringCheck);
         String command
= getCommand(stringCheck);         
         
if(!stringCheck.equals(""))
         {
                 
/* if remark part exist,check the rules
                  * all the check rules right the check 
                  * function return 1;else return 0
*/
             
if(checkPdu(stringCheck)&&checkCommand(command)
                     
&&checkChangeLineFirst(stringCheck)
                     
&&checkEnd(stringCheck)&&checkSecondLine(contentPart))
             {
                 
return true;
             }
             
else
             {
                 
return false;
             }
         }
         
else
         {
             
return true;
         }
    }
    
    
/**
     *  check pdu
     * 
@param checkString
     * 
@return true ,false
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static  boolean checkPdu(String checkString) {
            
            
if(checkString.length()>=start.length())
            {    
                String sub 
= checkString.substring(0,start.length());
                
if(sub.equals(start))
                {
                    
return true;
                }
            }
            
else
            {
                
return false;
            }
            
return true;
    }

    /**
     * get command
     * 
@param checkString
     * 
@return true ,false
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static String getCommand(String checkString){
        String command 
="";
        
if(checkString.length()>start.length())
        {
            
for ( int  i = start.length(); i < checkString.length(); i++)
            {
                    
if(checkString.charAt(i)==signalBegin)
                    {
                         command 
=checkString.substring(start.length(), i);
                         
return command;
                    }
            }
            
return checkString.substring(start.length());
        }
        
return null;
    }
        
    
/**
     * check the character whether number
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static boolean isDigit(char character){
        
if(character< '0' || character> '9')
        {
            
return false;
        }
        
else
        {
            
return true;
        }
    }
    
    
/**
     * check the character whether letter
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static boolean isLetter(char character){
        
if((character < 'a' || character> 'z')
            
&&(character < 'A' ||character >'Z'))
        {
            
return false;
        }
        
else
        {
            
return true;
        }
    }
    
    
private static boolean isSign(char character){
        
for(int i =0;i<signLib.length();i++)
        {
            
if(signLib.charAt(i)==character)
            {
                
return true;
            }    
        }
        
return false;
    }    
    
    
/**
     * check is Hex
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static boolean isHex(char character){
        
if((character < 'a' || character> 'f')
            
&&(character < 'A' ||character >'F'))
        {
            
return false;
        }
        
else
        {
            
return true;
        }
    }
    
    
/**
     * check Command,cannot begin with '-'and number
     * 
@param checkString
     * 
@return true,false
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-22)
     
*/

    private static boolean checkCommand(String checkString){
        
if(checkString!=null)
        {
            
if((checkString.charAt(0)!='_')
                    
&&(!isDigit(checkString.charAt(0))))
            {
                
for (  int i = 0; i < checkString.length(); i++)
                {
                    
if (!isDigit(checkString.charAt(i))
                        
&&(!isLetter(checkString.charAt(i))))
                    {
                        
return false;
                    }
                }
            }
            
else 
            {
                
return false;
            }
        }
        
return true;
        }    
    
    
/**
     * Check the first line change 
     * 
@param checkString
     * 
@return true,false
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static boolean checkChangeLineFirst(String checkString){
        
for(int i =0;i<checkString.length();i++)
        {
            
if(checkString.charAt(i)==signalBegin)
            {
                
if(i+1<checkString.length())
                {
                    
if(checkString.charAt(i+1)!=lineEndTag)
                    {
                        
return false;
                    }
                    
else 
                    {
                        
return true;
                    }
                }
            }
        }
        
return true;
    }
        
    
/**
     * check the Content start position
     * 
@param checkString
     * 
@return the start position of the content part
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     * 
     
*/
    
private static int contentStartPosition(String checkString){
        
for(int i =0;i<checkString.length();i++)
        {
                
if(checkString.charAt(i)==lineChangeTag)
                {
                    
return i;
                }
        }
        
return 0;
    }
    
    
/**
     * check the Content end position
     * 
@param checkString
     * 
@return the end position of the content part
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static int contentEndPosition(String checkString){
        
for(int i =0;i<checkString.length();i++)
        {
            
if(checkString.charAt(i)==signalEnd)
            {
        
// retrun content end . In windows OS,   is the line change tag
                return i-2;
            }
        }
        
return checkString.length();
    }    
    
    
/**
     * get substring about the Content part
     * 
@param checkString
     * 
@return string of content part
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     * 
     
*/
    
private static String getContent(String checkString)
    {
        
int ContentStarPosition = contentStartPosition(checkString);
        
int MarkPosition =checkString.indexOf(remarkTag);        
            
if(ContentStarPosition!=0)
            {
                
if(MarkPosition>0)
                {
                    String ContentPart 
= 
                    checkString.substring(ContentStarPosition
+1, MarkPosition);
                    
return ContentPart.trim();//return string with out space
                }            
                
else 
                {
                    
int ContentEndPosition = contentEndPosition(checkString);
                    
/*defense starposition out of arrayif no input*/
                    String ContentPart 
=checkString
                    .substring(ContentStarPosition
+1, ContentEndPosition);
                    
return ContentPart.trim();//return string with out space
                }                
            }
            
else return null;
    }
    

    /**
     * check Content
     * 
@param checkString
     * 
@return if right return 1,else return function remark
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static int checkContent(String checkString){
        
if(checkString!=null)
        {
            
for(int i =0;i<checkString.length();i++)
            {
                
if(checkString.charAt(i)!=stringTag)
                {
                    
if(checkString.charAt(i)!=remarkTag)
                    {
                        
if ((!isDigit(checkString.charAt(i))
                            
&&(!isHex(checkString.charAt(i)))))
                            
return 0;                    
                    }
                    
else return  checkRemark(checkString);
                }
                
else return 1;
            }
        }
        
return 1;
    }            
    
    
/**
     * check String
     * 
@param checkString
     * 
@return if rigtht return 1, else return function 
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     *  
     
*/
    
private static int checkString(String checkString)
    {
        
int i= 0;
        
if(checkString!=null)
        {
            
if(checkString.charAt(i)!=remarkTag)
            {
                
for( i= 0;i<checkString.length();i++)
                {
                    
if (!isDigit(checkString.charAt(i))
                        
&&(!isLetter(checkString.charAt(i)))
                        
&&(!isSign(checkString.charAt(i))))
                        
return 0;
                }
            }
            
else return  checkRemark(checkString);                    
        }
        
return 1;
    }
    
    
/**
     * check the remark part
     * 
@param checkString
     * 
@return return the length of the remark part
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     * 
     
*/
    
private static int checkRemark(String checkString){
        
int i =0;
        
if(checkString.charAt(i)==remarkTag)
        {
            
/* in remark part, just return the end of the remark part*/
            
for(i=0;checkString.charAt(i)!=lineEndTag;i++)
            {        
            }
            
return i;
        }
        
return 1;
    }
    
    
/**
     * check the data part.both check the hex and the string
     * 
@param checkString
     * 
@return true,false
     * 
@author tianrenliang
     * 
@since v0.1(2007-10-23)
     
*/
    
private static boolean checkSecondLine(String checkString)
    {
        
if(checkString!=null)
        {
            String StringPart 
= checkString;
            String subString 
=checkString;
            
int a=1,b=1,c=1,d=1;
            
int leftLength=checkString.length();
             
while(leftLength > 0)
           {           
                
int StringStartPosition = 0;
               
int StringEndPosition = StringStartPosition;
               StringStartPosition 
= subString.indexOf(stringTag);            
               String DataPart 
= "";
               
if(StringStartPosition>=0)
               {
                   DataPart 
= subString.substring(0, StringStartPosition);
                   a
=checkContent(DataPart);
                   StringPart 
= subString.substring(StringStartPosition + 1);
                   StringEndPosition 
= S

抱歉!评论已关闭.