Last Java Assignment!!

Discussion in 'Mac Programming' started by MBP123, Dec 3, 2007.

  1. MBP123 macrumors regular

    Joined:
    May 26, 2006
    #1
    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.
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    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
     
  3. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #3
    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?
     
  4. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #4
    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.
     
  5. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #5
    So right now I have this

    Code:
    
    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);
    	}  
    }
     
  6. prostuff1 macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #6
    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
     
  7. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #7
    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?

    Code:
    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);
    	}  
    }
     
  8. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
  9. prostuff1 macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #9
    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.

    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.

    Code:
    private static void lettersInArray(Arraylist<Character> a)

    This should be dead simple so figure it out yourself

    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.

    again you can use the lettersInArray method but just restrict what letters you are looking for for each case

    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??
     
  10. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #10
    Code:
    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?
     
  11. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #11
    I am also having issues with my loop, as I cannot get it to stop executing when "!" is inputted.
     
  12. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #12
    This is not syntax checked, but you can do all this within the loop.

    Code:
    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
     
  13. weinrdog macrumors newbie

    Joined:
    Oct 25, 2007
    Location:
    o-HI-o
    #13
    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.
     
  14. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #14
    Could I use a do/while loop? Or is only a while loop sufficient?
     
  15. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #15
    Code:
    
    char letter; 
            do {
             String input = JOptionPane.showInputDialog("Please enter a letter (Type ! when finished) "); 
             letter= input.charAt(0);
            }while (letter != '!');
    
    
     
  16. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #16
    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
     
  17. MBP123 thread starter macrumors regular

    Joined:
    May 26, 2006
    #17
    So I got it to work with the do/while loop. Will that work for the goals of the program?
     
  18. prostuff1 macrumors 65816

    prostuff1

    Joined:
    Jul 29, 2005
    Location:
    Don't step into the kawoosh...
    #18
    the while loop above will work fine but i believe you can use a do while if you like.
     
  19. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #19
    Yes, the do/while loop construct will work great.

    Todd
     

Share This Page