Question: In my java project a user inputs the name of a boat that is saved in the text file, the program checks if the name
In my java project a user inputs the name of a boat that is saved in the text file, the program checks if the name is in the file and if it is it prints it into the console. I am having trouble getting the program to read the text file.
My code:
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner;
/** * * @author Kristen Albrecht */ public class mian {
/** * @param args the command line arguments */ public static void main(String[] args) throws FileNotFoundException { String filename = "Boats.txt"; //PrintWriter out; try { FileReader fr = new FileReader(filename); } catch(FileNotFoundException e) { System.out.println("unable to open file " + filename); return; } System.out.println("Enter boat name (name) or quit: "); Scanner keyboard = new Scanner(System.in); while(keyboard.hasNextLine()) { String input = keyboard.nextLine(); if (input.equalsIgnoreCase("quit")) { break; } String[] tokens = input.split(","); if (tokens.length !=2) { System.out.println("Invalid format, try again"); continue; } String name = tokens[0].trim(); fr.printf("%-10s ", name, args); } keyboard.close(); fr.close(); } }
_____________________________________________
Boats.txt
_____________________________________________
Sea Monkey Backdraft Cast Away Nautifish Destiny
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
