Question

In: Computer Science

Write a program that displays all the cars in the given file (no sorting). Each car...

Write a program that displays all the cars in the given file (no sorting). Each car property is separated by a tab character: \t

Given file:

cars.txt

make     model year
Ford   Expedition 2003
Mazda  B-Series   1989
Ford   Freestar   2003
Hyundai    Elantra    2001
Hyundai    Entourage  2008
Chevrolet  Camaro 2011
Chevrolet  Monte Carlo    2006
Chevrolet  Blazer 1996
Chevrolet  Aveo   2005
Chevrolet  Corvette   1999
Mercedes-Benz  E-Class    2006
Dodge  Avenger    1995
Pontiac    Grand Prix 1973
Mitsubishi Outlander  2011
MINI   Clubman    2011
Suzuki Aerio  2007
Dodge  Dakota Club    1992
Chevrolet  Astro  2002
Chevrolet  Tahoe  1996
Mitsubishi Mirage 1994
Porsche    944    1991
Hyundai    Elantra    1994
Mercury    Grand Marquis  1998
Volkswagen Golf   2001
Jaguar XJ Series  2005
Toyota Echo   2005
GMC    Safari 2002
GMC    Sierra 1500    2000
Chevrolet  Cobalt 2005
Jeep   Patriot    2008
Mazda  Navajo 1991
Chevrolet  Malibu 2001
Saab   900    1990
Mercury    Grand Marquis  1998
Hummer H1 2004
Subaru Loyale 1993
Jeep   Wrangler   1999
Ford   Mustang    1994
Austin Mini Cooper S  1963
Mercedes-Benz  M-Class    1998
Jeep   Wrangler   2006
Honda  Civic  1997
Plymouth   Voyager    1994
Ford   Club Wagon 1997
Audi   5000S  1984
Saturn VUE    2003
Oldsmobile Achieva    1994
Mercedes-Benz  G55 AMG    2006
Chevrolet  Express 3500   1997
Lexus  ES 1992
Cadillac   Allante    1992
Hyundai    Tiburon    1997
Pontiac    Grand Prix 1965
Ford   Focus  2000
Mitsubishi Chariot    1987
Chrysler   Prowler    2001
Land Rover Discovery  2012
Volkswagen Scirocco   1984
Ford   Bronco 1984
Hyundai    Accent 1996
Volkswagen Routan 2012
Volkswagen Golf   2003
GMC    Terrain    2010
Ford   F150   2009
GMC    Sierra 2011
Dodge  Ram Van 1500   2000
Chrysler   300    2009
Oldsmobile Achieva    1997
Land Rover Discovery  2008
Toyota 4Runner    2002
Porsche    911    1995
Toyota Land Cruiser   2002
Land Rover Defender   1994
Chevrolet  Lumina 1997
Audi   TT 2002
Chrysler   Town & Country 2009
Nissan Frontier   2000
Toyota Tercel 1997
Buick  Riviera    1997

Output needed:

Required Output

           Ford               Expedition 2003\n
          Mazda                 B-Series 1989\n
           Ford                 Freestar 2003\n
        Hyundai                  Elantra 2001\n
        Hyundai                Entourage 2008\n
      Chevrolet                   Camaro 2011\n
      Chevrolet              Monte Carlo 2006\n
      Chevrolet                   Blazer 1996\n
      Chevrolet                     Aveo 2005\n
      Chevrolet                 Corvette 1999\n
  Mercedes-Benz                  E-Class 2006\n
          Dodge                  Avenger 1995\n
        Pontiac               Grand Prix 1973\n
     Mitsubishi                Outlander 2011\n
           MINI                  Clubman 2011\n
         Suzuki                    Aerio 2007\n
          Dodge              Dakota Club 1992\n
      Chevrolet                    Astro 2002\n
      Chevrolet                    Tahoe 1996\n
     Mitsubishi                   Mirage 1994\n
        Porsche                      944 1991\n
        Hyundai                  Elantra 1994\n
        Mercury            Grand Marquis 1998\n
     Volkswagen                     Golf 2001\n
         Jaguar                XJ Series 2005\n
         Toyota                     Echo 2005\n
            GMC                   Safari 2002\n
            GMC              Sierra 1500 2000\n
      Chevrolet                   Cobalt 2005\n
           Jeep                  Patriot 2008\n
          Mazda                   Navajo 1991\n
      Chevrolet                   Malibu 2001\n
           Saab                      900 1990\n
        Mercury            Grand Marquis 1998\n
         Hummer                       H1 2004\n
         Subaru                   Loyale 1993\n
           Jeep                 Wrangler 1999\n
           Ford                  Mustang 1994\n
         Austin            Mini Cooper S 1963\n
  Mercedes-Benz                  M-Class 1998\n
           Jeep                 Wrangler 2006\n
          Honda                    Civic 1997\n
       Plymouth                  Voyager 1994\n
           Ford               Club Wagon 1997\n
           Audi                    5000S 1984\n
         Saturn                      VUE 2003\n
     Oldsmobile                  Achieva 1994\n
  Mercedes-Benz                  G55 AMG 2006\n
      Chevrolet             Express 3500 1997\n
          Lexus                       ES 1992\n
       Cadillac                  Allante 1992\n
        Hyundai                  Tiburon 1997\n
        Pontiac               Grand Prix 1965\n
           Ford                    Focus 2000\n
     Mitsubishi                  Chariot 1987\n
       Chrysler                  Prowler 2001\n
     Land Rover                Discovery 2012\n
     Volkswagen                 Scirocco 1984\n
           Ford                   Bronco 1984\n
        Hyundai                   Accent 1996\n
     Volkswagen                   Routan 2012\n
     Volkswagen                     Golf 2003\n
            GMC                  Terrain 2010\n
           Ford                     F150 2009\n
            GMC                   Sierra 2011\n
          Dodge             Ram Van 1500 2000\n
       Chrysler                      300 2009\n
     Oldsmobile                  Achieva 1997\n
     Land Rover                Discovery 2008\n
         Toyota                  4Runner 2002\n
        Porsche                      911 1995\n
         Toyota             Land Cruiser 2002\n
     Land Rover                 Defender 1994\n
      Chevrolet                   Lumina 1997\n
           Audi                       TT 2002\n
       Chrysler           Town & Country 2009\n
         Nissan                 Frontier 2000\n
         Toyota                   Tercel 1997\n
          Buick                  Riviera 1997\n

My program so far:

import java.io.*;
import java.util.Scanner;

public class PS1{

    public static void main(String[] args) {

        try{

            Scanner scnr = new Scanner(new File("cars.txt"));

            String headings = scnr.nextLine();

            String line;
            String make;
            StringBuilder model;
            String temp;

            int year=0;

            while(scnr.hasNext()){

                line = scnr.nextLine();

                Scanner scnr2 = new Scanner(line);

                make = scnr2.next();

                model = new StringBuilder(scnr2.next());

                while(scnr2.hasNext()){

                    temp = scnr2.next();

                    if(scnr2.hasNext())

                        model.append(" ").append(temp);

                    else

                        year = Integer.parseInt(temp);

                }

                System.out.printf("%15s%25s%5s\n", make, model.toString(), year);
            }

            scnr.close();

        }catch(FileNotFoundException e){

            System.out.println(e.getMessage());

        }
    }
}

My output:

Your Program's Output

           Ford               Expedition 2003\n
          Mazda                 B-Series 1989\n
           Ford                 Freestar 2003\n
        Hyundai                  Elantra 2001\n
        Hyundai                Entourage 2008\n
      Chevrolet                   Camaro 2011\n
      Chevrolet              Monte Carlo 2006\n
      Chevrolet                   Blazer 1996\n
      Chevrolet                     Aveo 2005\n
      Chevrolet                 Corvette 1999\n
  Mercedes-Benz                  E-Class 2006\n
          Dodge                  Avenger 1995\n
        Pontiac               Grand Prix 1973\n
     Mitsubishi                Outlander 2011\n
           MINI                  Clubman 2011\n
         Suzuki                    Aerio 2007\n
          Dodge              Dakota Club 1992\n
      Chevrolet                    Astro 2002\n
      Chevrolet                    Tahoe 1996\n
     Mitsubishi                   Mirage 1994\n
        Porsche                      944 1991\n
        Hyundai                  Elantra 1994\n
        Mercury            Grand Marquis 1998\n
     Volkswagen                     Golf 2001\n
         Jaguar                XJ Series 2005\n
         Toyota                     Echo 2005\n
            GMC                   Safari 2002\n
            GMC              Sierra 1500 2000\n
      Chevrolet                   Cobalt 2005\n
           Jeep                  Patriot 2008\n
          Mazda                   Navajo 1991\n
      Chevrolet                   Malibu 2001\n
           Saab                      900 1990\n
        Mercury            Grand Marquis 1998\n
         Hummer                       H1 2004\n
         Subaru                   Loyale 1993\n
           Jeep                 Wrangler 1999\n
           Ford                  Mustang 1994\n
         Austin            Mini Cooper S 1963\n
  Mercedes-Benz                  M-Class 1998\n
           Jeep                 Wrangler 2006\n
          Honda                    Civic 1997\n
       Plymouth                  Voyager 1994\n
           Ford               Club Wagon 1997\n
           Audi                    5000S 1984\n
         Saturn                      VUE 2003\n
     Oldsmobile                  Achieva 1994\n
  Mercedes-Benz                  G55 AMG 2006\n
      Chevrolet             Express 3500 1997\n
          Lexus                       ES 1992\n
       Cadillac                  Allante 1992\n
        Hyundai                  Tiburon 1997\n
        Pontiac               Grand Prix 1965\n
           Ford                    Focus 2000\n
     Mitsubishi                  Chariot 1987\n
       Chrysler                  Prowler 2001\n
           Land          Rover Discovery 2012\n
     Volkswagen                 Scirocco 1984\n
           Ford                   Bronco 1984\n
        Hyundai                   Accent 1996\n
     Volkswagen                   Routan 2012\n
     Volkswagen                     Golf 2003\n
            GMC                  Terrain 2010\n
           Ford                     F150 2009\n
            GMC                   Sierra 2011\n
          Dodge             Ram Van 1500 2000\n
       Chrysler                      300 2009\n
     Oldsmobile                  Achieva 1997\n
           Land          Rover Discovery 2008\n
         Toyota                  4Runner 2002\n
        Porsche                      911 1995\n
         Toyota             Land Cruiser 2002\n
           Land           Rover Defender 1994\n
      Chevrolet                   Lumina 1997\n
           Audi                       TT 2002\n
       Chrysler           Town & Country 2009\n
         Nissan                 Frontier 2000\n
         Toyota                   Tercel 1997\n
          Buick                  Riviera 1997\n

When mine outputs Land Rover is separated and Rover is put with the model, can someone help me fix this, thank you. Keep this in Java.

Solutions

Expert Solution

The solution to the above problem in Java is as follows:-

I have used Buffered reader to fetch tab seprated properties:-

Code-

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class PS1{
    public static void main(String[] args) {
        try{
            BufferedReader buf = new BufferedReader(new FileReader("cars.txt")); //using buffered reader to read files
            ArrayList<String> words = new ArrayList<>();
            String lineJustFetched = null;
            String[] wordsArray;

            int year;
            String make;
            String model;

            while(true){
                lineJustFetched = buf.readLine();
                if(lineJustFetched == null){
                    break;
                }else{
                    wordsArray = lineJustFetched.split("\t"); //Splitting the fetched line
                    make=wordsArray[0]; //Fetching the make
                    model=wordsArray[1]; //Fetching the model
                    year=Integer.parseInt(wordsArray[2]); //Fetching the year
                    System.out.printf("%15s%25s%5s\n", make,model,year);
                }
            }

            buf.close();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

Code Screenshots-

Outputs-

Feel free to comment for any issues, do rate the answer positively


Related Solutions

● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Using python Write a program that displays all of states in the U.S. and display each...
Using python Write a program that displays all of states in the U.S. and display each state that begins with the letter A.
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
In this lab, you will read and write to a file, as well as sorting the...
In this lab, you will read and write to a file, as well as sorting the information. Copy and paste the following into a file named "inputs.txt": 7 199922007 C.J. Cregg Political_Science 200822013 Olivia Dunham Criminal_Justice 199822007 Josh Lyman Law 199922006 Toby Ziegler Communications 200922015 Leslie Knope Public_and_Environmental_Affairs 199922004 Sam Seaborn Law 200722013 Walter Bishop General_Sciences The input file provides details for a student database in the following format: Number_of_students ID_Number Student_First_Name Student_Last_Name Major …… ID_Number Student_First_Name Student_Last_Name Major Your...
Java Write a program that displays all the numbers from 100 to 200 that are divisible...
Java Write a program that displays all the numbers from 100 to 200 that are divisible by 5 or 6, but not both Make sure all instructions and outputs for the user are explicit
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT