-5

I am writing a console application. UI would be like this.

Please input :

  1. to learn java
  2. to learn VB.NET
  3. to quit

if user enters 1 it will go for java learning, if enters 2 it will go for VB learning, if enters 3 program will quit. For all other inputs the program will show the above message again.

I implemented this in below way :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DriverTest {

public static void main(String[] args) {
    new DriverTest().takeOver();
}

public void takeOver() {
    BufferedReader inputScanner = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please enter:");
    System.out.println("1. to learn JAVA");
    System.out.println("2. to learn VB");
    System.out.println("3. to quit");

    try {
        int actionKey = Integer.parseInt(inputScanner.readLine());
        switch (actionKey) {
        case 1:
            System.out.println("Welcome to JAVA learning");
            break;
        case 2:
            System.out.println("Welcome to VB learning");
            break;
        case 3:
            System.exit(0);
            break;
        default:
            System.out.println("Please select a proper action :");
            takeOver();
        }
    } catch (NumberFormatException | IOException e) {
        System.out.println("Some error occured please try again. :(");
        takeOver();
    }
}

}

Now if user continuously provide invalid input, it will result eventually into StackOverflow error.(You can test it, copy the class in folder.complie it : javac DriverTest.java, create one text file input.txt and write anything except 1,2 and 3. e.g. hgsad, now run java DriverTest < input.txt)

Please suggest, if there is any better solution of the problem.

Community
  • 1
  • 1
anunaki
  • 989
  • 9
  • 16
  • Use a loop instead of recursion? I see no reason why this has to be a recursive function. – David Jun 05 '16 at 15:40
  • David let say I have another driver called JavaLearningDriver which also show some message and based on user interaction it do something. It also return to previous menu by calling DriverTest.takeOver() . Now how I will use loop in that case? – anunaki Jun 05 '16 at 15:44
  • That's a fairly vague description of the logic, but it doesn't sound like it would need recursion either. There are generally very few logical scenarios which call for recursion. – David Jun 05 '16 at 15:46
  • check the actual code here https://github.com/chandrakgeccse13/mithrems . You will understand the problem. – anunaki Jun 05 '16 at 15:50
  • 1
    If you mean to say that the code posted in the question is *not* the actual code then how can you expect anybody to help in the first place? This isn't a scavenger hunt, and we're not going to debug your code for you. The problem presented in the question has good and valid answers below. If there are other problems unrelated to this one, perhaps they merit another question. – David Jun 05 '16 at 15:52
  • Just check https://github.com/chandrakgeccse13/mithrems/blob/master/src/main/java/mithr/ems/driver/MainDriver.java and https://github.com/chandrakgeccse13/mithrems/blob/master/src/main/java/mithr/ems/driver/EventStoreDriver.java – anunaki Jun 05 '16 at 15:54
  • Just 2 click David no need to debug, Code are written in clear way. and sorry if I offended you. – anunaki Jun 05 '16 at 15:56
  • You are misunderstanding how Stack Overflow works. Relevant information should be included in the question. Off-site links have a tendency to go stale over time. – David Jun 05 '16 at 15:58

3 Answers3

2

You keep calling takeOver() recursively. If you do that enough times, you will eventually reach a point where your call stack overflows. If you want to avoid that, call your code in a loop instead of using recursion.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
2

You are calling takeOver() recusively, so you keep adding started functions to the stack, hence the StackOverflow. You should use a loop instead:

public void takeOver() {
    BufferedReader inputScanner = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please enter:");

    boolean inLoop = true;
    while(inLoop){

        System.out.println("1. to learn JAVA");
        System.out.println("2. to learn VB");
        System.out.println("3. to quit");

        try {
            int actionKey = Integer.parseInt(inputScanner.readLine());
            switch (actionKey) {
            case 1:
                System.out.println("Welcome to JAVA learning");
                inLoop = false;
                break;
            case 2:
                System.out.println("Welcome to VB learning");
                inLoop = false;
                break;
            case 3:
                System.exit(0);
            default:
                System.out.println("Please select a proper action :");
            }
        } catch (NumberFormatException | IOException e) {
            System.out.println("Some error occured please try again. :(");
        }
    }
}
Keiwan
  • 8,031
  • 5
  • 36
  • 49
0

Invoking takeOver() recursively adds the call to the stack continuously and you receive a StackOverflow error. Use a loop instead.

public void takeOver() {
BufferedReader inputScanner = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter:");
System.out.println("1. to learn JAVA");
System.out.println("2. to learn VB");
System.out.println("3. to quit");
boolean continue = true;
while(continue)
{
    int actionKey = Integer.parseInt(inputScanner.readLine());
    switch (actionKey) {
    case 1:
        System.out.println("Welcome to JAVA learning");
        continue = false;
    case 2:
        System.out.println("Welcome to VB learning");
        continue = false;
    case 3:
        System.exit(0);
        continue = false;
    default:
        System.out.println("Please select a proper action :");
    }
} 
}
Sashi
  • 1,977
  • 16
  • 15