Again I have been given an assignment and have done it completely wrong. Please understand that I am completely new to programing and made the mistake of entering an accelerated class. I'm not looking for an A, I just want to pass and seek out help as often as I can get any. The assignment outline: I forgot to make them methods as my code is below which works but as can be found in the notes has some problems.
_________________________________________________________________________________________
Write a Java program to gather information about strings. Write your own user-defined
methods to calculate vowel, digit, and white space counts using some Character class
methods. The Character class methods DO NOT count characters for you, that is what
you must do.
There will be a loop in your main() method to read strings from the user until end of file
(EOF) has occurred.
With each String read from the user, perform the following:
1) Report how many characters are in the string (length() String class method).
2) Report how many vowels are in the string (no Java method exists to help with
this).
3) Report how many digits are in the string (isDigit() Character class method).
4) Report how many letters are in the string (isLetter() Character class method).
5) Report how many white-space characters are in the string (isWhitespace()
Character class method).
Your output should resemble (assuming a Unix system):
Enter a string for character classification: (EOF to end):
The quick brown fox jumps over the lazy dog.
inputLine = "The quick brown fox jumps over the lazy dog."
inputLine is 44 characters long and contains the following:
8 whitespace characters.
0 digits.
35 letters.
11 vowels.
Enter a string for character classification: (EOF to end):
1001001
SOS!
inputLine = "1001001
SOS!"
inputLine is 18 characters long and contains the following:
1 whitespace characters.
7 digits.
3 letters.
1 vowels.
Enter a string for character classification: (EOF to end):
<CTRLD>
_________________________________________________________________________________________
*****************************************************************************************_________________________________________________________________________________________
The assignment I had started before i realized I did it wrong is... (almost works correctly though)
// imports the java utility file to include the scanner class needed for user input
import java.util.*;
// determines that everyone can access this program and the name of the program
public class p6
{
// deteremines that the keyboard will be the scanner or "input device" used for the collected information from the user
static Scanner kb = new Scanner(System.in);
// determines the main method to be used for entry of this program
public static void main(String[] args)
{
// varibles to be used in the program
String inputLine;
int length = 0, digit = 0, whitespace = 0,letter = 0, vowel = 0;
// prints instructions for the user
System.out.println("Enter a string for character classification: (EOF to end):");
// hold in memory what the user entered
inputLine = kb.nextLine();
// begins the loop with what the user entered in memory
while (kb.hasNext())
{
// varible determining the legnth of the string entered by the user
length = inputLine.length();
// puts into memory the string the user entered and saves it as inputLine
inputLine = kb.nextLine();
// determines whether each indiviual character of the user entered string is a letter (is counting cumilative for each string still)
for (int x = 0; x < inputLine.length(); x++)
if (Character.isLetter(inputLine.charAt(x)))
letter++;
// determines whether each indiviual character of the user entered string is a digit (is counting cumilative for each string still)
for (int z = 0; z < inputLine.length(); z++)
if (Character.isDigit(inputLine.charAt(z)))
digit++;
// determines whether each indiviual character of the user entered string is a space (is counting cumilative for each string still)
for (int y = 0; y < inputLine.length(); y++)
if (Character.isWhitespace(inputLine.charAt(y)))
whitespace++;
// my so far failed attempt at vowel counting of the user entered string
// is giving me a number that usually exceeds 100 for some reason, still needs alot of work
// supposed to look at each vowel and change it into an interger count
for (int a = 0; a < inputLine.length(); a++)
vowel = inputLine.charAt(a);
switch (vowel)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': vowel++;
break;
}
// displays the results of the user entered string for the user
System.out.println("InputLine = " + inputLine + ".");
System.out.println("InputLine is " + length + " characters long and contains the following:");
System.out.println("" + whitespace + " whitespace characters.");
System.out.println("" + digit + " digits.");
System.out.println("" + letter + " letters.");
System.out.println("" + vowel + " vowels.");
// displays the begining of a new loop
System.out.println("Enter a string for character classification: (EOF to end):");
}
}
}