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.
Code :
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.");
}
Re: text.length method problem?
Quote:
Originally Posted by
computercoder
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.
Code :
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.
Code java:
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.
Re: text.length method problem?
Use console.nextLine(), as next returns only the first word of the input
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:
Quote:
Input a sequence of characters
There are 0 vowels in the inputted text.
So an input box doesnt even show up
.......???
Re: text.length method problem?
Hmmm. Maybe console needs to be static.
Yep, now it works:
Code java:
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.");
}
}
Re: text.length method problem?
Seems to work for me...
Code :
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.
Re: text.length method problem?
Quote:
Originally Posted by
javapenguin
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?