0

Okay, I'm working on something kind of like the Windows CMD, but without the access commands. It will have commands from my friends and me, but when I add the second command and test it, it does't work. If anybody can help me with this I would love to get this done right.

import java.util.*;

public class Console {

private static final Scanner cmd = null;
public static void main(String[] args)
{
    cmd();
}
private static void cmd()
{
    System.out.print(">");
    final Scanner cmd = new Scanner(System.in);

    if (cmd.next("help") != null)
    {
        help();
    }

    if(cmd.next("ping") != null)
    {
        ping();
    }

}



private static void ping() {

    System.out.println("Pong!");

}
private static void help() {
    System.out.println("ADD" + "                     Adds 2 numbers");
    System.out.println("SUBTRACT" + "                Subtracts 2 numbers");
    System.out.println("MULTIPLY" + "                Multiplies 2 numbers");
    System.out.println("PING" + "                    Pong!");
    cmd();
}

}
Groax
  • 23
  • 1
  • 1
  • 9
  • Define "it doesn't work". – JB Nizet Oct 31 '14 at 22:16
  • It crashes and will not loop back to the "cmd" method. @JBNizet – Groax Oct 31 '14 at 22:16
  • 2
    The code is so rotten, it can't really work. I'd suggest you read some tutorial on how to use `Scanner` properly. – MightyPork Oct 31 '14 at 22:18
  • @Groax 1. your using the `next(String)` method wrong. Look at `nextLine()`. 2. Dont call `cmd()` from the `help()` method. This is very very bad, you will end up with a stack that keeps building because no method ever returns. Instead use a loop inside the `cmd()` method that loops until you enter something like quit. – ug_ Oct 31 '14 at 22:23
  • @JBNizet It says there are errors on lines 15, 39, 17, and 8 – Groax Oct 31 '14 at 22:24
  • So, fix them? Any IDE will highlight the errors and show you what error you have with the syntax. – MightyPork Oct 31 '14 at 22:24
  • @ug_ Thank you, I will try that. You're the first person to help me. – Groax Oct 31 '14 at 22:25
  • @Groax: your compiler/IDE helps you. It displays specific, precise error messages, indicating what is wrong and where. But you seem to choose to ignore these messages, and also leave us in the dark. Read error messages, and post them if you don't understand them. Posting code and saying "it doesn't work, please help" is not a good way to ask a question. You don't go to the doctor and say "It hurts, please heal me", do you? – JB Nizet Oct 31 '14 at 22:28
  • @MightyPork it doesn't show the errors because there are none. It just can't read it correctly for some reason. I'm gonna try what ug_ said. – Groax Oct 31 '14 at 22:45
  • @ug_ I tried the `nextLine()` and it is not running the `help()` method, I've tried putting the `System.out.println()` statements in the if statements, but to no avail. It just prints an empty line. If you have a code sample please post it as an answer. – Groax Oct 31 '14 at 22:57
  • @JBNizet I there are no error messages. The my IDE (eclipse) would come up with a prompt telling me it has an error and asking me if I still want to run it. It just runs and if I enter anything other than help with the code above it just "stops" so to speak. – Groax Oct 31 '14 at 22:59
  • If it tells you "there are errors, do you still want to run it", then it means that... there are errors. Open the "Problems" view or the "Markers" view, and let it opened constantly. Don't even think about running the code while there are compilation errors. – JB Nizet Oct 31 '14 at 23:18
  • @JBNizet ug_ solved my problem. Thank you for your interest – Groax Oct 31 '14 at 23:54

4 Answers4

0

You have a few errors with your program. The first one is the method you are using to take the user input is incorrect. You should be using the Scanner.nextLine() method, this will read the user input until a new line character is read (AKA they press enter). Making this modification will make your cmd() method read something like this:

System.out.print(">");
final Scanner cmd = new Scanner(System.in);
String userInput = cmd.nextLine();
if (userInput.equals("help")) {
    help();
} else if(userInput.equals("ping")) {
    ping();
}

Additionally you are have some very bad juju inside the help() method. Inside the help() method you call the cmd() method again. To understand why this is bad let me give you a scenario. Pretend your program worked and this is your user input and the -- help outputlisted was what your help method printed out.

>help
-- help outputlisted
>help
-- help outputlisted
>help
-- help outputlisted

What would be going on in your program is the none of the methods would ever terminate. You would end up with a call stack like this:

cmd()
    help()
        cmd()
            help()
                cmd()
                    help()
                    ......on and on and on.....

Neither the cmd or help method would terminate! You are essentially using recursion, if you built more methods using this design pattern you would surely end up with weird problems. Bottom line is its just bad form.

What you should be doing is using a loop inside your cmd method. The loop below will keep prompting for user input until the line reads "quit". When the user enters quit then the loop will terminate and the cmd() method will end.

final Scanner cmd = new Scanner(System.in);
String userInput = null;
do {
    System.out.print(">");
    userInput = cmd.nextLine();
    if (userInput.equals("help")) {
        help();
    } else if(userInput.equals("ping")) {
        ping();
    }
} while(userInput != null && !userInput.equals("quit"));

Also remove the cmd() call from your help() method..


You have a static final variable named cmd. Static variables in java should follow the UPPER_SNAKE_CASE naming convention. Also you might rename either the variable or your method. Having them both named cmd probably isnt the best, also not very descriptive of what it is.

ug_
  • 11,267
  • 2
  • 35
  • 52
  • I know this has been on here for a while but I'd like to say that you're help helped me create my program and I have continued it since. I'm probably going to have to make categories in the help instead of individual commands. Keep doing what you're doing! – Groax Apr 29 '15 at 14:01
0
public static void main(String[] args) {
    cmd();
}

private static void cmd() {
    Scanner input = new Scanner(System.in);

    System.out.print("> ");
    String command = input.next();

    while (!command.equals("exit")) {

        if (command.equals("help")) {
            help();
            System.out.print("> ");
            command = input.next();
        } else if (command.equals("ping")) {
            ping();
            System.out.print("> ");
            command = input.next();
        }
    }
}

private static void ping() {
    System.out.println("Pong!");
}

private static void help() {
    System.out.println("ADD" + "                     Adds 2 numbers");
    System.out.println("SUBTRACT" + "                Subtracts 2 numbers");
    System.out.println("MULTIPLY" + "                Multiplies 2 numbers");
    System.out.println("PING" + "                    Pong!");
}
Shahzad
  • 2,033
  • 1
  • 16
  • 23
0

From here you just think the other methods you are missing.

import java.util.*;

public class Console {

static Scanner cmd = new Scanner(System.in);
public static Boolean Exit = false;

public static void main(String[] args)
{
    do {
        cmd();
    } while(!Exit);
}

private static void cmd()
{
    String Command;
    System.out.print(">");
    Command = cmd.nextLine().trim();

    if (Command.equalsIgnoreCase("help")) {
        help();
    }
    else if (Command.equalsIgnoreCase("ping")) {
        ping();
    }
    else if (Command.equalsIgnoreCase("exit")) {
        Exit = true;
    }
    //Here Add more else if as are created more methods.
    else {
        System.out.println("Unrecognized command!");
    }          
}

private static void ping() {
    System.out.println("Pong!");
}

private static void help() {
    System.out.println("ADD" + "                     Adds 2 numbers");
    System.out.println("SUBTRACT" + "                Subtracts 2 numbers");
    System.out.println("MULTIPLY" + "                Multiplies 2 numbers");
    System.out.println("PING" + "                    Pong!");
    System.out.println("EXIT" + "                    End of Program!");
}
}
SkyMaster
  • 1,323
  • 1
  • 8
  • 15
-3

why don't you use something like this

import java.util.Scanner;

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter command: ");
String cmd = keyboard.nextLine();
if (cmd == "help"){
help();
}
Fafore Tunde
  • 319
  • 2
  • 8
  • 4
    [How do I compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – JB Nizet Oct 31 '14 at 22:40