I need help with making the \"Enter a command\" line only happen once when readi
ID: 3629165 • Letter: I
Question
I need help with making the "Enter a command" line only happen once when reading multiple commands in from a file.
public class CS210Main {
/** The array of all the commands */
protected ICommand[] commands = { new BackupCommand(),
new DefineIndexCommand(), new DefineTableCommand(),
new DeleteCommand(), new DropCommand(), new ExitCommand(),
new InsertCommand(), new IntersectionCommand(), new JoinCommand(),
new MinusCommand(), new PrintCommand(), new ReadCommand(),
new RenameCommand(), new RestoreCommand(), new SelectionCommand(),
new UnionCommand(), new UpdateCommand() };
/**
* Gets the command from the user input.
*
* @param Scanner ins
* @param String input
* @return the command
*/
public void getCommand(Scanner ins) {
System.out.println("Enter a command.");
String input = "";
try {
//enterLine(ins);
input = MultiLine(ins);
} catch (NoSuchElementException e) {
return;
}
processCommand(input);
getCommand(ins);
}
//private void enterLine(Scanner ins){
//while (!(ins.nextLine()==null))
//System.out.println("Enter a command.");
//}
private void processCommand(String input) {
for (ICommand c : commands) {
if (c.matches(input)) {
try {
c.execute();
return;
} catch (FullerException e) {
System.out.println(e.getMessage());
}
}
}
System.out.println("This is an incorrect Statment.");
}
/**
* MultiLine. Allows the user to type on multiple lines before ending a
* command.
* @param Scanner ins
* @return String input
*/
private String MultiLine(Scanner ins) {
String input;
input = ins.nextLine();
while (!input.contains(";")) {
input = input + " " + ins.nextLine();
}
return input;
}
/**
* The main method that runs the program.
*
* @param args the arguments
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new CS210Main().getCommand(new Scanner(System.in));
}
}
______________________________________________________________________
package one.commands;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import one.main.CS210Main;
import one.main.FullerException;
// TODO: Auto-generated Javadoc
/**
* The Class ReadCommand.
* Reads in commands from user input or a file.
*
*/
public class ReadCommand implements ICommand {
/** The pattern. */
private final Pattern pattern = Pattern.compile(
"Read\s+'\s*(.*?)\s*'\s*;", Pattern.CASE_INSENSITIVE);
/** The String fileName. */
private String fileName;
/** The Scanner ins. */
private Scanner ins;
/*
* (non-Javadoc)
*
* @see one.commands.ICommand#matches(java.lang.String)
*/
@Override
public boolean matches(String str) {
Matcher matcher = pattern.matcher(str);
boolean result = matcher.find();
if (result) {
fileName = matcher.group(1);
}
return result;
}
/*
* (non-Javadoc)
*
* @see one.commands.ICommand#execute()
*/
@Override
public void execute() throws FullerException {
try {
ins = new Scanner(new FileReader(fileName));
} catch (FileNotFoundException e) {
throw new FullerException("File not found.");
}
new CS210Main().getCommand(ins);
}
}
/**
* The main method.
*
* @param args the arguments
*/
Explanation / Answer
I haven't done alot of Java, however - the following block appears to be recursive - ie- where it calls itself. In recursion, terminating conditions are critical, to avoid infinite loops. /** * Gets the command from the user input. * * @param Scanner ins * @param String input * @return the command */ public void getCommand(Scanner ins) { System.out.println("Enter a command."); String input = ""; try { //enterLine(ins); input = MultiLine(ins); } catch (NoSuchElementException e) { return; } processCommand(input); getCommand(ins); /* THIS LOOKS RECURSIVE */ /* if the recursion is intentional, then you'll need a terminating condition, if not - perhaps remove the internal call? */ }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.