0

I am writing a program that is supposed to count the number of vowels in an inputted (by the user) body of text.

import java.util.*;

public class Vowels{
  public static void main(String[] args){
    Scanner console = new Scanner(System.in);
    int counter= 0;
    System.out.println("\nInput a sequence of characters");
    String text = console.nextLine();
    text = text.toLowerCase();
    
    for (int loop = 0; loop < text.length(); loop++){
      char lett = text.charAt(loop);
      if (lett=='a' || lett=='e' || lett=='i' || lett=='o' || lett=='u') {
        counter++;
      }
    }
    System.out.println("There are " + counter + " vowels in the inputted text.");
  }
}

Whenever I run this code, there's no input box for the user to input text.
If I change the "String text = console.nextLine();" to "String text = console.next();" then only the first word is taken into account.

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Eric Cute
0

> Whenever I run this code, there's no input box for the user to input text.
It's a console application - you type in the console. Working as intended.

> If I change the "String text = console.nextLine();" to "String text = console.next();" then only the first word is taken into account.
Also working as intended. Those are two different methods which do two different things. You can read the description of each in the API doc for the Scanner class.

0

If you want an Input Box to input your sentence then you need to build a GUI. Try searching for JTextField.

This article 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.