9 Replies - 1173 Views - Last Post: 18 January 2011 - 07:22 PM Rate Topic: -----

#1 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Small problem

Posted 18 January 2011 - 06:25 PM

I am trying to learn Java, and have written a method, yet it does not work as intended:

 public  void checkForVowels()
     {

         // Get the user input from the UI Textbox
         String UserString = jTextArea1.getText();

         // Declare our counter varibles.
         int Count = 0;
         int vowel = 0;

         // Loop through each character in the string.

         // There is an issue here as the program seems delayed.
         // Typing a vowel does not add automatically add to the vowel
         // count until the next character is added.

        while (Count < UserString.length() && UserString.length() > 0)
        {

            // Grab the next character from the string
            char MyLetter = UserString.charAt(Count);

            // Determine if the character is a vowel
            if (isVowel(MyLetter) == true)
            {

                // Vowel found! Add to the vowel count
                vowel ++;
            
            }

            // Add to the total charater count for compairing to the string length
            Count ++;

        } 

         // Change the jTextField text
         jTextField1.setText( "There are " + vowel + " vowels in your text.");

    }


If I type a vowel into the textarea, it does not add to the result until another character is typed. The loop should go through all characters in the string, as far as I calculate.

The action is declared here:

 private void jTextArea1KeyTyped(java.awt.event.KeyEvent evt) {

        // Update on key press
        checkForVowels();

    }

This post has been edited by vbnet9: 18 January 2011 - 06:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Small problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Small problem

Posted 18 January 2011 - 06:35 PM

Make your loop that way a lot clearer and shorter
         // Declare our counter varibles.
         int vowel = 0;

         // Loop through each character in the string.
        for(int i = 0; i < UserString.length(); ++i)
        {

            // Grab the next character from the string
            char MyLetter = UserString.charAt(i);

            // Determine if the character is a vowel
            if (isVowel(MyLetter) == true)
            {
                // Vowel found! Add to the vowel count
                vowel ++;
            }
       } 



As for your textArea you seem to use a KeyListener, you added that way ?

textArea.getDocument().addDocumentListener(new KeyListener());
Was This Post Helpful? 0
  • +
  • -

#3 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Small problem

Posted 18 January 2011 - 06:38 PM

I was going to use a for-loop, but a while loop seems more effective since that is the possibility that is does not need to run in cases when there is a 0 length.

I did not add a keylistner, I just used the events function in the NetBean GUI jframe form editor for the event of when a key is typed in the textarea.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Small problem

Posted 18 January 2011 - 06:42 PM

View Postvbnet9, on 18 January 2011 - 08:38 PM, said:

I was going to use a for-loop, but a while loop seems more effective since that is the possibility that is does not need to run in cases when there is a 0 length.

I won't i < string.length() and string.length will be < 0

Quote

I did not add a keylistner, I just used the events function in the NetBean GUI jframe form editor for the event of when a key is typed in the textarea.

events function in the NetBean GUI that does not eist in your code. This is NetBeans stuff that it uses to generate code.
Check the horrible code produced... sure it did used one of the 2 techiques
Was This Post Helpful? 0
  • +
  • -

#5 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Small problem

Posted 18 January 2011 - 06:45 PM

There is something wrong with using NetBeans events? I am not learning the correct method?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Small problem

Posted 18 January 2011 - 06:54 PM

View Postvbnet9, on 18 January 2011 - 08:45 PM, said:

There is something wrong with using NetBeans events? I am not learning the correct method?

Not a good idea at all to use a GUI generator to start with.
And you are a good example just now :) There is not such a thing as a NetBeans events, actually yes they might exist within NetBeans but it is just an expression to say "click on this button and I will generate the appropriate Java KeyEvent for you" (probably same thing for ButtonEvent, ComboEvent, RadioButton event, CheckBox event....

So you will learn what is a NetBeans event, but you won't know what in does in Java under the hood. An one day, you will apply for a Java job, they will ask you to write a simple Java program... using Notepad and you won't have a slightest clue on how to do it :(
Was This Post Helpful? 0
  • +
  • -

#7 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Small problem

Posted 18 January 2011 - 06:56 PM

Well I have been writing in JGrasp, but I do not know how to make a GUI in JGrasp. I thought it had to be done in NetBeans. Or is Jgrasp bad too?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Small problem

Posted 18 January 2011 - 07:08 PM

View Postvbnet9, on 18 January 2011 - 08:56 PM, said:

Well I have been writing in JGrasp, but I do not know how to make a GUI in JGrasp. I thought it had to be done in NetBeans. Or is Jgrasp bad too?

Use Jgrasp or NetBeans the one you are the more confortable with.
But write you GUI by hand, not the GUI gnerator of your IDE.

Swing is not that complicated and there are a lot of tutotials, here and on the net.

Also you can find many posts in this forum where people post problematic and we (many DICs) always reply: "We won't even try to decipher GUI generated code" this type of code can't hardly be read by human beings. And you will never seen, but for prototypes or prove of concepts, that type of code in production in the field.

Free advice worthes every cent :)
Was This Post Helpful? 0
  • +
  • -

#9 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: Small problem

Posted 18 January 2011 - 07:18 PM

Okay. I figured it out and used the keyreleased event. I will see what I can do using swing like I was in jgrasp. My instructor talks about Netbeans all the time so I thought it was the better one.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Small problem

Posted 18 January 2011 - 07:22 PM

View Postvbnet9, on 18 January 2011 - 08:25 PM, said:

I am trying to learn Java,

You might learn Java using JGrasp or NetBeans but you will never learn Java if you use NetBeans GUI generator
Good luck and happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1