TheDavidTurner
TheDavidTurner

Reputation: 1

Count Vowels - Scanner, String

Im completely lost on what to do from here. How do I make the string to be processed are not to be input as command line arguments, but instead as a line of text read from standard input, and prompted for with a question mark?

Current Code

import java.util.*;

public class CountVowels {

public static void main (String[] args) {
        if (args.length ==1) {
        String str = args [0];
        int ofVowels = 0;
    for(int i=0;i <str.length();i++){
        if((str.charAt(i) == 'a') || 
            (str.charAt(i) == 'e')  ||
            (str.charAt(i) == 'i') || 
            (str.charAt(i) == 'o') ||
            (str.charAt(i) == 'u')) {
            ofVowels ++;


        }

    }
    System.out.println(" In The String  "+str+", there are "+ofVowels+" vowels");
}
}
}

Upvotes: 0

Views: 405

Answers (1)

Jalitha De Silva
Jalitha De Silva

Reputation: 62

Firstly, you need to have import java.util.Scanner;

Then you go:

Scanner s = new Scanner(System.in); // create and attach the scanner
System.out.println("?");
String str = s.nextLine(); // gets what the user typed

Edit: Adding to your code, this makes it:

Scanner s = new Scanner(System.in); // create and attach the scanner
System.out.println("?");
String str = s.nextLine(); // gets what the user typed
int ofVowels = 0;
for(int i=0;i <str.length();i++){
    if((str.charAt(i) == 'a') || 
        (str.charAt(i) == 'e')  ||
        (str.charAt(i) == 'i') || 
        (str.charAt(i) == 'o') ||
        (str.charAt(i) == 'u')) {
        ofVowels ++;
    }
}
System.out.println(" In The String "+str+", there are "+ofVowels+" vowels");

Upvotes: 1

Related Questions