Results 1 to 8 of 8
- 07-21-2011, 07:50 PM #1
Basic Java Calculator Code Problem
Hello forums for the first time. I am new to Java and am looking to learn it for my A2 computing project. I'm using the latest Eclipse IDE.
Here is my code so far:
Forgive me if there are any signs of bad programming practice. But anyway, if i run this code it says that 'result may not have been initialized'. If i assign 'result' with a value of zero for example it runs, but not correctly. Can anyone help me?Java Code:class Calculator { public static void main(String[] args) throws Exception { int num1,num2,result; char Letter1; System.out.println("Enter number 1..."); num1 = (int)System.in.read(); System.out.println("Enter number 2..."); num2 = (int)System.in.read(); System.out.println("add, sub or mul?"); Letter1 = (char)System.in.read(); if (Letter1 == 'a') result = num1 + num2; if (Letter1 == 's') result = num1 - num2; if (Letter1 == 'm') result = num1 * num2; System.out.println(result); } }Last edited by JosAH; 07-21-2011 at 08:22 PM. Reason: added [code] ... [/code] tags.
- 07-21-2011, 08:24 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Note that System.in.read() reads just a single byte; you don't want that. Read the API documentation for the Scanner class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-21-2011, 09:54 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Also; what if Letter1(should be letter1), is not a, s or m? What value will result be then?
- 07-22-2011, 09:17 AM #4
Thanks for your help.
This is the page i looked at to help me, hopefully it can help others who look at this post: Scanner class
- 07-22-2011, 09:27 AM #5
I have changed the variable declaration to 'letter1'. Ahh ok that makes sense, if i don't declare 'result' with a value it doesn't run for that very reason. If i declare it with zero it runs and if someone enters something that is not a, s or m then it simply returns with the value of zero.
I'm more familiar with pascal since that is what we've been learning this year. Is there some form of loop in java similar to the "Repeat...Until" in pascal that i can use? I will use this so that the user cannot enter a value other than what is asked for.
- 07-22-2011, 09:29 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You can use a do...while, while, or a for loop. A switch statement is also very helpful for doing something based on a character of input. Google java tutorial <term> to find out about them.
- 07-22-2011, 01:47 PM #7
can you give a link?Read the API documentation for the Scanner class.
- 07-22-2011, 01:50 PM #8
Similar Threads
-
help with basic sum calculator in java
By java157 in forum New To JavaReplies: 8Last Post: 03-17-2011, 11:12 PM -
Java calculator problem ???
By danielmessick in forum Advanced JavaReplies: 2Last Post: 03-13-2010, 06:53 PM -
Basic Calculator
By trtoy in forum New To JavaReplies: 1Last Post: 12-25-2009, 01:06 AM -
Basic Calculator
By Rose88 in forum Java AppletsReplies: 3Last Post: 06-25-2009, 12:34 AM -
help with basic java code
By elizabeth in forum New To JavaReplies: 1Last Post: 08-07-2007, 06:47 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks