Jeg har en tekstfil og vil gerne have java til at fortælle mig hvor mange ord og hvor mange punktummer der forekommer i denne tekstfil. Nogen som kan fortælle hvilken funktion jeg skal bruge ?
Hej, Her har du en simpel udgave: Pladser følgende i Head delen af den html side: <script type="text/javascript"> function countit(){ var formcontent=document.wordcount.wordcount2.value formcontent=formcontent.split(" ") document.wordcount.wordcount3.value=formcontent.length } </script>
Pladcere følgende i Body delen <form name="wordcount"> <textarea rows="12" name="wordcount2" cols="38" wrap="virtual"></textarea><br> <input type="button" value="Calculate Words" onClick="countit()"> <input type="text" name="wordcount3" size="20"> </form>
Og lidt indviklet udgave: Pladser følgende i Head delen af den html side: <script type="text/javascript"> function CountWords(this_field, show_word_count, show_char_count){ if (show_word_count===undefined){show_word_count=true;} //show is default if (show_char_count===undefined){show_char_count=false;} //noshow is deft el=document.getElementById(this_field); char_count=el.value.length; // very crude measure fullStr=el.value+" "; // add space delimiter to end of text initial_whitespace_rExp= /^[^A-Za-z0-9]+/gi; //use for complex whitespace left_trimmedStr=fullStr.replace(initial_whitespace_rExp, " "); non_alphanumerics_rExp=/[^A-Za-z0-9]+/gi; // and for delimiters cleanedStr=left_trimmedStr.replace(non_alphanumerics_rExp, " "); splitString=cleanedStr.split(" ");word_count=splitString.length -1; if (fullStr.length <2){word_count=0;} if (word_count===1){wordOrWords=" word";}else{wordOrWords=" words";} if (char_count===1){charOrChars=" character";}else{charOrChars=" characters";} if (show_word_count && show_char_count){ msg="Word Count:\n"+" "+word_count+wordOrWords+"\n"; msg += " "+char_count+charOrChars;window.alert(msg); } else{ if (show_word_count){alert("Word Count: "+word_count+wordOrWords);} else{ if (show_char_count){ window.alert("Character Count: "+char_count+charOrChars);} } } return word_count; } </script> Pladcere følgende i Body delen <form name="wordcount"> <textarea id="w_in" cols="55" rows="8"></textarea> <button onclick="CountWords('w_in');return false;">Count The Words Now! </button> </form> Jeg håber hjælper. Funktionerne udregner antal ord i en sætning!
aaben filen med en BufferedReader omkring en FileReader en while loekke som laeser linierne ind med readLine tael antal punktummer i linien med en for loekke split linien i ord med split funktionen (som tager regex udtryk)
/** * Command line program to count lines, words and characters in files or from standard input, similar to the wc utility. * Run like that: java WordCount FILE1 FILE2 ... or * like that: java WordCount < FILENAME. */ public class WordCount { /** * Count lines, words and characters in given input stream * and print stream name and those numbers to standard output. * @param name name of input source * @param input stream to be processed * @throws IOException if there were I/O errors */ private static void count(String name, BufferedReader in) throws IOException { long numLines = 0; long numWords = 0; long numChars = 0; String line; do { line = in.readLine(); if (line != null) { numLines++; numChars += line.length(); numWords += countWords(line); } } while (line != null); System.out.println(name + "\t" + numLines + "\t" + numWords + "\t" + numChars); }
/** * Open file, count its words, lines and characters * and print them to standard output. * @param fileName name of file to be processed */ private static void count(String fileName) { BufferedReader in = null; try { FileReader fileReader = new FileReader(fileName); in = new BufferedReader(fileReader); count(fileName, in); } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } }
/** * Count words, lines and characters of given input stream * and print them to standard output. * @param streamName name of input stream (to print it to stdout) * @param input InputStream to read from */ private static void count(String streamName, InputStream input) { try { InputStreamReader inputStreamReader = new InputStreamReader(input); BufferedReader in = new BufferedReader(inputStreamReader); count(streamName, in); in.close(); } catch (IOException ioe) { ioe.printStackTrace(); } }
/** * Determine the number of words in the argument line. * @param line String to be examined, must be non-null * @return number of words, 0 or higher */ private static long countWords(String line) { long numWords = 0; int index = 0; boolean prevWhitespace = true; while (index < line.length()) { char c = line.charAt(index++); boolean currWhitespace = Character.isWhitespace(c); if (prevWhitespace && !currWhitespace) { numWords++; } prevWhitespace = currWhitespace; } return numWords; }
public static void main(String[] args) { if (args.length == 0) { count("stdin", System.in); } else { for (int i = 0; i < args.length; i++) { count(args[i]); } } } }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.