java - Getting A StackOverFlowError when making a basic calcualator -
i trying make calculator 4 functions , word based. when tried implement selecting of operations, stackoverflow error.
class calculator:
package us.plexproductions.main; import java.util.scanner; public class calculator { public static int firstnum; public static int secondnum; public static int answer; public static int operation; calculator c = new calculator(); static scanner s = new scanner(system.in); static operations o = new operations(); public static void main(string[] args) { prompt(); start(); } public static void prompt() { system.out.println("########################"); system.out.println("#calculator version 1.0#"); system.out.println("########################"); system.out.println("************************"); system.out.println("this calculator that\ncan add, subtract, multiply, , divide\n" + "multiple numbers @ once."); system.out.println("------------------------"); system.out.println("start calculating entering number , then\nspelling out operation " + "want preform that\nnumber , type in number."); } public static void start() { system.out.println("------------------------"); system.out.println("enter first number: "); firstnum = s.nextint(); system.out.println("------------------------"); system.out.println("which operation use?\naddition = 1, subtraction = 2, multiplication = 3, or division = 4?"); operation = s.nextint(); second(); } public static void second() { if (operation == 1) { o.addition(); } if (operation == 2) { o.subtraction(); } if (operation == 3) { o.multiplication(); } if (operation == 4) { o.division(); } } }
class operations:
package us.plexproductions.main; import java.util.scanner; public class operations { calculator c = new calculator(); scanner s2 = new scanner(system.in); public void addition() { system.out.println("addition selected."); system.out.println("enter second number: "); calculator.secondnum = s2.nextint(); } public void subtraction() { } public void multiplication() { } public void division() { } }
each instance of calculator
creates new calculator()
field c
. these calculators
, in turn, create more calculators
, , on.
the stackoverflowerror
occurs because sequence of calls calculator
constructor never ends, filling available stack memory. doesn't appear need nested calculator
, try removing it.
Comments
Post a Comment