4
\$\begingroup\$

I'm trying to getting integer keyboard input to call the corresponding method.

invalid input works, keyboard input 3 works, input 2 exits, input 1 calls the wrong method. I've not yet worked on the subtring and points problem method yet.

Regards, and I appreciate your feedback.

import java.io.File;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;



public class WordGames {

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


    getSelection();
    File file = new File("DICTIONARY.txt");
        if (file.isFile() == true) {
        }
        else {
            System.out.println("File doesn't exist.  Exiting.");
            System.exit(0);  
        }
    
    subStringProblem();
    pointsProblem();
            
    }
    
    static String getSelection() {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Welcome to the Word Games program menu.");
        System.out.println("Select from one of the following options.");
        System.out.println("1. Substring problem.");
        System.out.println("2. Points problem.");
        System.out.println("3. Exit.");
        System.out.println("Enter your selections: ");
        
        
        if ( ! keyboardInput.hasNextInt()) {
           
           System.out.println("Invalid option.  Try again.");
           getSelection();
            
        } else if (keyboardInput.hasNextInt(1)){
            subStringProblem(); 
            getSelection();
            
        } else if (keyboardInput.hasNextInt(2)){
            pointsProblem();
            getSelection();
            
        } else if (keyboardInput.hasNextInt(3)){
        }    System.out.println("Good Bye!");
             System.exit(0);  
        return null;
    }
        
        
    static String subStringProblem() {
        System.out.println("Substring problem.");
        System.out.println("Enter a Substring:");
    return null;
    }
    static String pointsProblem() {
        System.out.println("Points problem.");
        
    return null;
    }
}
\$\endgroup\$
5
\$\begingroup\$

1. Proper indentation

Please have a look at a style guide. One important topic of style guides is how to "correctly" indent your code:

import java.io.File;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;

public class WordGames {

    public static void main(String[] args) throws FileNotFoundException {
        getSelection();
        File file = new File("DICTIONARY.txt");
        if (file.isFile() == true) {
        }
        else {
            System.out.println("File doesn't exist.  Exiting.");
            System.exit(0);  
        }

        subStringProblem();
        pointsProblem();
    }
    
    static String getSelection() {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Welcome to the Word Games program menu.");
        System.out.println("Select from one of the following options.");
        System.out.println("1. Substring problem.");
        System.out.println("2. Points problem.");
        System.out.println("3. Exit.");
        System.out.println("Enter your selections: ");
              
        if (!keyboardInput.hasNextInt()) {
           
           System.out.println("Invalid option.  Try again.");
           getSelection();
            
        } else if (keyboardInput.hasNextInt(1)) {
            subStringProblem(); 
            getSelection();
            
        } else if (keyboardInput.hasNextInt(2)) {
            pointsProblem();
            getSelection();
            
        } else if (keyboardInput.hasNextInt(3)) {
            System.out.println("Good Bye!");
            System.exit(0);
        }  
        return null;
    }
            
    static String subStringProblem() {
        System.out.println("Substring problem.");
        System.out.println("Enter a Substring:");
        return null;
    }
    
    static String pointsProblem() {
        System.out.println("Points problem.");     
        return null;
    }
}

2. Empty if

if (file.isFile() == true) {
}
else {
    System.out.println("File doesn't exist.  Exiting.");
    System.exit(0);  
}

This is not necessary. Just use

if (file.isFile()) {
}
else {
    System.out.println("File doesn't exist.  Exiting.");
    System.exit(0);  
}

Without an == the if will automatically check if the value is true. Even better:

if(!file.isFile()) {
    System.out.println("File doesn't exist.  Exiting.");
    System.exit(0);  
}

Empty if-statements should be avoided.

3.Input validation

In getSelection(), your program doesn't crash when the user enters a letter instead of a number. That is good. But your program does crash, when the user enters a number not equal to 1, 2 or 3. This problem can be avoided like this:

int sel = 0;
try{
    sel = keyboardInput.nextInt();
} catch (InputMismatchException e){
    keyboardInput.next();
    System.out.println("Invalid option. Try again.");
    getSelection();
}

if (sel == 1) {
    subStringProblem(); 
    getSelection();
}
else if (sel == 2) {
    pointsProblem();
    getSelection();
}   
else if (sel == 3) {
    System.out.println("Good Bye!");
    System.exit(0);
}
else {
    System.out.println("Invalid option. Try again.");
    getSelection();
}

If you are not familiar with try-catch yet, I suggest reading the documentation.

4.Unnecessary returns

If you don't want to return something in your methods, you can use void as return type instead of String. For example:

static String subStringProblem() {
    System.out.println("Substring problem.");
    System.out.println("Enter a Substring:");
    return null;
}

should be replaced with

static void subStringProblem() {
    System.out.println("Substring problem.");
    System.out.println("Enter a Substring:");
}

5. Access modifiers

It is considered good practice to always add Access modifiers to your methods. Instead of

static void getSelection() {

it should be

private static void getSelection() {

6. throws

The throws-keyword in the main-method is not necessary.

Code

import java.io.File;
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.InputMismatchException;

public class WordGames {

    public static void main(String[] args) {
        getSelection();
        File file = new File("DICTIONARY.txt");
        if(!file.isFile()) {
            System.out.println("File doesn't exist. Exiting.");
            System.exit(0);  
        }
        subStringProblem();
        pointsProblem();
    }
    
    private static void getSelection() {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Welcome to the Word Games program menu.");
        System.out.println("Select from one of the following options.");
        System.out.println("1. Substring problem.");
        System.out.println("2. Points problem.");
        System.out.println("3. Exit.");
        System.out.println("Enter your selections: ");
         
        int sel = 0;
        try{
            sel = keyboardInput.nextInt();
        } catch (InputMismatchException e){
            keyboardInput.next();
            System.out.println("Invalid option. Try again.");
            getSelection();
        }
        
        if (sel == 1) {
            subStringProblem(); 
            getSelection();
        }
        else if (sel == 2) {
            pointsProblem();
            getSelection();
        }   
        else if (sel == 3) {
            System.out.println("Good Bye!");
            System.exit(0);
        }
        else {
            System.out.println("Invalid option. Try again.");
            getSelection();
        }
    }
            
    private static void subStringProblem() {
        System.out.println("Substring problem.");
        System.out.println("Enter a Substring:");
    }

    private static void pointsProblem() {
        System.out.println("Points problem.");     
    }
}
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

getSelection is a recursive method that also terminate the program. There is no need to return anything. All the code behind that call in your main method will never be executed and is useless.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$
        Scanner keyboardInput = new Scanner(System.in);

You call this on each recursive call to getSelection. That's unnecessary. You could simply create one Scanner and use it for the entire program. That could be as simple as adding a class variable:

    private static Scanner keyboardInput = new Scanner(System.in);

Or you could use a try-with-resources and pass it into a constructor for your WordGames object.

        try (Scanner keyboardInput = new Scanner(System.in)) {
            WordGames game = new WordGames(keyboardInput);
            game.play();
        }

Now the Scanner will be automatically closed when finished.

The static version will automatically close the Scanner when the program finishes. Not quite as clean as using the try-with-resources but acceptable for a Scanner.

Your original code will keep opening new Scanner instances for each invalid entry and never closes any of them. With enough entries, it would be possible to crash the program that way.

Of course, even without multiple copies, the recursive calls themselves use an ever increasing number of call stacks. So you can still crash the program just from the recursion. But that will take longer if you only have a single Scanner. Regardless, it would be better to rewrite your program's logic as a loop (while, for, etc.) rather than recursively. Recursion is used when you care about the state. But in this case, you don't.

As a practical matter, it would be difficult to crash this simple program by entering an endless stream of 4s. But it wouldn't be impossible. And it's probably a bad pattern to get in the habit of using, because if the program is less I/O based, the computer can more easily overrun its limits.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.