java.util.scanner - Java Scanner: Continuous loop -


when input valid option (a,b,c) if statement thinks option a, b, or c, , lead continuous loop.

package other;  import java.util.scanner;  public class menu implements interfacemenu{  private string option;  public void greeting(){      system.out.println("this program use pythagorean theorem");     system.out.println("to calculate missing side.\n");     system.out.println("choose option!\n");     system.out.println("choose option missing side c");     system.out.println("choose option b missing side b");     system.out.println("choose option c missing side a\n");  }  public string optionget(){      system.out.print("choose option: ");     scanner ad = new scanner(system.in);     option=ad.next().touppercase();      if( (option=="a") || (option=="b") || (option=="c") ){         ad.close();     }     else{         optionget();     }      return option; }  } 

aside using wrong method compare strings, there serious problem: optionget() opens new scanner on system.in. if input contains option not a, b, or c, optionget() calls recursively. recursive call

scanner ad = new scanner(system.in); 

while first 1 still open. there no telling do, cause problems. not that. don't use recursion. it's inappropriate here. instead, put code checks options in loop, , make sure new scanner outside loop.


Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -