0

so basically for my uni work i have to create an applet that gives a word pattern for a particular sentence, ie '1 am a man' would give 2, 1, 1, because there are 2 1 letter words, 1 2 letter word and 1 3 letter word. the harder part (atleast for me) is that i have to exclude all punctuation ( i, i would come out as 2).

My lecturer said this bit of code should work str = str.replaceAll("[^A-Za-z]", ""); but the problem is i dont know how to intergrate it into my applet, spent ages looking at it. this is basically the last part of the applet (this plus a graph). hopefully you understand what i mean, if you can help me out it would be much appreciated. also on a side note i haven't actually been taught str.replace yet.

This is what i have so far (hopefully ive put this in correctly)

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);
        }
    }

Kind Regards Jack5848

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

That piece of code looks through the string str and replaces everything that's not a letter with "" (nothing) - thus deleting everything that's not a letter.
You need to do the same thing to your string after you get it from the user but before you start to analyse it.
You code has no comments, unhelpful variable names, and seems to re-use the same variables for quite different purposes, so there's no way I'm going to try to understand it,but I guess you will know which variable has the string that you're going to analyse, and where the analysis begins.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.