0

I'm trying to give the user an infinite amount of inputs until they enter q. I'm using a while statement to run the program, but when the user tries to quit I get an error because the program would try and parse q as an integer. Any ideas on how I should change the structuring of this to prevent the error from occurring?

Scanner in = new Scanner(System.in);
System.out.println("What would you like your Fibonacci number to be?(enter q to quit)"); 
String value = in.next(); 
int trueValue;
while(!value.equalsIgnoreCase("q")) { 
    trueValue = Integer.parseInt(value);
    Fibonacci userCase = new Fibonacci(trueValue);
    System.out.println(userCase.calculateFibonacci(userCase.getCaseValue()));
    System.out.println("Please enter another number.");
    value = in.next(); 
    trueValue = Integer.parseInt(value);
} 

If it matters, here are the methods being called within the loop.

public int calculateFibonacci(int caseValue) {
    if(caseValue == 0) 
        return 0; 
    else if(caseValue == 1) 
        return 1; 
    else 
        return calculateFibonacci(caseValue-1) + calculateFibonacci(caseValue-2);
}

public int getCaseValue() 
{ 
    return caseValue;
}
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
Andrew McAvoy
  • 21
  • 1
  • 6

2 Answers2

1

You can remove the last

trueValue = Integer.parseInt(value);

since you are already doing that at the start of the loop.

clinomaniac
  • 2,200
  • 2
  • 17
  • 22
  • 1
    But will also fail if the input given in `String value = in.next();` is not `"q"` and not int. – Juan Carlos Mendoza Jan 30 '18 at 21:17
  • @JuanCarlosMendoza You are right, the program will error out in that scenario but the behavior is not defined for that scenario. Should it continue? Error out? Use the last number as valid input and repeat process? It says it should quit on `q` but doesn't define what other inputs to consider. – clinomaniac Jan 30 '18 at 21:21
0

do{ getting the user value before checking } while(checking if it's ok);

    /* https://stackoverflow.com/questions/40519580/trying-to-determine-if-a-string-is-an-integer */
    private boolean isInteger(String str) {
        if(str == null || str.trim().isEmpty()) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if(!Character.isDigit(str.charAt(i))) {
                return false;
            } 
        }
        return true;
    }

    public static String check(Scanner in) {
        String value;
        do {
           System.out.println("Please enter a number or q to quit.");
           value = in.next(); 
        } while(!value.equalsIgnoreCase("q") && !isInteger(value));
        return value;
    }

    public static void main (String[] args) { 
          Scanner in = new Scanner(System.in);
          String value = check(in);       
          while(!value.equalsIgnoreCase("q")) { 
              Fibonacci userCase = new Fibonacci(Integer.parseInt(value));
              System.out.println(userCase.calculateFibonacci(userCase.getCaseValue()));
              value = check(in);        
          }
          in.close();   
    }
romph
  • 487
  • 3
  • 11