LinkedList problem

just 1 program needed,

Page 1 of 1

6 Replies - 1172 Views - Last Post: 23 September 2008 - 03:21 AM Rate Topic: -----

#1 suxinjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-August 08

LinkedList problem

Posted 23 September 2008 - 01:33 AM

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







//my coding
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;
	   
	   
	   System.out.println("Enter 5 words");
	   
	   //Q.2 Input a sentence(several words)into the list
	   String answer = new String("yes");
		 while( answer.equals("yes")) 
		 {
			 str = JOptionPane.showInputDialog("Enter a word : ");
			 wordList.addFirst(word);
			 answer = JOptionPane.showInputDialog("More words : <yes/no> ");
		 }
		 
		 //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++;
			}
		  
		 System.out.println("Total words" + count);
		 
		 //Q.4 Count to the number of words that begins with vowel
			   //and consonants in the list and display
		for(int k=0;k<wordList.size();k++)
		{ 
			JOptionPane.showMessageDialog(null,"Enter the 5 vowel into the list");
			wordList.add();
		}
		
		 if(wordList.contains()==a)
			countA++;
		 if(wordList.contains()==e)
			countE++;
		 if(wordList.contain()==i)
			countI++;
		 if(wordList.contain()==o)
			countO++;
		 if(wordList.contain()==u)
			countU++;
		 else
			System.out.println("Words begin with consonants");
		}
	}

		
		
   




p/s i need u guys to help me to solve this quest.those who r willing to cooperate.

Is This A Good Question/Topic? 0
  • +

Replies To: LinkedList problem

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,153
  • Joined: 20-September 08

Re: LinkedList problem

Posted 23 September 2008 - 02:09 AM

You're heading in the right direction. The following should help:

final String VOWELS = "aeiou';

...


if(VOWELS.indexOf(wordList.get(k).toString().toLowerCase().charAt(0))  > -1) {
	// Starts with vowel
}
else {
	// Starts with consonant
}


This post has been edited by g00se: 23 September 2008 - 02:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 Unknown Hero  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 51
  • Joined: 04-September 07

Re: LinkedList problem

Posted 23 September 2008 - 02:34 AM

while( answer.equals("yes"))
         {
             str = JOptionPane.showInputDialog("Enter a word : ");
             wordList.addFirst(word);
             answer = JOptionPane.showInputDialog("More words : <yes/no> ");
         }


Notice the difference:

while( answer.equals("yes"))
         {
             str = JOptionPane.showInputDialog("Enter a word : ");
             wordList.addFirst(str);
             answer = JOptionPane.showInputDialog("More words : <yes/no> ");
         }



You can also do it like this:
             str = JOptionPane.showInputDialog("Enter a sentence: ");
             wordList = Arrays.asList(s.split(" "));



You don't need to increment count all the time in for loop. Because at the end count will be equal to wordList.size().
So you can just write that outside of for loop:
count = wordList.size();



for(int k=0;k<wordList.size();k++)
        {
            JOptionPane.showMessageDialog(null,"Enter the 5 vowel into the list");
            wordList.add();
        }
        
         if(wordList.contains()==a)
            countA++;
         if(wordList.contains()==e)
            countE++;
         if(wordList.contain()==i)
            countI++;
         if(wordList.contain()==o)
            countO++;
         if(wordList.contain()==u)
            countU++;
         else
            System.out.println("Words begin with consonants");

This doesn't work, that's obvious.

You have to make another for loop and for each string in list check with what vowel it begins. Try this:
if (s.startsWith("a") || s.startsWith("A"))
countA++;
else if...

Was This Post Helpful? 0
  • +
  • -

#4 suxinjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-August 08

Re: LinkedList problem

Posted 23 September 2008 - 02:35 AM

View Postg00se, on 23 Sep, 2008 - 02:09 AM, said:

You're heading in the right direction. The following should help:

final String VOWELS = "aeiou';

...


if(VOWELS.indexOf(wordList.get(k).toString().toLowerCase().charAt(0))  > -1) {
	// Starts with vowel
}
else {
	// Starts with consonant
}





i still cant see the resolution.
where i gonna put those code?which shud i replace?

thnks ;]
Was This Post Helpful? 0
  • +
  • -

#5 suxinjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-August 08

Re: LinkedList problem

Posted 23 September 2008 - 02:51 AM

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;
	   
	   
	   System.out.println("Enter 5 words");
	   
	   //Q.2 Input a sentence(several words)into the list
	   String answer = new String("yes");
		 while( answer.equals("yes"))  
		 {  
			 str = JOptionPane.showInputDialog("Enter a word : ");  
			 wordList.addFirst(str);  
			 answer = JOptionPane.showInputDialog("More words : <yes/no> ");  
		 }  
		 
		 //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();  
		  
		 System.out.println("Total words" + count);
		 
		 //Q.4 Count to the number of words that begins with vowel
			   //and consonants in the list and display
		for(int k=0;k<wordList.size();k++)
		{ 
			JOptionPane.showMessageDialog(null,"Enter the 5 vowel into the list");
		 count = wordList.size();  
		}
		
		 if (s.startsWith("a") || s.startsWith("A"))  
		  countA++;
		 if(s.startsWith("e") || s.startsWith("E"))  
			countE++;
		 if(s.startsWith("i") || s.startsWith("I"))  
			countI++;
		 if(s.startsWith("o") || s.startsWith("O"))  
			countO++;
		 if(s.startsWith("u") || s.startsWith("U"))  
			countU++;
		 else
			System.out.println("Words begin with consonants");
		}
	}

		
		


im using Mr unknown hero's coding,but sir i still cant get/compile.sorry
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,153
  • Joined: 20-September 08

Re: LinkedList problem

Posted 23 September 2008 - 03:15 AM

You need to put it in your loop, e.g.

		final String VOWELS = "aeiou";

		for(int k=0;k<wordList.size();k++)
		{

			String s = wordList.get(k).toString();
			if(VOWELS.indexOf(s.toLowerCase().charAt(0))  > -1) {
				// Starts with vowel
				System.out.printf("%s starts with a vowel\n", s);
			}
			else {
				// Starts with consonant
				System.out.printf("%s starts with a consonant\n", s);
			}
		}


Was This Post Helpful? 0
  • +
  • -

#7 suxinjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-August 08

Re: LinkedList problem

Posted 23 September 2008 - 03:21 AM

View Postg00se, on 23 Sep, 2008 - 03:15 AM, said:

You need to put it in your loop, e.g.

		final String VOWELS = "aeiou";

		for(int k=0;k<wordList.size();k++)
		{

			String s = wordList.get(k).toString();
			if(VOWELS.indexOf(s.toLowerCase().charAt(0))  > -1) {
				// Starts with vowel
				System.out.printf("%s starts with a vowel\n", s);
			}
			else {
				// Starts with consonant
				System.out.printf("%s starts with a consonant\n", s);
			}
		}



mr/mrs goose,ive just learn only basic in linkedlist n got to do presentation infront of my lecturer,
so ur way is to profesional.maybe got another way? ;]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1