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):");

	}
 }
}

Recommended Answers

All 18 Replies

Okay?

// 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++;

This little piece of your code examines the String inputLine and sets the variable letter to be the number of letters in the String. That's good.
It's very easy to make this into a method that takes the String as its parameter and returns an integer for the count of letters.
You can then easily replace this block of code with a call to your new method.

You may need to revise your course materials on how to define and call a method.

Give it a try and come back here if you have problems (don't forget to post whatever you have done so far).

I am still failing horribly even after reading the section on user defined methods... its not recognozing them. What am I doing wrong?

// 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 p6b
{

// 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 out a prompted line for the user to input a string
	System.out.println("Enter a string for character classification: (EOF to end):");

	// puts into memory "inputLine" the string the user entered
	inputLine = kb.nextLine();

	// variable determining the length of the string enter by the user
	length = inputLine.length();

	// prints out a copy of the string entered by the user
	System.out.println("InputLine = " + inputLine + ".");

	// prints out the results of how many characters are in the string that was entered by the user
	System.out.println("InputLine is " + length + " characters long and contains the following:");

	// user defined method that examines the string and determines how many characters are letters then prints out that result when the method is called
	public int letters()
	{
		for (int x = 0; x < inputLine.length(); x++)
		if (Character.isLetter(inputLine.charAt(x)))
		letter++;
		System.out.println("" + letter + " letters.");
	}

	// user defined method that examines the string and determines how many characters are digits then prints out that result when the method is called
	public int digits()
	{
		for (int z = 0; z < inputLine.length(); z++)
		if (Character.isDigit(inputLine.charAt(z)))
		digit++;
		System.out.println("" + digit + " digits.");
	}

	// user defined method that examines the string and determines how many characters are whitespaces then prints out that result when the method is called
	public int whitespaces()
	{
		for (int y = 0; y < inputLine.length(); y++)
		if (Character.isWhitespace(inputLine.charAt(y)))
		whitespace++;
		System.out.println("" + whitespace + " whitespace characters.");
	}

	// user defined method that examines the string and determines how many characters are vowels then prints out that result when the method is called
	public int vowels()
	{
		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;
				}
		System.out.println("" + vowel + " vowels.");
	}

	// begins the loop with the information entered by the user
	while (kb.hasnext())

		// calls the method letters
		letters();

		// calls the method digits
		digits();

		// calls the method whitespaces
		whitespaces();

		// calls the method vowels
		vowels();

		// prompts the user to end program or enter a new string returning to the begining of the loop
		System.out.println("Enter a string for character classification: (EOF to end):");
	}
}

Errors I am getting when I compile:
C:\Documents and Settings\Chalandria\Desktop\p6b.java:35: illegal start of expression
public int letters()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:35: ';' expected
public int letters()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:44: illegal start of expression
public int digits()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:44: ';' expected
public int digits()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:53: illegal start of expression
public int whitespaces()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:53: ';' expected
public int whitespaces()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:62: illegal start of expression
public int vowels()
^
C:\Documents and Settings\Chalandria\Desktop\p6b.java:62: ';' expected
public int vowels()
^
8 errors

Tool completed with exit code 1

You have put the method definitions inside the definition of the main method. That's illegal. You must put them in the class, but outside any other methods.
Then you just need to call them from inside the main method.

You have put the method definitions inside the definition of the main method. That's illegal. You must put them in the class, but outside any other methods.
Then you just need to call them from inside the main method.

So if I am to understand you correctly I need to move all the user defined methods to the top before the main method and outside of the brackets. Do I keep them with their definition in brackets? This is the part that is losing me. I can read that book until I'm blue in the face unless I can actually play around with examples I'm never going to figure this out, something the textbook is severely lacking.

class X {

   public void main(...) {
     ...
     myMethod(); // call method
   }

   void myMethod() { // define method
      ...
   }

}
class X {

   public void main(...) {
     ...
     myMethod(); // call method
   }

   void myMethod() { // define method
      ...
   }

}

Now see thats what is confusing me. Everything is telling me that the code is read from top to bottom, left to right, so why is it that when doing a user defined method you call the method before it is defined. If I am reading this correctly... I am just all sorts of confused right now.

