Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Simple Calculator
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="TomorrowIsAMystery" data-source="post: 298779" data-attributes="member: 51085"><p>[CODE] </p><p> public static void run(){</p><p> System.out.println("Input the value which you would like to calculate, include an addition sign with no spaces");</p><p> Scanner user = new Scanner(System.in);</p><p> String userQuestion = user.nextLine();</p><p> int lengthofMaths = userQuestion.length(); //this is the length of what the user put in</p><p> String splitter[] = new String[3]; //assigning array</p><p></p><p> </p><p> for (int i = 0; i <=lengthofMaths; i++){ //this loops through the question that they had put in</p><p> int location1 = 0; //setting location at index 0</p><p> location1 = userQuestion.indexOf("+"); //what im doing here is making it loop to find the addition sign</p><p> splitter[1] = userQuestion.substring(0, location1); //once its found the addition it assigns splitter[1] to equal everything before it</p><p> splitter[2] = userQuestion.substring(location1 + 1, lengthofMaths); //and here is everything after it</p><p> </p><p> }</p><p> </p><p> int y = Integer.parseInt(splitter[1]); //i didn't think ahead so i needed to parse my strings into integers so i could display the final answer</p><p> int x = Integer.parseInt(splitter[2]); //same thing for the second value</p><p> </p><p> int finalCalculation = x + y; //adding them up</p><p> System.out.println("The answer is... " + finalCalculation); //display</p><p> </p><p> </p><p>}</p><p> [/CODE]</p><p>I talked the talk, but can I walk the walk?</p><p></p><p>This is a simple calculator only for addition showing how I used arrays. If its inefficent someone do please tell me, but this is also here for others to learn and understand</p></blockquote><p></p>
[QUOTE="TomorrowIsAMystery, post: 298779, member: 51085"] [CODE] public static void run(){ System.out.println("Input the value which you would like to calculate, include an addition sign with no spaces"); Scanner user = new Scanner(System.in); String userQuestion = user.nextLine(); int lengthofMaths = userQuestion.length(); //this is the length of what the user put in String splitter[] = new String[3]; //assigning array for (int i = 0; i <=lengthofMaths; i++){ //this loops through the question that they had put in int location1 = 0; //setting location at index 0 location1 = userQuestion.indexOf("+"); //what im doing here is making it loop to find the addition sign splitter[1] = userQuestion.substring(0, location1); //once its found the addition it assigns splitter[1] to equal everything before it splitter[2] = userQuestion.substring(location1 + 1, lengthofMaths); //and here is everything after it } int y = Integer.parseInt(splitter[1]); //i didn't think ahead so i needed to parse my strings into integers so i could display the final answer int x = Integer.parseInt(splitter[2]); //same thing for the second value int finalCalculation = x + y; //adding them up System.out.println("The answer is... " + finalCalculation); //display } [/CODE] I talked the talk, but can I walk the walk? This is a simple calculator only for addition showing how I used arrays. If its inefficent someone do please tell me, but this is also here for others to learn and understand [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Simple Calculator
Top