2

所以基本上对于我的大学工作,我必须创建一个小程序,为特定句子提供单词模式,即“我是男人”会给出 2、1、1,因为有 2 个 1 字母单词、1 个 2 字母单词和1 个 3 个字母的单词。更难的部分(至少对我来说)是我必须排除所有标点符号,例如 i, i 会以 2 的形式出现,因为逗号被排除在字符之外。

我的讲师说这段代码应该可以工作 str = str.replaceAll("[^A-Za-z]", ""); ,但问题是我不知道如何将它集成到我的小程序中,花了很长时间看它。这基本上是小程序的最后一部分(这加上一个图表)。希望你明白我的意思,如果你能帮助我,将不胜感激。顺便说一句,我实际上还没有被教过 str.replace 。

这就是我到目前为止所拥有的(希望我正确地输入了这个)

import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class clarke_j_reass  extends Applet implements ActionListener
{
    Button pr_input1, pr_input2;

    Label pr_label;
    TextField pr_text;
    String pr_name;

    public void init()

    {
        pr_input1 = new Button("Analyze");
        pr_input2 = new Button("Reset");
        add(pr_input1);
        add(pr_input2);
        pr_input1.addActionListener(this);
        pr_input2.addActionListener(this);
        //add the buttons with action listeners
        pr_label = new Label("Word Pattern");
        add(pr_label);
        pr_text = new TextField();
        add(pr_text);
        pr_text.addActionListener(this);
        //add text field
    }    
    public void start()
    {           
        pr_name="";
            setSize(400,400);
            setBackground(Color.gray);
        pr_text.setBackground(Color.white);      
    }

    public void actionPerformed(ActionEvent e){


    pr_name = e.getActionCommand();
    repaint();
        if(e.getSource() == pr_input1)
            pr_name = pr_text.getText();
        else
            if(e.getSource() == pr_input2)
            {   pr_name = "";        
                pr_text.setText("");
                }
         repaint();
             // The user's input from the text area.                     
            int pr_char;

           String array[]=pr_name.split(" ");
           int counter=0;
           for(int i=0;i<array.length;i++)
               if(counter<array[i].length())
                   counter=array[i].length();
           int intArray[]=new int[counter];
           for(int i=0;i<intArray.length;i++){
               intArray[i]=0;
           }
           for(int i=0;i<array.length;i++){
               intArray[array[i].length()-1]++;
           }
           String a="";
           for(int i=0;i<intArray.length;i++){
               if(intArray[i]>0)
               {
                a+=String.valueOf(intArray[i]);
                a+=", ";
               }
           }
           pr_label.setText(a);


           pr_char = pr_name.length(); 
    }

    public void paint(Graphics g)
    {
  //      g.setColor(pr_col);
     //   g.drawString(pr_name,0,250);
      pr_text.setSize(400, 200);
      pr_text.setLocation(0,0);
      pr_input1.setLocation(150,220);
      pr_input2.setLocation(200,220);
      pr_label.setLocation(0,270);
      pr_label.setSize(400,30);
    }
}

空格不是问题,因为代码不包含它们,我只查找单词。我现在遇到的问题是,当我将代码放在我被告知的位置时,它并没有给我正确的模式,而是将其限制为 1。即我是一个男人出来的时候是 1,而不是 2、1、1,

4

4 回答 4

1

定义为后,用循环中array[i].length()的变量替换所有实例:lengthlength

int length = array[i].replaceAll("[^A-Za-z]", "").length();

代替:

       for(int i=0;i<array.length;i++)
           if(counter<array[i].length())
               counter=array[i].length();

和:

       for(int i=0;i<array.length;i++) {
           int length = array[i].replaceAll("[^A-Za-z]", "").length();
           if(counter<length)
               counter=length;
       }
于 2012-07-15T13:00:39.987 回答
0

This is an example of Regular Expressions. The first string in the replaceAll call defines the pattern of what it's looking for.

In this case, the "^" character means "not" and "A-Za-z" means all uppercase and lowercase letters. So the overall pattern means, anything that's not a letter. That's the wrong pattern if you want to split on spaces, so you also have to add "\s" to include whitespace characters. Thus, the call:

str = str.replaceAll("[^A-Za-z\s]", "")

Will replace anything that's not a letter or a whitespace character with an empty string.

In your program, you would insert this call before you do your string processing. Like this:

pr_name = pr_name.replaceAll("[^A-Za-z\s]", "")
String array[]=pr_name.split(" ");
于 2012-07-15T12:56:27.887 回答
0

代替

for(int i=0;i<array.length;i++){
   intArray[array[i].length()-1]++;
}

for(int i=0;i<array.length;i++){
   intArray[array[i].replaceAll("[^A-Za-z]", "").length()-1]++;
}
于 2012-07-15T13:11:07.090 回答
-1

Place your instructor's suggestion above this:

String array[]=pr_name.split(" ");

It appears you're storing the text string as pr_name so before you split on the spaces, you can strip the other characters. It should appear like this:

pr_name = pr_name.replaceAll("[^a-zA-Z\s:]", "");
String array[]=pr_name.split(" ");
于 2012-07-15T12:56:30.207 回答