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?
Word manipulation
Collapse
X
-
How are you going to determine which word is valid and which one is not? What code have you written so far?Originally posted by RoadieI 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?Comment
-
Originally posted by Ganon11What 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.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.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") ; } }Comment
-
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 javaCode: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") ; } }Comment
-
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
-
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..Originally posted by r035198xYour 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; }
when I put it into m IDE I got
for this lineCode:" ';' expected "
so i put in the ';' and then I gotCode:for(int i = 0;i < s.length()i ++) {
forCode:char cannot be dereferenced
&Code:if(s.charAt(i).equals(" ")) {
forCode:incompatible types
&Code:String words = sentence.split(" ");
forCode:longestWord(java.lang.String[]) in x cannot be applied to (java.lang.String)
Code:System.out.println("Longest word is :"+longestWord(words));Comment
-
Yaccck. What a mess of code I just posted. Please kindly adjust toOriginally posted by RoadieThanks 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
for this lineCode:" ';' expected "
so i put in the ';' and then I gotCode:for(int i = 0;i < s.length()i ++) {
forCode:char cannot be dereferenced
&Code:if(s.charAt(i).equals(" ")) {
forCode:incompatible types
&Code:String words = sentence.split(" ");
forCode:longestWord(java.lang.String[]) in x cannot be applied to (java.lang.String)
Code:System.out.println("Longest word is :"+longestWord(words));
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
-
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..Originally posted by r035198xYaccck. 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
-
I hope whatever you submitted worked.Originally posted by RoadieThank 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
Comment