Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2011, 09:07 PM   PM User | #1
blubbasta
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
blubbasta is an unknown quantity at this point
Couting letters in Java [CODE]

Hi all!
I have a question on a java assignment that I have been working on for one of my classes and can figure it out. In this program we have to prompt for and read in any line of text, that can contain any number or words or characters including spaces and punctuation.
It then prints out the number of times each letter of the alphabet occurs in that line. I have most of the program written but it is giving me a few compile errors; hence why I am here. The code is as follows:


public static int indexOfLetter(char theLetter)
{
int index = Character.toLowerCase (theLetter)-'a';
return index
}

import java.util.Scanner;
public class CountLetter
{
public static void man(String[] args)
{
Scanner scan = new Scanner(System.in);
int[] letterCount = new int[26];

System.out.println("Please type in a line of text." + " You may use punctuation & spaces,");
String line = scan.nextLine();
int count = 0;
int i = 0;
while (i < letterCount.length)
{
letterCount[i] = 0;
i++;
}
i=0;
while (i < line.length())
{
if (countLetters.isLetter(line.charAT(i)))
count++;
i++;


System.out.println("in the total number " + " of characters = "+count);
i = 0;
while (i < letterCount.length)

if (letterCount[i]!=0)
system.out.println("Number of " + (char)(i + 'a') + "\'s = " + letterCount[i]);
i++;
}

}

}



These are the two errors it is returning in jgrasp:

CountLetter.java:4: class, interface, or enum expected
public static int indexOfLetter(char theLetter)
^
CountLetter.java:7: class, interface, or enum expected
return index
^

Thanks in advance!!!
blubbasta is offline   Reply With Quote
Old 11-09-2011, 09:58 PM   PM User | #2
blubbasta
New to the CF scene

 
Join Date: Nov 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
blubbasta is an unknown quantity at this point
Edit: Thread moved to correct forum.
blubbasta is offline   Reply With Quote
Old 11-09-2011, 10:37 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This simply cannot be here:
Code:
public static int indexOfLetter(char theLetter)
{
int index = Character.toLowerCase (theLetter)-'a';
return index
}
That must be moved into your class.

Beyond this, you'll have a couple of other errors as well. One will be a compilation error, and the other will be a runtime error which is simply the placement of a closing brace. Since this is a homework assignment you should attempt to resolve these first, and come back if you have problems.
Another thing to note is that in Java, an int[] will always default to 0. So the loop to populate with 0 is actually optional. This differs from an Integer[] which defaults to null (like Object[]).

Also, in the future please wrap all code in [code][/code] or [php][/php] tags to preserve the formatting.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:38 AM.


Advertisement
Log in to turn off these ads.