0

"Source not found." appears when the program try to get a char from user. What character I input for testing is '3'. We can also encounter the same problem when we try other characters except '5'.

Code:

package assignment1;

import java.util.Scanner;

public class main_menu {

    private static char main_menu_choice = '0';

    private static void setMainMenuChoice(char choice) {
        main_menu_choice = choice;
    }
    private static char getMainMenuChoice() {
        return main_menu_choice;
    }
    public static void main(String[] args) {
        do {
            showMainMenu();
            setMainMenuChoice( getChoiceFromUser() );
        } while (getMainMenuChoice() != '5');
    }

    private static void showMainMenu() {
        System.out.println("----------------Main Menu----------------");
        System.out.println("1. Initialize the address book");
        System.out.println("2. Create person contact information");
        System.out.println("3. Lookup person contact information");
        System.out.println("4. Lookup all person contact information");
        System.out.println("5. Quit");
        System.out.println();
        System.out.print("Enter your choice : ");
    }

    private static char getChoiceFromUser() {
        Scanner reader = new Scanner(System.in); <--------"Source not found."
        char choice = reader.next().charAt(0);
        reader.close();
        return choice;
    }
}

How to solve this problem?

Zong
  • 5,701
  • 5
  • 27
  • 46
Casper
  • 3,259
  • 9
  • 37
  • 64
  • 1
    The issue here is that you are creating your `Scanner` more than once (similar to [this question][1]). Try moving your `reader` so that it is only instantiated once, and don't close it. [1]: http://stackoverflow.com/questions/19766566/java-multiple-scanners – Evan Knowles May 23 '14 at 13:21
  • @EvanKnowles Have you tried it on your machine? – Braj May 23 '14 at 13:22
  • This is definitely not a debugging source not found issue. – Evan Knowles May 23 '14 at 13:33

3 Answers3

0

You can refactor the application for example to create the Scanner on start and closing it when the application exits.

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    do {
        showMainMenu();
        setMainMenuChoice(getChoiceFromUser(reader));
    } while (getMainMenuChoice() != '5');
    reader.close();
}

. . .

private static char getChoiceFromUser(Scanner reader) {
    char choice = reader.next().charAt(0);
    return choice;
}
Xantier
  • 292
  • 2
  • 11
-1

I get another exception, when try to run your code.

Exception in thread "main" java.util.NoSuchElementException

So first of all you need to delete reader.close(); because you try to close System.in and this is not a good idea.

From Java Doc's:

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

Aleksandr Podkutin
  • 2,333
  • 1
  • 15
  • 28
  • Nope. System.in does not implement Closeable as far as I know, and also it is standard practice to close your Scanner upon being done using it. – Ricky Mutschlechner May 23 '14 at 14:01
  • @RickyMutschlechner I know that System.in does not implement Closeable interface, I don't wrote it. I wrote that this guy "try to close System.in and this is not a good idea", exactly because System.in does not implement Closeable interface. – Aleksandr Podkutin May 25 '14 at 09:21
-1

A possible way of doing what was suggested in the comments would be:

package assignment1;

import java.util.Scanner;

public class main_menu {

    private static char main_menu_choice = '0';
    static Scanner reader = new Scanner(System.in);

    private static void setMainMenuChoice(char choice) {
        main_menu_choice = choice;
    }
    private static char getMainMenuChoice() {
        return main_menu_choice;
    }
    public static void main(String[] args) {
        do {
            showMainMenu();
            setMainMenuChoice( getChoiceFromUser() );
            System.out.println(main_menu_choice);
        } while (getMainMenuChoice() != '5');
        reader.close();
    }

    private static void showMainMenu() {
        System.out.println("----------------Main Menu----------------");
        System.out.println("1. Initialize the address book");
        System.out.println("2. Create person contact information");
        System.out.println("3. Lookup person contact information");
        System.out.println("4. Lookup all person contact information");
        System.out.println("5. Quit");
        System.out.println();
        System.out.print("Enter your choice : ");
    }

    private static char getChoiceFromUser() {
         //<--------"Source not found."
        char choice = reader.next().charAt(0);

        return choice;
    }
}

So if your main_menu class is instantiated a Scanner is created and after quitting it is closed. Also I printed out the choice the user made to see if it is working.

rhaecker
  • 52
  • 5