Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: text.length method problem?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default text.length method problem?

    I am attempting to create a program that counts the number of vowels in a sentence or text, which is inputted by the user.
    PROBLEM: I am trying to use the length of the string to continue my loop until the very last character has been reached. However, the text.length only recognizes the first word and stops after spaces.

    import java.util.*;
     
    public class Vowels{
      public static void main(String[] args){
        Scanner console = new Scanner(System.in);
     
        int counter= 0;
        System.out.println("Input a sequence of characters");
        String text = console.next();
     
        for (int loop = 0; loop < text.length(); loop++){
          char lett = text.charAt(loop);
          if (lett=='a' || lett=='e' || lett=='i' || lett=='o' || lett=='u' || lett=='A' || lett=='E' || lett=='I' || lett=='O' || lett=='U') {
            counter++;
          }
        }
        System.out.println("There are " + counter + " vowels in the inputted text.");
      }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: text.length method problem?

    Quote Originally Posted by computercoder View Post
    I am attempting to create a program that counts the number of vowels in a sentence or text, which is inputted by the user.
    PROBLEM: I am trying to use the length of the string to continue my loop until the very last character has been reached. However, the text.length only recognizes the first word and stops after spaces.

    import java.util.*;
     
    public class Vowels{
      public static void main(String[] args){
        Scanner console = new Scanner(System.in);
     
        int counter= 0;
        System.out.println("Input a sequence of characters");
        String text = console.nextLine();
     
        for (int loop = 0; loop < text.length(); loop++){
          char lett = text.charAt(loop);
          if (lett=='a' || lett=='e' || lett=='i' || lett=='o' || lett=='u' || lett=='A' || lett=='E' || lett=='I' || lett=='O' || lett=='U') {
            counter++;
          }
        }
        System.out.println("There are " + counter + " vowels in the inputted text.");
      }
    String.trim() will return a copy of the String without trailing and preceding whitespaces.

    The number of vowels in that String should be the same as the full String that has the spaces.

    import java.util.*;
     
    public class Vowels{
      public static void main(String[] args){
        static Scanner console = new Scanner(System.in);
     
        int counter= 0;
        System.out.println("Input a sequence of characters");
        String text = console.nextLine();
        String shorterString = text.trim();  
     
        for (int loop = 0; loop < shorterString.length(); loop++){
          char lett = shorterString.charAt(loop);
          if (lett=='a' || lett=='e' || lett=='i' || lett=='o' || lett=='u' || lett=='A' || lett=='E' || lett=='I' || lett=='O' || lett=='U') {
            counter++;
          }
        }
        System.out.println("There are " + counter + " vowels in the inputted text.");
      }

    Also, maybe Zula is right. Maybe we don't need to have the second String.
    Last edited by javapenguin; November 2nd, 2010 at 03:05 PM.

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: text.length method problem?

    Use console.nextLine(), as next returns only the first word of the input

  4. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 2nd, 2010)

  5. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: text.length method problem?

    Now when I change it to .nextLine the scanner doesnt even work. It skips the instruction and the result is:


    Input a sequence of characters
    There are 0 vowels in the inputted text.
    So an input box doesnt even show up
    .......???

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: text.length method problem?

    Hmmm. Maybe console needs to be static.

    Yep, now it works:

    import java.util.*;
     
    public class Vowels{
    	static Scanner console = new Scanner(System.in);
      public static void main(String[] args){
     
     
     
     
        int counter= 0;
        System.out.println("Input a sequence of characters");
        String text = console.nextLine();
        String shorterString = text.trim();  
     
        for (int loop = 0; loop < shorterString.length(); loop++){
          char lett = shorterString.charAt(loop);
          if (lett=='a' || lett=='e' || lett=='i' || lett=='o' || lett=='u' || lett=='A' || lett=='E' || lett=='I' || lett=='O' || lett=='U') {
            counter++;
          }
        }
        System.out.println("There are " + counter + " vowels in the inputted text.");
      }
    }
    Last edited by javapenguin; November 2nd, 2010 at 03:10 PM.

  7. #6
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: text.length method problem?

    Seems to work for me...
    import java.util.*;
     
    public class Vowels2{
      public static void main(String[] args){
        Scanner console = new Scanner(System.in);
     
        int counter= 0;
        System.out.println("Input a sequence of characters");
        String text = console.nextLine();
     
        for (int loop = 0; loop < text.length(); loop++){
          char lett = text.charAt(loop);
          if (lett=='a' || lett=='e' || lett=='i' || lett=='o' || lett=='u' || lett=='A' || lett=='E' || lett=='I' || lett=='O' || lett=='U') {
            counter++;
          }
        }
        System.out.println("There are " + counter + " vowels in the inputted text.");
      }
    }


    I get
    Input a sequence of characters
    a a <-----my input
    There are 2 vowels in the inputted text.

    Input a sequence of characters
    You kicked my dog!
    There are 5 vowels in the inputted text.
    Last edited by Zula; November 2nd, 2010 at 03:09 PM.

  8. The Following User Says Thank You to Zula For This Useful Post:

    javapenguin (November 2nd, 2010)

  9. #7
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: text.length method problem?

    Quote Originally Posted by javapenguin View Post
    Hmmm. Maybe console needs to be static.
    Why do you think declaring it outside of main and making it static, instead of declaring and instantiating it inside of main, makes a difference to the functionality of this code?

Similar Threads

  1. Comparing Strings only using the .length() method - possible?
    By Ryker in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2010, 05:52 PM
  2. Javamail html mail sended as text and headers problem
    By johnymj in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 29th, 2010, 09:22 AM
  3. Problem with a JFrame method
    By Lakmini Kuruppu in forum AWT / Java Swing
    Replies: 1
    Last Post: May 20th, 2010, 08:40 AM
  4. Run Length Encoding Problem
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 7th, 2010, 07:24 AM
  5. java. Text problem...
    By Ranger-Man in forum Java SE APIs
    Replies: 8
    Last Post: September 7th, 2009, 07:19 PM

Tags for this Thread