Question: Hello, I need help altering the following java program! Id like for it to ask for the user for a name, search a text file

Hello, I need help altering the following java program! Id like for it to ask for the user for a name, search a text file for that name, and return the names correlating phone numbers. At the moment I have to use display("Don Rose") for it to work, id like for it to instead ask for a name to search instead of hardcoding one in. If you have any questions please ask, thank you!

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

public class Hw1 {

public static void main(String[] args) throws Exception{ boolean loop = true; File file = new File("Hw1_Data.txt"); FileWriter fw = new FileWriter(file,true); fw.write("Hw 1 Data"); fw.write(" "+"last,first:cell,5021934246;home,4556784517"); // add some more contacts fw.write(" "+"Kewn,Mike:cell,7021934111;home,7756784212"); fw.write(" "+"Don,Rose:cell,1121934222;home,3336784556"); fw.write(" "+"Doe,John:cell,3341934876;home,2006784200"); //do {}(loop) fw.close(); display("Don Rose"); } // declare as static so you can just call it directly in main() public static void display(String name) { String[] names = name.split(" "); // separate last and first names try { FileReader inFile = new FileReader("Hw1_Data.txt"); BufferedReader inStream = new BufferedReader(inFile); // to read entire line String line;

while ((line = inStream.readLine()) != null) { // check for both last (names[0]) and first (names[1]) if(line.contains(names[0]) && line.contains(names[1])) { String cellhome = line.split(":")[1]; // right side of ":" /* to find and display cell and home separately String cell = cellhome.split(";")[0].split(",")[1]; String home = cellhome.split(";")[1].split(",")[1]; System.out.println("Cell: "+cell); System.out.println("Home: "+home); */ System.out.println(cellhome); } } inStream.close(); }catch (IOException e) { System.out.println("Problems reading file."); e.printStackTrace(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!