fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. userInput ui = new userInput(new Scanner(System.in));
  13. int num = ui.thisMethod();
  14. System.out.println("returned "+ num);
  15. }
  16. }
  17.  
  18. class userInput{
  19. Scanner sc;
  20.  
  21. public userInput(Scanner scanner){
  22. sc = scanner;
  23. }
  24.  
  25. public int thisMethod(){
  26. int n = -1; // this is to just set the variable to something
  27. System.out.println("Question to user");
  28. try{
  29. n = Integer.parseInt(sc.nextLine());
  30. if (n < 0){
  31. //i want specificly want input over 0
  32. n = thisMethod();
  33. }
  34. if (n > 3){
  35. //8 is a random number, but the number is linked to an array length, so the index needs to be lower than 3
  36. n = thisMethod();
  37. }
  38. } catch (NumberFormatException e){
  39. n = thisMethod();
  40. }
  41. return n;
  42. }
  43. }
Success #stdin #stdout 0.12s 36000KB
stdin
10
2
stdout
Question to user
Question to user
returned 2