Word manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roadie
    New Member
    • Feb 2007
    • 6

    Word manipulation

    I have to write a java program to take in sentence and then output the number of valid words, the longest word, how many times it occurred, the shortest word and how many times that occurred.. What I want to do is create methods for processing each of those and then have the main to ask for the sentence and output the results. Can anyone help?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    What do you have so far? You've given us your ideas...now, how do you think you'll accomplish the first function? The second?

    Show us what you've tried, and we'll help guide you to the answer that way.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Roadie
      I have to write a java program to take in sentence and then output the number of valid words, the longest word, how many times it occurred, the shortest word and how many times that occurred.. What I want to do is create methods for processing each of those and then have the main to ask for the sentence and output the results. Can anyone help?
      How are you going to determine which word is valid and which one is not? What code have you written so far?

      Comment

      • Roadie
        New Member
        • Feb 2007
        • 6

        #4
        Originally posted by Ganon11
        What do you have so far? You've given us your ideas...now, how do you think you'll accomplish the first function? The second?

        Show us what you've tried, and we'll help guide you to the answer that way.
        Code:
        class CountValidWords
        {
        	public static boolean isValid(String theString)	
        	
        	{
        		boolean validWord ;
        		int index ;
        		
        		validWord = true ;
        		index = 0 ;
        		while ( (index < theString.length()) && (validWord == true) )
        			{
        				if(((theString.charAt(index) >= 'A') && (theString.charAt(index) <= 'Z')) || ((theString.charAt(index) >= 'a') && (theString.charAt(index) <= 'z')))
        					{
        						index ++ ;
        					}
        				else
        					{
        						validWord = false ;
        					}		
        			}
        		
        		return validWord ;	
        	
        	}
        	
            public static void main(String[] args)
            
            {
            	String theWord ;
            	String  sentence ;
            	int fin ;
            	int st ;
            	int numOfSpaces = 0 ;
            	int numOfWords = 0 ;
            	int numOfValidWords = 0 ;
            	
            	System.out.print ("\n\n\t Please enter a sentence: \t") ;
            	System.out.print ("\n\n\t ") ;
            	sentence = EasyIn.getString() + " " ;
            	st = 0 ;
            	fin = sentence.indexOf(' ') ;
            	
            	
            	
            	while(fin != -1)
            	{
            		theWord = sentence.substring(st,fin) ;
            		
            		if(isValid(theWord) == true)
            		{
            			numOfValidWords ++ ;
            		}
            		
            		st= fin + 1 ;
            		fin = sentence.indexOf(' ',st) ;
            	}	
            	
            	System.out.print( "\n\n\t there are " + numOfValidWords + " Valid words in this sentence") ;
            	System.out.print( "\n\n\t") ;
            }
            
        }
        This is how I count valid words. I don't even know where to start with the rest - I've hit a complete blank. I know i have to use a loops to get the program to check the length of the word and then store it if its longer than the previous longestword or ++ the count if its the same.

        Comment

        • Roadie
          New Member
          • Feb 2007
          • 6

          #5
          Code:
          class LongestWord
          {
          	public void String (String longestWord)
          	{
          		String sentence = "this is a test" ;
          		String theWord ;
          		longestWord = "" ;
          		int index = 0 ;
          		
          		while (index < sentence.length())
          			if (theWord.length() >> longestWord)
          			{
          				longestWord = theWord ;
          			}
          	}
          	
          	public static int occLongestWord(int occLongestWord)
          	{
          		String theWord ;
          		String longestWord ;
          		occLongestWord = 0 ;
          			
          		if (theWord == longestWord)
          		{
          			occLongestWord ++ ;
          		}
          		return occLongestWord ;
          	}
          	
              public static void main(String[] args)
          	{
          		String sentence = "This is a test" ;
          		
          	    System.out.print ("\n\n\tThe longest word occurs " + occLongestWord + " times.") ;
          	    System.out.print ("\n\n\t") ;
          	}
              
          }
          This is what I've got so far on the longest word part. I know there's alot wrong with it but its where I'm at at the moment. I appreciate any help you can give me. I'm writing the program for a project but I'm now more interested in understanding everything that getting it done. I'm gonna have to go back to the basics and start again. I find it hard though to find any material thats not extremely confusing. Its a shame because I really like java

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Your program design is a little difficult to follow. Spilt the string into a strin array first and call some methods that do what you want. Here is how you should have started


            Code:
             
            public static void main(String[] args) {
              String sentence = EasyIn.getString();
              String words = sentence.split(" "); //splits a given string using space into a strin array
              System.out.println("Number of spaces is :"+numOfSpaces(sentence));
              System.out.println("Longest word is :"+longestWord(words));
             }
             //tells us the longest word in an array of words
             public static String longestWord(String[] words) {
              String s = "";
              for(String a : words) {
               if(a.length() > s.length()) {
            	s = a;
               }
              }
              return s;
             }
            
             //Tells us the number of spaces in a string
             public static int numOfSpaces(String s) {
              int num = 0;
              for(int i = 0;i < s.length()i ++) {
               if(s.charAt(i).equals(" ")) {
            	num++;
               }
              }
              return num;
             }

            Comment

            • Roadie
              New Member
              • Feb 2007
              • 6

              #7
              Originally posted by r035198x
              Your program design is a little difficult to follow. Spilt the string into a strin array first and call some methods that do what you want. Here is how you should have started


              Code:
               
              public static void main(String[] args) {
                String sentence = EasyIn.getString();
                String words = sentence.split(" "); //splits a given string using space into a strin array
                System.out.println("Number of spaces is :"+numOfSpaces(sentence));
                System.out.println("Longest word is :"+longestWord(words));
               }
               //tells us the longest word in an array of words
               public static String longestWord(String[] words) {
                String s = "";
                for(String a : words) {
                 if(a.length() > s.length()) {
              	s = a;
                 }
                }
                return s;
               }
              
               //Tells us the number of spaces in a string
               public static int numOfSpaces(String s) {
                int num = 0;
                for(int i = 0;i < s.length()i ++) {
                 if(s.charAt(i).equals(" ")) {
              	num++;
                 }
                }
                return num;
               }
              Thanks for replying! Really appreciate it. I actually haven't done anything with arrays yet so I've no understanding of them at all. That looks so simple..

              when I put it into m IDE I got
              Code:
              " ';' expected "
              for this line
              Code:
               for(int i = 0;i < s.length()i ++) {
              so i put in the ';' and then I got
              Code:
              char cannot be dereferenced
              for
              Code:
              if(s.charAt(i).equals(" ")) {
              &
              Code:
              incompatible types
              for
              Code:
              String words = sentence.split(" ");
              &
              Code:
              longestWord(java.lang.String[]) in x cannot be applied to (java.lang.String)
              for
              Code:
              System.out.println("Longest word is :"+longestWord(words));

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Roadie
                Thanks for replying! Really appreciate it. I actually haven't done anything with arrays yet so I've no understanding of them at all. That looks so simple..

                when I put it into m IDE I got
                Code:
                " ';' expected "
                for this line
                Code:
                 for(int i = 0;i < s.length()i ++) {
                so i put in the ';' and then I got
                Code:
                char cannot be dereferenced
                for
                Code:
                if(s.charAt(i).equals(" ")) {
                &
                Code:
                incompatible types
                for
                Code:
                String words = sentence.split(" ");
                &
                Code:
                longestWord(java.lang.String[]) in x cannot be applied to (java.lang.String)
                for
                Code:
                System.out.println("Longest word is :"+longestWord(words));
                Yaccck. What a mess of code I just posted. Please kindly adjust to


                Code:
                 
                import java.util.Scanner;
                public class Test1 {
                 public static void main(String[] args) {
                  Scanner input = new Scanner(System.in);
                  System.out.print("Enter the sentence :");
                  String sentence = input.nextLine();
                  String[] words = sentence.split(" "); //splits a given string using space into a strin array
                  System.out.println("Number of spaces is :"+numOfSpaces(sentence));
                  System.out.println("Longest word is :"+longestWord(words));
                 }
                 //tells us the longest word in an array of words
                 public static String longestWord(String[] words) {
                  String s = "";
                  for(String a : words) {
                   if(a.length() > s.length()) {
                	s = a;
                   }
                  }
                  return s;
                 }
                 //Tells us the number of spaces in a string
                 public static int numOfSpaces(String s) {
                  int num = 0;
                  for(int i = 0;i < s.length();i++) {
                   if(s.charAt(i) == ' ') {
                	num++;
                   }
                  }
                  return num;
                 }
                }

                Comment

                • Roadie
                  New Member
                  • Feb 2007
                  • 6

                  #9
                  Originally posted by r035198x
                  Yaccck. What a mess of code I just posted. Please kindly adjust to


                  Code:
                   
                  import java.util.Scanner;
                  public class Test1 {
                   public static void main(String[] args) {
                    Scanner input = new Scanner(System.in);
                    System.out.print("Enter the sentence :");
                    String sentence = input.nextLine();
                    String[] words = sentence.split(" "); //splits a given string using space into a strin array
                    System.out.println("Number of spaces is :"+numOfSpaces(sentence));
                    System.out.println("Longest word is :"+longestWord(words));
                   }
                   //tells us the longest word in an array of words
                   public static String longestWord(String[] words) {
                    String s = "";
                    for(String a : words) {
                     if(a.length() > s.length()) {
                  	s = a;
                     }
                    }
                    return s;
                   }
                   //Tells us the number of spaces in a string
                   public static int numOfSpaces(String s) {
                    int num = 0;
                    for(int i = 0;i < s.length();i++) {
                     if(s.charAt(i) == ' ') {
                  	num++;
                     }
                    }
                    return num;
                   }
                  }
                  Thank you very much sir! I handed in my project earlier this morning with what I had done already but more importantly I understand how to do it now. Hopefully I'll be able to help someone else further down the line..

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Roadie
                    Thank you very much sir! I handed in my project earlier this morning with what I had done already but more importantly I understand how to do it now. Hopefully I'll be able to help someone else further down the line..
                    I hope whatever you submitted worked.

                    Comment

                    • Roadie
                      New Member
                      • Feb 2007
                      • 6

                      #11
                      Originally posted by r035198x
                      I hope whatever you submitted worked.
                      Yeah it did but it wasn't everything i was supposed to have done. Better than handing in nothing.. Again I really appreciate the help!

                      Comment

                      Working...