Jeg er nået her til, men jeg er altså lidt blank på hvordan jeg opdaterer c. Kan du ikke hjælpe med det jeg skal kode præcist? Nedenstående tæller dog antallet af tegn/spaces i alt, men jeg skal jo kun have talt ord.
public void count() throws IOException {
int wordcount = 1; String wordlist = "en text med fem ord"; int index = 0; char c = wordlist.charAt(index); int wordlistLength = wordlist.length();
Her er en løsning som du måske kan kigge på og bruge.
import java.util.Scanner; /** * This class is used to test small task, developed in Java. * * @author Martin Rohwedder * @version 03-03-2011 */ public class Test {
private static int wordCount;
/** * Main Method. * @param args */ public static void main(String[] args) { Scanner input = new Scanner(System.in); //A scanner to read the users input. boolean stopped = false;
//Print a Welcome Message. printWelcome();
while (!stopped) { System.out.print(">> "); String sentence = input.nextLine().toLowerCase();
/** * Count our words in the text provided. * @param text A String containing the text. */ private static void countWords(String text) { //Split the String every time we have a whitespace. String[] words = text.split("\\s+");
//Loop trough our array, and count for every word in the array. while (wordCount < words.length) { wordCount++; } }
/** * Get the number of words counted. * @return An integer with the words counted. */ private static int getWordCount() { return wordCount; }
/** * Print a Welcome/start message. */ private static void printWelcome() { System.out.print("Welcome to the Word Counter.\n\n" + "If you want to end the program, you have to type 'END',\n" + "else just enter a sentence, so I can count the words for you!\n"); }
/** * Print a Goodbye/End message. */ private static void printGoodbye() { System.out.print("Thank You, for using the Word Counter.\n" + "Created by Martin Rohwedder (2011)"); }
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.