Welcome to Dream.In.Code
Become a Java Expert!

Join 150,196 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,026 people online right now. Registration is fast and FREE... Join Now!




hello worldddd!this is my last wish!

 
Reply to this topicStart new topic

hello worldddd!this is my last wish!

suxinjava
23 Sep, 2008 - 01:11 PM
Post #1

New D.I.C Head
*

Joined: 10 Aug, 2008
Posts: 17


My Contributions

1 program needed

Write an application to :

1.create a LinkedList called wordList
2.Input a sentence(several words) into the list.
3.count and display the total words in the list
4.count the number of words that begins with vowel and consonants in the list and display them
according to the following format:

Words begin with vowels Count
A or a ..............x
E or e ..............x
I or i ...............x
O or o .............x
U or u ..............x
Words begin with consonants x


CODE


import java.util.*;
import javax.swing.*;

public class Lab5
{
    public static void main(String args[])
    {
        //Q.1 Create a LinkedList called wordList
        LinkedList wordList= new LinkedList();
        
        
      
       String str,word;   //declare the variable
       int count=0;
       int vowels=5;
       int consonants=26;

      
        

      
      
      
      
      
      
      
      
       //Q.2 Input a sentence(several words)into the list
              
              
              
              str = JOptionPane.showInputDialog("Enter a word : "); {  
              wordList.addFirst(str);  
    }
        
        
        //Q.3 Count and display the total words in the list
         //int count=0;
        
        for(int k=0;k<wordList.size();k++)
         {
             System.out.println(wordList.get(k));
                count++;
            
         count = wordList.size();
                
            }
          
            
          

        
         //Q.4 Count to the number of words that begins with vowel
               //and consonants in the list and display
      
      

          
            
    

           for (int i = 0; i < wordList.size(); i++) {  
      String next = (String) wordList.get(i);  
      switch (next.charAt(0)) {  
        case 'a': case 'A':
        vowels++;
         break;
        case 'e': case 'E':
        vowels++;
         break;
        case 'i': case 'I':
        vowels++;
         break;
        case 'o': case 'O':
        vowels++;
        break;
        case 'u': case 'U':  
          vowels++;  
         break;  
      
      
       default:  
         consonants++;  
        
        
        
        
        
         System.out.println("words begin with vowels count");
        
     }  
   }  
        
        
        
  }
}
        
        
  





theres no syntax error,but the output damn sucks.come on help me on this last minute!

User is offlineProfile CardPM
+Quote Post

Gloin
RE: Hello Worldddd!this Is My Last Wish!
23 Sep, 2008 - 01:27 PM
Post #2

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
To output the results you could type:

System.out.println("words that begin with vowels = " + vowels);
System.out.println("words that begin with consonants = " + consonants);

they should however be put outside the switch statement and for-loop, not inside as they are now.

Then as far as I can understand from the problem specification, vowels should be counted individually, meaning you should keep track of 5 variables

vowelsAa
vowelsEe
vowelsIi
vowelsOo
vowelsUu

meaning you should also print them separately (still outside the for-loop)

And in case you still get strange results, in the beginning of your program you initialize vowels = 5 and consonants = 26. I do believe you want to initialize both to 0.

This post has been edited by Gloin: 23 Sep, 2008 - 01:30 PM
User is offlineProfile CardPM
+Quote Post

suxinjava
RE: Hello Worldddd!this Is My Last Wish!
23 Sep, 2008 - 01:34 PM
Post #3

New D.I.C Head
*

Joined: 10 Aug, 2008
Posts: 17


My Contributions
QUOTE(Gloin @ 23 Sep, 2008 - 02:27 PM) *

To output the results you could type:

System.out.println("words that begin with vowels = " + vowels);
System.out.println("words that begin with consonants = " + consonants);

they should however be put outside the switch statement and for-loop, not inside as they are now.

Then as far as I can understand from the problem specification, vowels should be counted individually, meaning you should keep track of 5 variables

vowelsAa
vowelsEe
vowelsIi
vowelsOo
vowelsUu

meaning you should also print them separately (still outside the for-loop)

And in case you still get strange results, in the beginning of your program you initialize vowels = 5 and consonants = 26. I do believe you want to initialize both to 0.



the loop?
i still cant get any
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Hello Worldddd!this Is My Last Wish!
23 Sep, 2008 - 02:06 PM
Post #4

Expert Schmexpert...
Group Icon

Joined: 4 Aug, 2008
Posts: 934



Thanked: 54 times
My Contributions
CODE


int vowelsAa = 0;
int vowelsEe = 0;
int vowelsIi = 0;
int vowelsOo = 0;
int vowelsUu = 0;

for (int i = 0; i < wordList.size(); i++) {  
      String next = (String) wordList.get(i);  
      switch (next.charAt(0)) {  
        case 'a': case 'A':
        vowelsAa++;
         break;
        case 'e': case 'E':
        vowelsEe++;
         break;
        case 'i': case 'I':
        vowelsIi++;
         break;
        case 'o': case 'O':
        vowelsOo++;
        break;
        case 'u': case 'U':  
          vowelsUu++;  
         break;  
      
       default:  
         consonants++;  
     }  
   }  

System.out.println("words begin with vowel A or a = " + vowelsAa);
System.out.println("words begin with vowel E or e = " + vowelsEe);
System.out.println("words begin with vowel I or i = " + vowelsIi);
System.out.println("words begin with vowel O or o = " + vowelsOo);
System.out.println("words begin with vowel U or u = " + vowelsUu);
System.out.println("words begin with consonants = " + consonants);


User is offlineProfile CardPM
+Quote Post

pbl
RE: Hello Worldddd!this Is My Last Wish!
23 Sep, 2008 - 02:19 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
I think the problem is there

CODE

           str = JOptionPane.showInputDialog("Enter a word : "); {  
              wordList.addFirst(str);  


you are adding the whole sentence to the word list making only one entry into it

You will have to split this sentence:

CODE

Scanner scanLine = new Scanner(str);
while(scanLine.hasNext())
    wordList.add(scanLine.next();



User is offlineProfile CardPM
+Quote Post

suxinjava
RE: Hello Worldddd!this Is My Last Wish!
24 Sep, 2008 - 03:58 AM
Post #6

New D.I.C Head
*

Joined: 10 Aug, 2008
Posts: 17


My Contributions
thanks everyone who helped me!
thanks a lot.
ive done the assignment .
wink2.gif
wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:38AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month