Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default Need a bit of help with a bit of code

    hi Im fairly new to Java and I have made a program. I just cant seem to make it count the characters or reset it to the start using the button. Also I'm trying to include a word counter within the program thanks. Also every time I run the code it just goes in a continuous loop
    here's the code:



    import javax.swing.*;
    import java.awt.event.*;
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class Analyzer extends Applet implements ActionListener
    {
    Button AnalyzeButton;
    Button ResetButton;
    TextField TextEntry;
    Label character;
    Label words;
    int charCount;
    int wordCount;
    int c=0;
    int w=0;

    public void init ()
    {
    setLayout (new FlowLayout ());
    AnalyzeButton = new Button ("Analyze");
    ResetButton = new Button ("Reset");
    TextEntry = new TextField ("Enter Text", 50);
    character = new Label ("Number of Charactes: " +c);
    words = new Label ("Number of Words: " +w);

    add (TextEntry);
    add (AnalyzeButton);
    add (ResetButton);
    add (character);
    add (words);


    AnalyzeButton.addActionListener(this);



    ResetButton.addActionListener (this);



    }


    public void actionPerformed (ActionEvent e)
    {
    if(e.getSource() == AnalyzeButton);
    {
    int charCount = (TextEntry.getText().length());
    int i=0;
    for (i=0;;)
    {

    {
    c++;
    character.setText("Character Count: " + c);
    AnalyzeButton.setEnabled(false);
    }

    {
    }

    if(e.getSource() == ResetButton);
    {
    c=0;
    TextEntry.setText("Enter Text");
    character.setText("Number of Characters"+c);
    AnalyzeButton.setEnabled(true);
    }



    }
    }

    }

    public void stop (){

    }
    public void destroy (){
    }



    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You should really be using swing and not awt, so JButton, JXxxx instead of Buttons and such. Also, variable names should be first letter first word lowercase, rest of the words capitalized first letter.

    I haven't checked the logic yet of your real problem, I will soon.

    I suggest you head here: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Oh boy, this looks like a mess. I don't see a JFrame, how are you adding these things to anything? Why are you using addActionListener(this) and e.getSource() instead of just making a new ActionListener for each button?

    Also you don't have any comments in your code, so I'm guessing at what everything is supposed to do. Does it enter an infinite loop as soon as you run it, or when you press a button?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    when ever I press the button it enters into an infinite loop...
    sorry about no comments

  5. #5
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Now I'm taking a stab at it, but I think its because your entire program is the actionlistener you might be running into a problem there. While you could potentially solve it that way, you would have to add conditions to check. Someone please correct me if I'm wrong, but it looks like when you click a button you enter into what looks like a while(true) loop with no termination conditions.

    Is this the entire standalone program? Or are there other parts you haven't posted?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Like I said, you may want to read the tutorials at the link I provided.

  7. #7
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ignore the gui builder tutorials, you shouldn't be focusing on those until you know Swing.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  8. #8
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    This is the entire program. as i say I'm fairly new to Java and could really use the help from you guys.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yeah, I've never extended Applet before so I'm not sure how you're even drawing a GUI. I guess there's something in Applet that does some stuff for you.

    Honestly, read the tutorials man. There is way to much broken stuff in your code. I would suggest learning the basics of Swing before trying to extend Applet and mess with that. You should have a separate action listener for each of your buttons, they don't do similar things so the action listener should be different. At least from my experience.

    To help you with your current code I think we would have to teach you GUI, and I don't think anyone here is going to play that role. So once you get a decent grasp on Swing, if you still have questions feel free to ask them.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  10. #10
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Ok cheers for that guys Ill come back later, with a new code aha thanks.

  11. #11
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Hi guys right I've given it another crack at this code and I'm struggling with making the analyze button: count the amount of characters. And the reset clear all the data. This is the code so far:



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

    public class Analyzer extends JFrame implements ActionListener
    {
    JTextField textField = new JTextField (50);
    JButton analysis = new JButton("Analyzer");
    JButton reset = new JButton ("Reset");
    JLabel myLabel = new JLabel ("Enter text to be analyzed", JLabel. CENTER);
    JLabel wordcounter = new JLabel ("number of words used ");
    JLabel charactercounter = new JLabel ("nuber of characters used ");
    int charCount;
    int c=0;


    public Analyzer ()
    {
    setTitle ("Text Analyzer");
    setLayout (new FlowLayout ());
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    setSize (600,500);
    setLocation (400, 300);
    add (textField);
    add (analysis);
    add (reset);
    add (myLabel);
    reset.addActionListener(this);
    setVisible(true);
    analysis.addActionListener(this);
    setVisible(true);
    }

    public void actionPerformed (ActionEvent e)
    {
    String userText;
    userText = textField.getText();
    myLabel.setText ("you entered "+"\"" + userText + "\"" );
    int charCount = (textField.getText().length());
    int i=0;
    for (i=0;;)
    {

    {
    c++;
    charactercounter.setText("Character Count: " + c);



    }
    }
    }
    }

  12. #12
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Hello,

    I don't know why these normally so friendly people are so picking on you, but a bit right they are ;>)
    (The reason I do this "spoonfeeding" is that otherwise it is very hard to tell where to start commenting, so read those comments.)
    I didnt look much at your second try, but did some corrections (if you don't understand them afterwards, ask.):

    Java Code:
    package misery;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    public class Analyzer extends JApplet implements ActionListener
    {
    	private int charCount = 0;
    	private int wordCount = 0;
    	private JButton analyzeButton = new JButton( "Analyze" );
    	private JButton resetButton = new JButton( "Reset" );
    	private JTextField textEntry = new JTextField( "Enter Text", 20 );
    	private JLabel character = new JLabel( "Number of Charactes: " + charCount );
    	private JLabel words = new JLabel( "Number of Words: " + wordCount );
    	
    	public void init (){
    		Container pane = getContentPane();
    		pane.setLayout( new FlowLayout() );
    		pane.add( textEntry );
    		pane.add( analyzeButton );
    		pane.add( resetButton );
    		pane.add( character );
    		pane.add( words );
    		
    		analyzeButton.addActionListener( this );
    		resetButton.addActionListener( this );
    	}
    	
    	
    	public void actionPerformed ( ActionEvent e ){
    		if( e.getSource() == analyzeButton ){
    			charCount = ( textEntry.getText().length() );
    			character.setText( "Character Count: " + charCount );
    			analyzeButton.setEnabled( false );
    		}
    		if( e.getSource() == resetButton ){
    			charCount = 0;
    			textEntry.setText( "Enter Text" );
    			character.setText( "Number of Characters" + charCount );
    			analyzeButton.setEnabled( true );
    		}
    	}
    }
    Comments:
    Made fields private
    Restored indentation
    Made all variables start with lowercase chars.
    Threw stop() and destroy() out
    The curly brackets are sometimes empty or misplaced. Reshuffled them.
    The if-statements are directly followed by ";", and thus doing nothing (except going into an endless loop). Removed.
    Changed all awt-Controls to Swing, Applet to JApplet.
    Initialisation fields removed from init() and combined with declarations (not necessary, but cleaner)
    Reshuffled the declarations to avoid illegal forward declarations.
    Introduced Container pane. Not absolutely necessary, but adding to JApplet directly is not advised.

    Made the textfield bit shorter.
    Declaring i outside of loop makes no sense
    Loop itself makes no sense. Both removed
    c is charCount. c removed. charCount local double declaration removed
    Idem wordCount (not working yet)

    Homework:
    - Use codetags and proper indentation in next posts.
    - Make the wordCount work
    - Remove the buttons and make recalculation being triggered by changing the textfieldcontent.
    Last edited by Jodokus; 07-09-2011 at 06:32 PM. Reason: Some formatting
    No bug ever had to calculate its fitnessfunction.

  13. #13
    Shimless12 is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default

    Ahh cheers an thanks alot ill get that homework done as well thanks!

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you do want to use buttons you should do either one of two things.

    1)Have each button have it's own unique listener. You can create an inner class which handles this, or use an anonymous inner class.

    2)if you has both buttons use the same listener(this), test which button is clicked with e.getSource().
    Last edited by sunde887; 07-09-2011 at 06:34 PM.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    You can create an inner lass which handles this
    I've heard of getting in touch with one's inner child, but I would really love to create an inner lass... any links?


  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by DarrylBurke View Post
    I've heard of getting in touch with one's inner child, but I would really love to create an inner lass... any links?

    Oops, I'm so bad at typing on the iPhone.

    Ninja edit inc

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,774
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by sunde887 View Post
    Oops, I'm so bad at typing on the iPhone.
    Phones are not suitable to type on; just like cauliflower; you can't type on cauliflower either, or herring or refrigerators. It's silly.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    My inner lass is doing the typing for me.
    No bug ever had to calculate its fitnessfunction.

  19. #19
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    Phones are not suitable to type on; just like cauliflower; you can't type on cauliflower either, or herring or refrigerators. It's silly.

    kind regards,

    Jos ;-)
    Maybe you can't type with cauliflower, however; I have mastered that art. The herring and refrigerator's are a different story.

  20. #20
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by sunde887 View Post
    Maybe you can't type with cauliflower, however; I have mastered that art. The herring and refrigerator's are a different story.
    All is not lost - I get the herring to type on the refrigerator

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  2. Code to check if a piece of code is legal.
    By vahshir in forum New To Java
    Replies: 3
    Last Post: 08-30-2010, 05:21 AM
  3. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 02:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •