Question

using Java program

please copy and paste the code don't screenshot it

import java.util.Scanner;
import java.io.File;

public class  {
   public static void main(String[] args) {
      // Create a new Scanner object to obtain 
      // input from System.in
      // --> TODO
      
      // Ask user for a word to search for. Print 
      // out a prompt
      // --> TODO
      
      // Use the Scanner object you created above to 
      // take a word of input from the user. 
      // --> TODO
      
      // *** Don't modify this line
      // Create a new file object to represent the 
      // file we are reading from. 
      // *** Important: Make sure that the file 
      // movieReviews.txt is in the same directory as
      // your Java source.  
      File file = new File("movieReviews.txt");
      
      // Read from the file, and print out each line
      try {
         // Just like we've created a Scanner object
         // to read from System.in, we can create one
         // to read from any File object. 
         Scanner fInput = new Scanner(file);
       
         // Variables to keep track of word scores
         int wordScoreTotal = 0;
         int count = 0;
         double wordScore = 0.0;
         
         // Keep looping through the file, and 
         // process each line
         while (fInput.hasNextLine()) {
            // Get the next line
            String line = fInput.nextLine();
            
            // Print out the next line. 
            System.out.println(line);
            // For your assignment, replace the 
            // line of code above with your code to count 
            // the number of occurrences of the word
            // that the user gave in the file.
            // You will use methods such as getNumericValue
            // in the Character class and the indexOf method 
            // in String.  
            // --> TODO 
         }
         
         // Print out the number of times the input word
         // appears in the file movieReviews.txt
         System.out.println("The word " + word +
                     " appears " + count + " times in the file");
                     
         // Calculate the average word score 
         // --> TODO
         
         // Print out the average word score
         System.out.println("The average score is " + wordScore);
      
      // DO NOT modify the next line   
      } catch(Exception e) { 
         e.printStackTrace();
      }
   
   }
}


Objective: This closed lab will provide you with experience with designing a class that is being used by another. The Problem

Given these scores, if a word appears in a line, then the score for the word is the score that the line itself has. For examp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

X Movie.java movieReviews.txt import java.util.Scanner; import java.io.File; public class Movie{ public static void main(Striif ( line.toLowerCase().indexOf(word.toLowerCase() >=0) { count++; // incrementing count char ch = line.charAt(0); // taking

Output:

Sum_1YQUITT! Chegg$ javac Movie.java s0m3_7hing@diff:-/Chegg$ java Movie Please enter the word you are looking for: spectacul

movieReviews.txt file :

Movie.java X movieReviews.txt X 4 If a horror movies primary goal is to frighten and disturd, then they works spectacularly

Raw Code:

import java.util.Scanner;
import java.io.File;

public class Movie{
public static void main(String[] args) {
// Create a new Scanner object to obtain
// input from System.in
// --> TODO
Scanner input = new Scanner(System.in);
// Ask user for a word to search for. Print
// out a prompt
// --> TODO
System.out.println("Please enter the word you are looking for: ");
// Use the Scanner object you created above to
// take a word of input from the user.
// --> TODO
String word = input.nextLine();
// *** Don't modify this line
// Create a new file object to represent the
// file we are reading from.
// *** Important: Make sure that the file
// movieReviews.txt is in the same directory as
// your Java source.
File file = new File("movieReviews.txt");
  
// Read from the file, and print out each line
try {
// Just like we've created a Scanner object
// to read from System.in, we can create one
// to read from any File object.
Scanner fInput = new Scanner(file);

// Variables to keep track of word scores
int wordScoreTotal = 0;
int count = 0;
double wordScore = 0.0;

// Keep looping through the file, and
// process each line
while (fInput.hasNextLine()) {
// Get the next line
String line = fInput.nextLine();
// first convert the line and word to lowercase (case-insensitive ex: silly and Silly both are same)
// using indexOf() method to find the given word in the current line or not . indexOf() method returns the index of the word in a line.
// it returns negative value when the given word is not present in the current line.
if ( line.toLowerCase().indexOf(word.toLowerCase()) >=0 ) {
count ++; // incrementing count
char ch = line.charAt(0); // taking first character(score) from line.
wordScoreTotal += Character.getNumericValue(ch); // converting characer to integer and adding it to wordScoreTotal
}
// For your assignment, replace the
// line of code above with your code to count
// the number of occurrences of the word
// that the user gave in the file.
// You will use methods such as getNumericValue
// in the Character class and the indexOf method
// in String.
// --> TODO
}

// Print out the number of times the input word
// appears in the file movieReviews.txt
System.out.println("The word " + word +
" appears " + count + " times in the file");

// Calculate the average word score
// --> TODO
wordScore = ((float)wordScoreTotal/count);


// Print out the average word score
System.out.println("The average score is " + wordScore);
  
// DO NOT modify the next line   
} catch(Exception e) {
e.printStackTrace();
}

}
}

If you have any doubts feel free to comment in comment section .

DO VOTE(LIKE).

Add a comment
Know the answer?
Add Answer to:
using Java program please copy and paste the code don't screenshot it import java.util.Scanner; import java.io.File;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This...

    Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException;...

    Use the csv file on spotify from any date Code from lab2 import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class SongsReport {    public static void main(String[] args) {               //loading name of file        File file = new File("songs.csv"); //reading data from this file        //scanner to read java file        Scanner reader;        //line to get current line from the file        String line="";       ...

  • please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){...

    please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...

  • read the code and comments, and fix the program by INSERTING the missing code in Java...

    read the code and comments, and fix the program by INSERTING the missing code in Java THank you please import java.util.Scanner; public class ExtractNames { // Extract (and print to standard output) the first and last names in "Last, First" (read from standard input). public static void main(String[] args) { // Set up a Scanner object for reading input from the user (keyboard). Scanner scan = new Scanner (System.in); // Read a full name from the user as "Last, First"....

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT