In: Computer Science
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
=======================
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!