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.

Dani AI

Generated

Short answer for : nothing is "wrong" with nextLine() — your program is a console application, so you must type into the console that launched it. If you launched it by double‑clicking a JAR on Windows you typically won't see a console. Run it from a terminal (for example java -jar Vowels.jar) or the IDE's Console / Output view so the prompt and input are visible. See the Scanner API for behavior details (Scanner docs).

If the symptom is that nextLine() seems to be "skipping" (common thread title), that usually happens when you mix token methods like nextInt()/next() with line methods. Those token methods leave the end‑of‑line in the buffer, so a subsequent nextLine() returns the leftover empty line. Fix by consuming the leftover newline before calling nextLine():

int n = console.nextInt();
console.nextLine(); // consume trailing newline
String line = console.nextLine(); // now reads the full line

As pointed out, typing happens at the console. As suggested, if you actually want a GUI input box then build a simple dialog instead of a console app. The easiest way is JOptionPane:

String text = JOptionPane.showInputDialog(null, "Enter text:");

(See JOptionPane docs.)

Extra tips: use System.out.print(...) and System.out.flush() if a prompt seems missing in some launchers, and rebuild/run from the IDE or command line when testing. For how to package/run a JAR from the command line, see Oracle's packaging tutorial (Packaging a Java Application).

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.