I'm trying to make a simple text based calculator in java my first program, EVER and I can't figure out how to turn an input String into a variable opOne. I would then attempt to operate numOne against numTwo using opOne as the operator. Code is as follows:
import java.io.*; import java.math.*; public class ReadString { public static void main (String[] args) { System.out.print("Enter the first number: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int numOne = 0 ; int numTwo = 0 ; String opOne = null; while(true){ try { numOne = Integer.valueOf(br.readLine()); break; } catch (IOException error) { System.out.println("error; try again."); System.exit(1); } catch (NumberFormatException nfe) { System.out.println("error;try again."); } } System.out.print("Enter the second number: "); while(true){ try { numTwo = Integer.valueOf(br.readLine()); break; } catch (IOException error2) { System.out.println("error"); System.exit(1); } catch (NumberFormatException nfe) { System.out.println("error;try again."); } } System.out.println("What would you like to do with " + numOne + " and " + numTwo + "?"); try { operator = br.readLine(); } catch (IOException ioe) { System.out.println("error"); System.exit(1); } catch (NumberFormatException nfe) { System.out.println("error"); } } }
ScriptEngineinstead. E.G. 1) in web page (applet) 2) non GUI 3) simple GUI 4) nice GUI..