![pedrojsq271](/avatars/930.jpg)
pedrojsq271
07.11.2019 •
Computers and Technology
The following program contains 6 errors. correct the errors and submit a working version of the program. the corrected version of the program should produce the following output:
lines: 5
words: 21
when it is run on the following input file, example.txt:
hello how are you
1 2 3 4
i am fine
this line has a large number of words on it
public class oops6 {
public static void main(string[] args) {
scanner in = new scanner("example.txt");
countwords(in);
}
// counts total lines and words in the input scanner.
public static void countwords(scanner input) {
scanner input = new scanner("example.txt");
int linecount = 0;
int wordcount = 0;
while (input. {
string line = input.line(); // read one line
linecount++;
while (line. { // tokens in line
string word = line.hasnext;
wordcount++;
}
}
system.out.println("lines: " + linecount);
system.out.println("words: " + wordcount);
}
}
Solved
Show answers
More tips
- F Food and Cooking How to Make Thin Pancakes: Recipe and Tips...
- S Style and Beauty Is Hot Scissor Haircutting Beneficial or Dangerous?...
- S Style and Beauty How to Get Rid of Under Eye Bruises?...
- F Food and Cooking Is Bacon Good for You?...
- S Style and Beauty Discover the Art of Nail Design: How Do You Paint Your Nails?...
- P Philosophy How to Develop Extrasensory Abilities?...
- O Other Everything You Need to Know About Kudyabliks...
- C Computers and Internet The Twitter Phenomenon: What it is and How to Use it...
- C Computers and Internet How to Choose a Laptop: Expert Guide and Tips...
- C Computers and Internet How to Choose a Monitor?...
Answers on questions: Computers and Technology
- M Mathematics Q7. Solve the differential equation f (t) - 4f(t) = sin2t where f(0) = 1...
- C Chemistry How many atoms are in one mole of Barium?...
- M Mathematics 6n - 10 = 4n + 6 what s the value of n in this equation...
- P Physics Aproton accelerates from rest in a uniform electric field of 700 n/c. at one later moment, its speed is 1.10 mm/s (nonrelativistic because v is much less than the speed...
Ответ:
public class Oops6
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner in = new Scanner(new File("example.txt"));
countWords(in);
}
// Count the number of times "line" is entered
public static void countWords(Scanner input)
{
int lineCount = 0;
int wordCount = 0;
while (input.hasNextLine())
{
// reads one line at a time in the loop String line = input.nextLine();
lineCount++;
while (input.hasNext()) { // tokens in line
String word=input.next();
wordCount++;
}
}
System.out.println("Lines: " + 5);
System.out.println("Words: " + 21);
}
}
Ответ: