Question

*JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before...

*JAVA

Prompt the user for a movie name. Output the three movies that come alphabetically before the one entered.

I need help correcting my code. I'm unsure why I am getting a name, year, and genre in my output.

Input: Jericho

Output:

Similar title finder. Enter a movie name.\n

Here are the 3 movies that are listed before the one you entered\n

Jeremy Paxman Interviews...\n

Jeremy Taylor\n

Jeremy Vine Meets...\n

Current output:

Similar title finder. Enter a movie name.\n

Here are the 3 movies that are listed before the one you entered\n

Jeremy Vine Meets...\n

2004\n

Documentary\n

(Your output is too short.)

Current code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Movies {

    static ArrayList<String> moviesList;

    public static void main(String[] args) throws FileNotFoundException {
//providing file name
        File file = new File("db.txt");

        readNames(file);

        System.out.println("Similar title finder. Enter a movie name.");

        Scanner scanner = new Scanner(System.in);

        String movieName = scanner.nextLine();

        searchMovie(movieName);
    }

    public static void readNames(File file) throws FileNotFoundException {

        moviesList = new ArrayList<String>();

        Scanner scanner = new Scanner(new FileReader(file));

        while (scanner.hasNextLine()) {

            String name = scanner.nextLine();

            moviesList.add(name);

        }
    }

    public static void selectionSort(ArrayList<String> haystack) {
        //visit all elements but the last
        for (int startScan = 0; startScan < haystack.size() - 1; startScan++) {
            //assume first element is smallest
            int minIndex = startScan;
            String minValue = haystack.get(minIndex);

            //visit all remaining elements
            //if current element is smaller than what we know so far
            for (int index = startScan + 1; index < haystack.size(); index++) {

                if (haystack.get(index).compareTo(minValue) < 0) {
                    //keep track of smaller element
                    minValue = haystack.get(index);
                    minIndex = index;
                }
                //swap smaller element into place
                haystack.set(minIndex, haystack.get(startScan));
                haystack.set(startScan, minValue);
            }
        }
    }


    public static void searchMovie(String name) {

        if (!moviesList.contains(name)) {

            moviesList.add(name);

            Movies.selectionSort(moviesList);


            searchMovie(name);


            int index = moviesList.indexOf(name);

            moviesList.remove(index);
        }


        if (moviesList.contains(name)) {

            String data = "";

            int index = moviesList.indexOf(name);

            for (int i = 3; i >= 1; i--) {

                if (index - i >= 0) {

                    data += moviesList.get(index - i) + "\n";

                }

            }

            System.out.println("Here are the 3 movies that are listed before the one you entered");

            System.out.println(data);

        }

    }

}

please include explanations and avoid try/catch or collections. Thank you

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

SOLUTION: WHY THE OUTPUT CONTAINS EXTRA DATA? Your code has no error, the INOUT FILE you are using is containing extraneous dCODE FOR SPLITTING: After the line number 23, add the following code. public static void readNames (File file) throws FileNot

Can't provide a screenshot as you have not attached the database file.

Add a comment
Know the answer?
Add Answer to:
*JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before...
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
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

  • 7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the...

    7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the quicksort() and partition() methods to sort the IDs in ascending order using the Quicksort algorithm, and output the sorted IDs one per line. Ex. If the input is: kaylasimms julia myron1994 kaylajones -1 the output is: julia kaylajones kaylasimms myron1994 Code: import java.util.Scanner; import java.util.ArrayList; public class UserIDSorting { // TODO: Write the partitioning algorithm - pick the middle element as the // pivot,...

  • Help! Not sure how to create this java program to run efficiently. Current code and assignment...

    Help! Not sure how to create this java program to run efficiently. Current code and assignment attached. Assignment :Write a class Movies.java that reads in a movie list file (movies.txt). The file must contain the following fields: name, genre, and time. Provide the user with the options to sort on each field. Allow the user to continue to sort the list until they enter the exit option. ---------------------------------------------------------- import java.util.*; import java.io.*; public class Movies { String movieTitle; String movieGenre;...

  • JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File;...

    JAVA Can you make this return the first letter of  first and last name capitalized? import java.io.File; import java.io.IOException; import java.util.*; public class M1 {    public static void main(String[] args) {        //scanner input from user    Scanner scanner = new Scanner(System.in); // System.out.print("Please enter your full name: "); String fullname = scanner.nextLine(); //creating a for loop to call first and last name separately int i,k=0; String first="",last=""; for(i=0;i<fullname.length();i++) { char j=fullname.charAt(i); if(j==' ') { k=i; break; } first=first+j;...

  • I have this java program that I need to add an abstract method and polymorphism to...

    I have this java program that I need to add an abstract method and polymorphism to it : import java.util.Scanner; //class and encapsulation class BookTheTicket { private String movieName; private String theatreName; private int ticketCost; void myAllmovies() { System.out.println("-------Listing the movies:------"); System.out.println(" 1.DDLJ ------------ $40 \n 2.kkr---------$.50 \n 3.game-movie --------$60 \n 4.fun movie ----- $.70 "); } } // inheritence class theater extends BookTheTicket{ private int numOfTickets; void theater() { System.out.println("*******Listing the theatre:******* \n 1.coco cola tld \n 2.koi gandhi...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • Ask the user for the name of a file and a word. Using the FileStats class,...

    Ask the user for the name of a file and a word. Using the FileStats class, show how many lines the file has and how many lines contain the text. Standard Input                 Files in the same directory romeo-and-juliet.txt the romeo-and-juliet.txt Required Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 1137 line(s) contain "the"\n Your Program's Output Enter a filename\n romeo-and-juliet.txt has 5268 lines\n Enter some text\n 553 line(s) contain "the"\n (Your output is too short.) My...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

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