For instance something, something? something; something! and something should co
ID: 3647353 • Letter: F
Question
For instance something, something? something; something! and something should count as the same word.Hi, I need help fixing this code so that is doesn't display duplicates.
import java.util.*;
public class WordCount1
{
public static void main(String[]args)
{
final int Lines=6;
Scanner in=new Scanner (System.in);
String paragraph = "";
System.out.println( "Please input "+ Lines + " lines of text.");
for (int i=0; i < Lines; i+=1)
{
paragraph=paragraph+" "+in.nextLine();
}
System.out.println(paragraph);
String word="";
int WordCount=0;
for (int i=0; i<paragraph.length()-1; i+=1)
{
if (paragraph.charAt(i) != ' ' || paragraph.charAt(i) !=',' || paragraph.charAt(i) !=';' || paragraph.charAt(i) !=':' )
{
word= word + paragraph.charAt(i);
if(paragraph.charAt(i+1)==' ' || paragraph.charAt(i) ==','|| paragraph.charAt(i) ==';' || paragraph.charAt(i) ==':')
{
WordCount +=1;
word="";
} //end if
} //end if
} //end for
System.out.println("There are "+WordCount +" words ");
} //end main
} //end class
Explanation / Answer
// this works pretty well import java.util.Scanner; public class WordCount1 { public static void main(String[]args) { final int Lines=6; Scanner in=new Scanner (System.in); String paragraph = ""; System.out.println( "Please input "+ Lines + " lines of text."); for (int i=0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.