The code within a method is executed top-to-bottom. But you can define the methods in any order you like. The compiler will process them and build a .class file where they can be accessed directly as needed at runtime.

I changed it and I am still getting illegal start of expression errors (See below) It doesn't seem to want to accept my methods at all. I reread the isDigit and others several times so I am sure that I have set the method statements up properly, I'm just not understanding why it is not working at this point... been trying to figure this out all night... what am I missing?

// 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 p6b

{

// 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)



// begins the loop with the information entered by the user
	while (kb.hasnext())


		// calls the method letters
		letters()

		// calls the method digits
		digits()

		// calls the method whitespaces
		whitespaces()

		// calls the method vowels
		vowels()

 {

	 // varibles to be used in the program
 	String inputLine;
 	int length = 0, digit = 0, whitespace = 0,letter = 0, vowel = 0;

	// prints out a prompted line for the user to input a string
	System.out.println("Enter a string for character classification: (EOF to end):");

	// puts into memory "inputLine" the string the user entered
	inputLine = kb.nextLine();

	// variable determining the length of the string enter by the user
	length = inputLine.length();

	// prints out a copy of the string entered by the user
	System.out.println("InputLine = " + inputLine + ".");

	// prints out the results of how many characters are in the string that was entered by the user
	System.out.println("InputLine is " + length + " characters long and contains the following:");



		// prompts the user to end program or enter a new string returning to the begining of the loop
		System.out.println("Enter a string for character classification: (EOF to end):");

			// user defined method that examines the string and determines how many characters are letters then prints out that result when the method is called
			public int letters()
			{
				for (int x = 0; x < inputLine.length(); x++)
				if (Character.isLetter(inputLine.charAt(x)))
				letter++;
				System.out.println("" + letter + " letters.");
			}

			// user defined method that examines the string and determines how many characters are digits then prints out that result when the method is called
			public int digits()
			{
				for (int z = 0; z < inputLine.length(); z++)
				if (Character.isDigit(inputLine.charAt(z)))
				digit++;
				System.out.println("" + digit + " digits.");
			}

			// user defined method that examines the string and determines how many characters are whitespaces then prints out that result when the method is called
			public int whitespaces()
			{
				for (int y = 0; y < inputLine.length(); y++)
				if (Character.isWhitespace(inputLine.charAt(y)))
				whitespace++;
				System.out.println("" + whitespace + " whitespace characters.");
			}

			// user defined method that examines the string and determines how many characters are vowels then prints out that result when the method is called
			public int vowels()
			{
				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;
						}
				System.out.println("" + vowel + " vowels.");
			}
	 }
	}

C:\Documents and Settings\Chalandria\Desktop\p6c.java:13: ';' expected
public static void main(String[] args)
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:60: illegal start of expression
public int letters()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:60: ';' expected
public int letters()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:69: illegal start of expression
public int digits()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:69: ';' expected
public int digits()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:78: illegal start of expression
public int whitespaces()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:78: ';' expected
public int whitespaces()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:87: illegal start of expression
public int vowels()
^
C:\Documents and Settings\Chalandria\Desktop\p6c.java:87: ';' expected
public int vowels()
^
9 errors

Tool completed with exit code 1

Your {} brackets are all screwed up. Go back to the tiny sample prog I gave you and fix the {} so that match the example. Start with line 13 - you have the main method's signature but the opening { for its definition is missing. Line 33 has a { that seems to be attached to nothing. Your code ends with three }}}, but the example has only two }}

Getting your indentation right is the best way to get your {} right. Indent each line when you pass a { and un-indent when you pass a }. If you get a programmer's editor or a proper IDE they will do it for you 100% correctly.

I lost all type of thought on what I was doing previously after rereading the book and my notes. So I tried doing it over again from scratch without being influenced by previous code and errors. Sadly I am still messing up the user defined methods. Below is a copy of my new code, please a little more help... oh yeah and to the girl who sits next to me in class (LOL) stop cheating and looking at my work :) sorry I forgot your name... I'll make it up to you and get ya some dunkin donuts coffee if you want.

Thanks in advance to whoever can help me get this figured out and pounded into memory.

import java.util.*;

public class p6final
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)
	{

	vowels(String inputLine);
	letters(String inputLine);
	whitespaces(String inputLine);
	digits(String inputLine);

	String inputLine;
	int length = 0, digit = 0, whitespace = 0,letter = 0, vowel = 0;
	inputLine = kb.nextLine();
	length = inputLine.length();

	public int vowels(String inputLine)
	for (int a = 0; a < inputLine.length(); a++)
		{
		 char a = inputLine.charAt(a);
		 if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U')
			{
			 vowel++;
			}
		}
	return count;

	public int letters(String inputLine)
	for (int x = 0; x < inputLine.length(); x++)
		{
		if (Character.isLetter(inputLine.charAt(x)))
			{
			letter++;
			}
		}
	return count;

	public int whitespaces(String inputLine)
	for (int y = 0; y < inputLine.length(); y++)
		{
		if (Character.isWhitespace(inputLine.charAt(y)))
			{
			whitespace++;
			}
		}
	return count;

	public int digits(String inputLine)
	for  (int z = 0; z < inputLine.length(); z++)
		{
		if (Character.isDigit(inputLine.charAt(z)))
			{
			digit++;
			}
		}
	return count;

	System.out.println("Enter a string for character classification: (EOF to end):");

	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.");

	System.out.println("Enter a string for character classification: (EOF to end):");
	}
}

Just realized I left out the loop... will have to add that too, but I guess I can do that after I get the user defined methods figured out.

Lines 10-13 are simply wrong. Get rid of them.

You are still trying to define your methods inside the main method. You are also missing the { } on all your method definitions (see my example below)

Your methods are confused as to how then communicate their results back to the main program. You can either:
1. Access a variable (eg digit) defined in the class, and update its value, or
2. Return the answer as a return value from the method
In general approach 2 is better.
Your methods use approach 1 (which is OK), and also kind of use method 2, except that the value "count" that they return doesn't actually get set in your methods

Lines 61 onwards, where you get the data and display the results are part of the digits method, but can never be executed because they come after a return statement.
They, and only they, need to be in the "main" method.

If you stick with approach 1 for your methods then you need to call all your methods after reading inputLine but before printing digit etc.
If you go for approach 2 (better) then you need to:
1. return the correct value from each method
eg

public int digits(String inputLine) {
   int count = 0;
   for (int z = 0; z < inputLine.length(); z++) {
      if (Character.isDigit(inputLine.charAt(z))) count++;
   }
   return count;
}

and
2. use these in the print statements

System.out.println("" + digits(inputLine) + " digits.");

There are probably other problems I didn't spot yet, but start by fixing those.

Added while loop, pretty sure I corrected the mistakes you pointed out and now it all done the 2nd way:easier way which actually did make more sense. Now I am getting "cannot be referenced from a static context" errors. (See below for exact errors) I don't see how they are static when the inputLine is changable with each loop.

import java.util.*;

public class p6final
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {
		String inputLine;
		int length = 0, digit = 0, whitespace = 0,letter = 0, vowel = 0;


		System.out.println("Enter a string for character classification: (EOF to end):");


		while (kb.hasNext())
		{
		inputLine = kb.nextLine();
		System.out.println("InputLine = " + inputLine + ".");

		length = inputLine.length();
		System.out.println("InputLine is " + length + " characters long and contains the following:");

		whitespaces(inputLine);
		System.out.println ("" + whitespace + " whitespace characters.");

		digits(inputLine);
		System.out.println("" + digit + " digits.");

		letters(inputLine);
		System.out.println("" + letter + " letters.");

		vowels(inputLine);
		System.out.println("" + vowel + " vowels.");

		System.out.println("Enter a string for character classification: (EOF to end):");
		}
 }

	public int vowels(String inputLine)
		{
		int vowel = 0;
		for (int i = 0; i < inputLine.length(); i++)
			{
			 char a = inputLine.charAt(i);
			 if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U') vowel++;
			}
		return vowel;
		}

	public int letters(String inputLine)
		{
		int letter = 0;
		for (int x = 0; x < inputLine.length(); x++)
			{
			if (Character.isLetter(inputLine.charAt(x))) letter++;
			}
			return letter;
		}

	public int whitespaces(String inputLine)
		{
		int whitespace = 0;
		for (int y = 0; y < inputLine.length(); y++)
			{
			if (Character.isWhitespace(inputLine.charAt(y))) whitespace++;
			}
		return whitespace;
		}

	public int digits(String inputLine)
		{
		int digit = 0;
		for (int z = 0; z < inputLine.length(); z++)
			{
			if (Character.isDigit(inputLine.charAt(z))) digit++;
			}
			return digit;
		}
}

C:\Documents and Settings\Chalandria\Desktop\p6final.java:25: non-static method whitespaces(java.lang.String) cannot be referenced from a static context
whitespaces(inputLine);
^
C:\Documents and Settings\Chalandria\Desktop\p6final.java:28: non-static method digits(java.lang.String) cannot be referenced from a static context
digits(inputLine);
^
C:\Documents and Settings\Chalandria\Desktop\p6final.java:31: non-static method letters(java.lang.String) cannot be referenced from a static context
letters(inputLine);
^
C:\Documents and Settings\Chalandria\Desktop\p6final.java:34: non-static method vowels(java.lang.String) cannot be referenced from a static context
vowels(inputLine);
^
4 errors

main is a static method - executes as part of the class itself.
The other methods are not static, ie they are instance methods and need an instance of the class to execute. To avoid this getting any more complex than it absolutely needs to be, just declare all the methods as static.

Now that you are returning the values properly from the methods, you no longer need these variables at line 11:
digit = 0, whitespace = 0,letter = 0, vowel = 0.
You missed the second part of my code - just replace

digits(inputLine);
System.out.println("" + digit + " digits.");

with

System.out.println(digits(inputLine) + " digits.");

(etc)

Thank you so much for all your help.

This is the final working program for p6 if anyone else in the class is looking it up and needs help as I did.
hint hint, dunkin donuts girl :)

import java.util.*;

public class p6
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {
		String inputLine;
		int length, digit, whitespace,letter, vowel;


		System.out.println("Enter a string for character classification: (EOF to end):");


		while (kb.hasNext())
		{
		inputLine = kb.nextLine();
		System.out.println("InputLine = " + inputLine + ".");

		length = inputLine.length();
		System.out.println("InputLine is " + length + " characters long and contains the following:");

		whitespaces(inputLine);
		System.out.println ("" + (whitespaces(inputLine)) + " whitespace characters.");

		digits(inputLine);
		System.out.println("" + (digits(inputLine)) + " digits.");

		letters(inputLine);
		System.out.println("" + (letters(inputLine)) + " letters.");

		vowels(inputLine);
		System.out.println("" + (vowels(inputLine)) + " vowels.");

		System.out.println("Enter a string for character classification: (EOF to end):");
		}
 }

	public static int vowels(String inputLine)
		{
		int vowel = 0;
		for (int i = 0; i < inputLine.length(); i++)
			{
			 char a = inputLine.charAt(i);
			 if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U') vowel++;
			}
		return vowel;
		}

	public static int letters(String inputLine)
		{
		int letter = 0;
		for (int x = 0; x < inputLine.length(); x++)
			{
			if (Character.isLetter(inputLine.charAt(x))) letter++;
			}
			return letter;
		}

	public static int whitespaces(String inputLine)
		{
		int whitespace = 0;
		for (int y = 0; y < inputLine.length(); y++)
			{
			if (Character.isWhitespace(inputLine.charAt(y))) whitespace++;
			}
		return whitespace;
		}

	public static int digits(String inputLine)
		{
		int digit = 0;
		for (int z = 0; z < inputLine.length(); z++)
			{
			if (Character.isDigit(inputLine.charAt(z))) digit++;
			}
			return digit;
		}
}

opps, I forgot to remove lines 25, 28, 31, and 34 although the program still runs properly with them in there

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.