Question: In my Java program the user enters a boat name and agrument . The program checks if the boat name exists in a text file.

In my Java program the user enters a boat name and agrument . The program checks if the boat name exists in a text file. I am trying to write a conditional that prints different things in the console depending what the argument is ex: power off. The problem seems to be with.... if(args.equals... because it is not priniting the following line but I am not sure why.

_______________________________________

package kristenalbrechtproject8;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.PrintWriter;

import java.util.ArrayList; import java.util.Arrays;

import java.util.Scanner;

/**

*

* @author Kristen Albrecht

*/

public class main {

/**

* @param args

* the command line arguments

*/

public static void main(String[] args) throws FileNotFoundException {

String filename = "Boats.txt";

// PrintWriter out;

ArrayList list = new ArrayList();

FileReader f = new FileReader(filename);

Scanner keyboard = new Scanner(f);

while(keyboard.hasNextLine()){

list.add(keyboard.nextLine());

}

System.out.println("File Contents: " + list);

System.out.println("Enter boat name (name) or quit: ");

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(); System.out.printf("%-10s ", name, args);

if(list.contains(name)){

System.out.println(name + " found in file"); if(args.equals("power on")) { System.out.println(name + " power up the boat"); } else if(args.equals("power off")) { System.out.println(name + " turn off the boat"); } else { System.out.println(name + " command does not exist"); }

}else{

System.out.println(name + " not found in file");

}

System.out.println("Enter boat name (name) or quit: ");

keyboard = new Scanner(System.in);

}

keyboard.close();

}

}

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!