View Full Version : Last Java Assignment!!
MBP123
Dec 3, 2007, 03:49 PM
I had previously posted assignments here and you guys had helped me out tremendously, and I really think because of some people here, my grade has gone up substantially (I got a 61 on the first test, 77 on the 2nd, and 85 on the 3rd), and I have been getting the hang of it a lot more lately. But this last assignment of the semester has me a bit perplexed, as it deals with arrays, and we have only talked about them for one class, so I feel its sort of a rush job. So if someone could steer me in the right direction itd be appreciated. I am not looking for people to do my assignment in any way, as I do actually want to learn this stuff. But please be patient, as I am still new to Java, and sometimes have trouble with the programming jargon.
Here is the assignment and thanks in advance!
Write a program which prompts the user to input a series of characters, one at a time. The program will stop prompting the user for characters once the user enters an exclamation point ('!').
Using an array, count the number of occurrences of each letter (regardless of whether it is upper or lower case (so for example, an 'A' and an 'a' both count for the first letter of the alphabet). In a separate counter, also count the total number of "other" characters ('.', '?', ' ', '2', etc.). The final exclamation point does not count.
Print out the count for each letter found (but not for those which where not found!)
Print the count of the non-letter characters.
List the characters that were not found (e.g. "The following letters were not found: t, z, c.").
By inspecting the array, print out the total number of all of the vowels, and the total number of all of the consonants.
Finally, print out which letter was found the most times. (Note there may be more than one letter which has the maximum count attached to it.) Also, print out which letter (or letters) was found the least number of times, but make certain to exclude letters which were not found at all.
toddburch
Dec 3, 2007, 03:57 PM
Congrats!
Put a shell of a program together that does everything you know how to do, and post it back here with questions for where you are stuck.
Todd
MBP123
Dec 3, 2007, 07:56 PM
Would I use char for the user inputted characters, or would they have to be converted to int due since they have to be counted?
MBP123
Dec 3, 2007, 08:00 PM
Actually I recall something about the subscripts of the arrays are always integers. I guess that helps a little bit, but I am still somewhat lost.
MBP123
Dec 3, 2007, 09:26 PM
So right now I have this
import javax.swing.JOptionPane;
public class asgn6 {
public static void main( String[] args )
{
int character;
String letter = JOptionPane.showInputDialog("Please enter a letter (Type ! when finished) ");
character = Integer.parseInt (letter);
if(letter.equals("!"))
System.exit(0);
}
}
prostuff1
Dec 3, 2007, 09:36 PM
I assume since you posted something with an "interface" you need to use it.
If might actually be easier to work with it via a command line if that is an option, at least to start.
I would look into ArrayList, and do some googling on how to tell if the char input is a character in the alphabet or some form of punctuation.
If i get a chance (doing my own final stuff right now) i might be kinda enough to put some snippets up once you show you have the general idea.
On a side note, I would also like to thank all the people that help out in this part of the forum. I know it has helped me immensely and i usually get a nudge in the right direction. Thanks
MBP123
Dec 3, 2007, 10:51 PM
Well this was an in class assignment that I had to do, which took the alphabet full of chars and spelled out "cat", or any other word you desired. If I instead used the user input as the source, instead of alphabet, would that be on the right track? It seems that the user input should be chars however. Is that correct?
public class array3 {
public static void main( String[] args )
{
char ThirdArray[];
ThirdArray = new char[27];
int i;
char letter = 'a';
for (i=1; i<=26; i++) {
ThirdArray[i] = letter ;
letter++;
}
for (i=1; i<=26; i++)
System.out.print(ThirdArray[i] );
System.out.print("\n");
// What does this line print?
System.out.print(" "+ThirdArray[3]+
ThirdArray[1]+ThirdArray[20]+"\n");
System.exit(0);
}
}
MBP123
Dec 4, 2007, 10:32 PM
So can anyone help me out a bit?
prostuff1
Dec 5, 2007, 12:42 AM
do you have any code beside what you have posted already??
You basically need to create a while loop that keeps going until the user inputs an "!".
As each character is input you need to add it to an array(look up arraylist on the sun site) so that later you can go through it and figure out what needs to be printed.
I would create 2 arrays (one to store letter and one to store punctuation and digits). Look up Character on the sun site (specifically the Character.isDigit(char) and Character.isLetter(char) and Character.isWhiteSpace(char) calls as they will come in very handy.
So you take the input check to see if it is a Digit, whitespace, or letter. if letter convert it to lowercase (makes it easier later) and store it in the array for letters. if it is not a letter then it is a digit, punctuation, or whitespace so add it to the other array.
Print out the count for each letter found (but not for those which where not found!)
you can probably adapt the code you posted below to do some of this and i would just make a method that you pass the arraylist into i.e.
private static void lettersInArray(Arraylist<Character> a)
Print the count of the non-letter characters.
This should be dead simple so figure it out yourself
List the characters that were not found (e.g. "The following letters were not found: t, z, c.").
you should be able to figure out how to use the lettersInArray method to accomplish this goal. If you need more help with this post back here. If you wanted to amke it easier on yourself you could probably store these (hint: in another arraylist) while you are going through the first array. That way you only have to print them out.
By inspecting the array, print out the total number of all of the vowels, and the total number of all of the consonants.
again you can use the lettersInArray method but just restrict what letters you are looking for for each case
Finally, print out which letter was found the most times. (Note there may be more than one letter which has the maximum count attached to it.) Also, print out which letter (or letters) was found the least number of times, but make certain to exclude letters which were not found at all.
again, you should be able to use a modification of the lettersInArray method to do this.
If you need help feel free to ask, but i think i have given you a step in the right direction (short of giving you the code) and you should be able to figure it out from here.
Also, if you don't mind me asking... when is this due??
MBP123
Dec 5, 2007, 02:13 PM
import javax.swing.JOptionPane;
public class asgn6 {
public static void main( String[] args )
{
char letter;
String input = JOptionPane.showInputDialog("Please enter a letter (Type ! when finished) ");
letter= input.charAt(0);
while (letter != '!'){
JOptionPane.showInputDialog("Please enter a letter (Type ! when finished) ");
letter++;
}
//System.out.println("\nThe characters that were not found were: " + );//
//System.out.println("\nThe total of all the vowels is: " + );//
//System.out.println("\nThe total of all the consonants is: " + );//
//System.out.println("\nThe letter that was found the most times was: " );//
System.exit(0);
}
}
I changed it to char instead of parsing it as an int. So I will be using the ASCII table. However, whenever I type in "!" the program still runs. What part of my syntax is incorrect with that?
MBP123
Dec 5, 2007, 03:04 PM
I am also having issues with my loop, as I cannot get it to stop executing when "!" is inputted.
toddburch
Dec 5, 2007, 03:18 PM
This is not syntax checked, but you can do all this within the loop.
letter = ' ' ;
char letter;
while (letter != '!') {
String input = JOptionPane.showInputDialog("Please enter a letter (Type ! when finished) ");
letter= input.charAt(0);
}
You'll need to add the other logic.
Todd
weinrdog
Dec 5, 2007, 03:29 PM
Print out letter within your loop.
Looks like your current code will initialize letter to the first character of the first input & then increment letter thru the ascii sequence from there ignoring your input in the additional dialogs you show. Also, since '!' is ascii 33, you will never increment letter to it from another of the printable characters and your loop will run forever.
MBP123
Dec 5, 2007, 03:33 PM
Could I use a do/while loop? Or is only a while loop sufficient?
MBP123
Dec 5, 2007, 03:34 PM
char letter;
do {
String input = JOptionPane.showInputDialog("Please enter a letter (Type ! when finished) ");
letter= input.charAt(0);
}while (letter != '!');
toddburch
Dec 5, 2007, 03:34 PM
Also, since '!' is ascii 33, you will never increment letter to it from another of the printable characters and your loop will run forever.
Sure it will. You'll have to hit enter a whole bunch, but adding 1 to a single byte over and over and over, it will wrap. Certainly not desirable behavior, but certainly not an infinite loop.
Todd
MBP123
Dec 5, 2007, 03:38 PM
So I got it to work with the do/while loop. Will that work for the goals of the program?
prostuff1
Dec 5, 2007, 03:42 PM
Could I use a do/while loop? Or is only a while loop sufficient?
the while loop above will work fine but i believe you can use a do while if you like.
toddburch
Dec 5, 2007, 03:42 PM
Yes, the do/while loop construct will work great.
Todd
vBulletin® v3.8.6, Copyright ©2000-2013, Jelsoft Enterprises Ltd.