Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By sunde887

Thread: Basic Java Calculator Code Problem

  1. #1
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default 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:

    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);
    	}	
    }
    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?
    Last edited by JosAH; 07-21-2011 at 08:22 PM. Reason: added [code] ... [/code] tags.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,433
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    SerbianSergeant likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also; what if Letter1(should be letter1), is not a, s or m? What value will result be then?
    SerbianSergeant likes this.

  4. #4
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    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

  5. #5
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default

    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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  7. #7
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default

    Read the API documentation for the Scanner class.
    can you give a link?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. help with basic sum calculator in java
    By java157 in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 11:12 PM
  2. Java calculator problem ???
    By danielmessick in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2010, 06:53 PM
  3. Basic Calculator
    By trtoy in forum New To Java
    Replies: 1
    Last Post: 12-25-2009, 01:06 AM
  4. Basic Calculator
    By Rose88 in forum Java Applets
    Replies: 3
    Last Post: 06-25-2009, 12:34 AM
  5. help with basic java code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •