Help with Code

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 6
Reputation: hedwards09 is an unknown quantity at this point 
Solved Threads: 0
hedwards09 hedwards09 is offline Offline
Newbie Poster

Help with Code

 
0
  #1
29 Days Ago
Hello,

I am an online student in an intro to Java programming course and am a little stuck on my current program. The program is supposed to prompt for the name of files until the user enters "stop" to quit and includes the following methods in addition to the main method:

1. countLines(), which takes a File object, and returns the number of lines of text in the file

2. countLettersInLines(), which takes a File object, and returns an array of integers. In this array of integers, the first entry is the number of letters in the first line of the file, the second entry is the number of letters in the second line of the file, etc.

3. countLetters(), which takes a File object, and returns the number of letters in the file

4. countOneLetter() takes a File object and a letter, and returns the number of occurrences of the specified letter in the file (in uppercase or lowercase). This method and countLetters() can be used to determine the frequency of each letter in a text file.

5. countAllLetters() takes a File object, and returns an array of 26 integers, such that the entries in the array contain the number of occurrences of each letter of the alphabet in the file, disregarding case. The first entry in the array will be the number of a's in the file (regardless of case), the second entry is the number of b's (regardless of case), and so on. This method should use countOneLetter().

6. countIntegers() takes a File object, and returns the number of integers in the file

An example to this program would be:
Please enter the file name, or "stop" to end: short.txt

Number of lines: 2
Line with maximum number of letters: 1
Line 1: 13 letters
Line 2: 5 letters

Number of letters: 18
Frequency of a: 5.555555555555555
Frequency of b: 0.0
Frequency of c: 0.0
Frequency of d: 0.0
Frequency of e: 16.666666666666668
Frequency of f: 0.0
Frequency of g: 0.0
Frequency of h: 5.555555555555555
Frequency of i: 11.11111111111111
Frequency of j: 0.0
Frequency of k: 0.0
Frequency of l: 16.666666666666668
Frequency of m: 11.11111111111111
Frequency of n: 5.555555555555555
Frequency of o: 5.555555555555555
Frequency of q: 0.0
Frequency of r: 0.0
Frequency of s: 11.11111111111111
Frequency of t: 0.0
Frequency of u: 0.0
Frequency of v: 5.555555555555555
Frequency of w: 0.0
Frequency of x: 0.0
Frequency of y: 5.555555555555555
Frequency of z: 0.0

Integers in the file: 2

Please enter the file name, or "stop" to end: stop


Thank you any help will be much appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 795
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
0
  #2
28 Days Ago
Sorry hedwards09, we can only give homework help to those that make an attempt first. Can you post what you have so far with the specific problems you are having? If you haven't started coding yet, what ideas do you have so far? We may be able to guide you if you show that you have made a start.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 6
Reputation: hedwards09 is an unknown quantity at this point 
Solved Threads: 0
hedwards09 hedwards09 is offline Offline
Newbie Poster
 
0
  #3
28 Days Ago
That's fine. Well let's start with the main method...I know it must contain a scanner object and a System.out.print that prompts the user for file names until it is asked to quit by typing "stop". Then there must be a sentinel loop created am I correct? And inside the sentinel loop it must contain the methods listed in the first post(countLines(), countLetters(), etc.)? So this is what I have so far:

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

String fName;

System.out.print("Enter file name or stop to quit");
fName = input.nextLine();

while (fName != "stop")
{
//Methods entered here
}
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 795
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster
 
0
  #4
28 Days Ago
Yes, that looks good to me. So let's look at what you need to do for your countOneLetter method. The method signature will look like this:
  1. public int countOneLetter(File inputFile, char searchLetter)
You can get this from the description given to you:
countOneLetter() takes a File object and a letter, and returns the number of occurrences of the specified letter in the file (in uppercase or lowercase).
If you look at what I've done with the method signature, I have said that we will return an int (you can't have fractions of occurrences) and we require the file and the letter that we are searching for.

Ok, so how to structure the method? First we need to set up a variable to store the number of times the letter occurs in the file. Let's call it count and we start at 0 because we haven't found any occurrences yet. So add the following line to your method, right at the start:
  1. int count = 0;
Next we need to check whether the char that was passed in is a letter, and whether it is upper or lower case. The Character.isLetter(), Character.isUpperCase() and Character.isLowerCase() methods will help you here. If it's not a letter, you can either throw an exception or return your count of 0. If it is a letter, and it's lower case, we need to work out what it's upper case letter is before we search the file. Similarly, if we have an upper case letter we need to work out its lower case equivalent. I'll leave this up to you to figure out. Finally, we search each charater in the File object and see if it is either the lower case or upper case version of the char. If it is increment the count, if not do nothing. Right at the end of the method, return the count.

Now that I've explained that method, the others are very similar. Your main method will need to call each method in turn and print out the results, using System.out.println.

I hope this has helped you see how to design it, have a try at coding the method I have described and the others, feel free to re-post if you have any more trouble. Good luck and happy programming
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: brownjohn is an unknown quantity at this point 
Solved Threads: 1
brownjohn brownjohn is offline Offline
Newbie Poster
 
1
  #5
26 Days Ago
I'm relatively new to Java, but I have about 3 years of C++ experience.

Rather than

  1. fName != "stop"

it should be

  1. !fname.equalsIgnoreCase("stop")

equalsIgnoreCase is used just in case the user capitalizes one, or all of the letters.
Last edited by brownjohn; 26 Days Ago at 7:15 pm.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC