Question

In: Computer Science

Using the USPRES.TXT file complete the following: Create a dialog where a user would select 'search...

Using the USPRES.TXT file complete the following:

Create a dialog where a user would select 'search by last name' and 'search by first name'.

use a select statement to determine which routine your will run. You can use procedures and functions if you want.Ensure your code is well documented.

Turn in all work.

Using the USPRES.TXT file complete the following:

Create a dialog where a user would select 'search by last name' and 'search by first name'.

use a select statement to determine which routine your will run. You can use procedures and functions if you want.Ensure your code is well documented.

Turn in all work.

[George Washington

John Adams

Thomas Jefferson

James Madison

James Monroe

John Q. Adams

Andrew Jackson

Martin Van Buren

William Harrison

John Tyler

James Polk

Zachary Taylor

Millard Fillmore

Franklin Pierce

James Buchanan

Abraham Lincoln

Andrew Johnson

Ulysses Grant

Rutherford Hayes

James Garfield

Chester Arthur

Grover Cleveland

Benjamin Harrison

Grover Cleveland

William McKinley

Theodore Roosevelt

William Taft

Woodrow Wilson

Warren Harding

Calvin Coolidge

Herbert Hoover

Franklin Roosevelt

Harry Truman

Dwight Eisenhower

John Kennedy

Lyndon Johnson

Richard Nixon

Gerald Ford

James Carter

Ronald Reagan

George H. W. Bush

Bill Clinton

George W. Bush

Barack Obama]

Update

Case statement for each of the options.

Search for President by first name or last name.

** open file, read in each row, parse out the name part, perform a match on names, if match return full name, else move to next row. Have message if you each the end without a match.

USE METHODS!

Update

Case statement for each of the options.

Search for President by first name or last name.

** open file, read in each row, parse out the name part, perform a match on names, if match return full name, else move to next row. Have message if you each the end without a match.

USE METHODS!

I NEED TO WRITE JAVA PROJECT IN THIS QUESTION

Solutions

Expert Solution

=======================

JAVA PROGRAM

=======================

package test.president;

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

public class PresidentSearch {
   private Scanner sc = null;
   private static final String FILENAME = "USPRES.txt"; //constant filename
   //constructor
   public PresidentSearch(){
       sc = new Scanner(System.in);//to read user input
   }
  
   //main method
   public static void main(String[] args){
       PresidentSearch ps = new PresidentSearch(); //create instance
       System.out.println("Enter 1 to search by firstName and 2 to search by lastName");
       int entry = -1;
       try{
       entry = Integer.parseInt(ps.readInput()); //read user entry
       }catch(NumberFormatException n){
           System.out.println("Invalid entry!");
           return;
       }
       switch(entry){ //use switch case to appropriately call searchby First or last name
           case 1: ps.searchByFirstName();break;
           case 2: ps.searchByLastName();break;
           default: System.out.println("Invalid entry!");
       }
      
       ps.closeScanner();//close scanner
   }
  
   private String readInput(){
       return sc.nextLine();
   }
  
   //set scanner to read from file
   private void setScanner(String filename){
       File file = new File(filename);
       try {
           sc = new Scanner(file);
       } catch (FileNotFoundException e) {
           System.out.println("File is not found!");
           sc = null;
       }
   }
  
   /**
   * this will search presidents with first name
   */
   private void searchByFirstName(){
       System.out.println("Enter first name to search:");
       String firstName = readInput();
      
       setScanner(FILENAME);
      
       if(sc!=null){
           StringBuilder sb = new StringBuilder();
           while(sc.hasNextLine()){
               String fullname = sc.nextLine();
               String nameArr[] = fullname.split(" "); //split the name
              
               //do case insensitive check if first name matches
               if(nameArr[0].equalsIgnoreCase(firstName)){
                   sb.append(fullname);//add to string builder
                   sb.append("\n");
               }
           }
           //if no result found string builder will be empty
           if(sb.toString().isEmpty()){
               System.out.println("No Result Found!");
           }else{
               System.out.println("Presidents with first name as "+firstName+" :");
               System.out.println(sb.toString());
           }
       }
   }
   /**
   * serach all presidents with user input last name
   */
   private void searchByLastName(){
       System.out.println("Enter last name to search:");
       String lastName = readInput();
      
       setScanner(FILENAME);
       if(sc!=null){
           StringBuilder sb = new StringBuilder();
           while(sc.hasNextLine()){
               String fullname = sc.nextLine();
               String nameArr[] = fullname.split(" ");
               int lastIndex = nameArr.length -1;
              
               if(nameArr[lastIndex].equalsIgnoreCase(lastName)){
                   sb.append(fullname);
                   sb.append("\n");
               }
           }
          
           if(sb.toString().isEmpty()){
               System.out.println("No Result Found!");
           }else{
               System.out.println("Presidents with first name as "+lastName+" :");
               System.out.println(sb.toString());
           }
       }
   }
  
   private void closeScanner(){
       if(sc!=null){
           sc.close();
       }
   }

}

=======================

OUTPUT

=======================

RUN1

Enter 1 to search by firstName and 2 to search by lastName
1
Enter first name to search:
john
Presidents with first name as john :
John Adams
John Q. Adams
John Tyler
John Kennedy

Run2

Enter 1 to search by firstName and 2 to search by lastName
2
Enter last name to search:
bush
Presidents with first name as bush :
George H. W. Bush
George W. Bush

Run3

Enter 1 to search by firstName and 2 to search by lastName
1
Enter first name to search:
anand
No Result Found!


Related Solutions

using python3 Write a Python program that lets a user search through the 'data.txt' file for...
using python3 Write a Python program that lets a user search through the 'data.txt' file for a given first name, last name, or email address, and print all the matches/results. Prompt the user for which field to search, (f) for first name, (l) for last name, or (e) for email data.txt entries are all formatted as: first_name, last_name, email, separated by TABS Your program should not read the entire file into memory, it should read line by line, and only...
Please write a java program (using dialog box statements for user input) that has the following...
Please write a java program (using dialog box statements for user input) that has the following methods in it: (preferably in order)   a method to read in the name of a University and pass it back a method to read in the number of students enrolled and pass it back a method to calculate the tuition as 20000 times the number of students and pass it back a method print the name of the University, the number of students enrolled,...
Explain how to search for a record (using hashing) in a file where the records originally...
Explain how to search for a record (using hashing) in a file where the records originally were stored using modules method . for example the id was modulated by the size of the file.
Create a C++ login program using file for 2 user. Usernames must be in one file...
Create a C++ login program using file for 2 user. Usernames must be in one file and password in the other file. Ask user for their usernames and password if it matches, they logged in successfully. If it doesn’t match,they have 3 trials ,then they have to sign in and create a username and password. After creating a username and password, they have to go to the login page again. Must work for visual studio code
Description Your program must start and keep dialog with the user. Please create the first prompt...
Description Your program must start and keep dialog with the user. Please create the first prompt for the dialog. After that your program must accept the user’s response and echo it (output to screen) in upper case and adding the question mark at the end. The program must run endlessly until the user say “stop” in any combination of cases. Then you program must say “GOODBYE!” and quit. Example: HELLO, I AM THE PROGRAM Hi, I am Michael HI, I...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
create a program using IDLE where you will gather input from the user using a loop....
create a program using IDLE where you will gather input from the user using a loop. The user needs to provide you with a list of test scores for two categories (two different classrooms). Test scores must be integer values between 0 and 10. You need to process each score so that you can output the following: Number of test scores entered for classroom A. Number of test scores entered for classroom B. Average of test scores entered for classroom...
(write a program that get the numbers from user and search the file numbers.text for that...
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9 you must to have the exact output: Enter a number: 10 10 last appears in the file at position 9 Enter a number: 29 29 does not appear in the file Enter a number: 9 9 last appears in the file at position 12 Enter a number:
In this assignment, you will create a Java program to search recursively for a file in...
In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively in the given...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT