mardi 6 février 2018

Using a class from another folder - JAVA

I have a java file with no main (Call.java) with a set of methods. I have another java file that I want to call the constructor in Call.java. Here is the structure: link to file structure

I'm using the command-line to run java programs.

My question is: how can I do so without getting an error

Source code: Class:

import java.util.*;

public class Call{
private int userTypedInt;
Scanner userInput = new Scanner(System.in);

Call(){
    while(true){
        mainMenu();
    }
}

public void mainMenu(){
    System.out.println("\n\t\t####| Main Menu |#### \n");
    System.out.println("\t\t0. Exit the program");
    System.out.println("\t\t1. Option");

    // check that the user typed a number - specifically an int 
    try{
        System.out.print("\nEnter a choice: ");
        userTypedInt = userInput.nextInt();
        path(userTypedInt);
    }catch(InputMismatchException e){
        System.out.println("\n\t\tInvalid entry");
        userInput.nextLine(); // clears the buffer
    }
}

public void path(int pick){
    switch(pick){
        case 0:
            userInput.close();
            System.exit(0);
            break;
        case 1:
            break;
        default:
            System.out.println("\t\tInvalid Entry");
    }
}
}

Test file:

public class Test implements Call{
    public static void main(String[] args){
        System.out.println("Online");
        new Call();

    }
}





Aucun commentaire:

Enregistrer un commentaire