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.

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.