Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Need help with creating simple calculater

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with creating simple calculater

    Hey,

    I made myself a small calculator, but I want to to choose what to use instead of only using plus.

    import java.util.Scanner;
     
    class calculator{
    	public static void main(String args[]){
    		Scanner thingy = new Scanner(System.in);
    		double firstnumber, secondnumber, answer;
     
    		System.out.println("Enter first number:");
    		firstnumber = thingy.nextDouble();			
    		System.out.println("Enter second number");
    		secondnumber = thingy.nextDouble();
    		answer = firstnumber + secondnumber;
    		System.out.println(answer);
    	}
    }

    What do I need to write in order to make it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    You need to decide what you want the code to do, what steps are needed and write the java code that will do those steps. Do you know what steps you want the code to do?

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    import java.util.Scanner;

    class epler{
    public static void main(String args[]){
    Scanner scn = new Scanner(System.in);
    double firstnumber, secondnumber, answer;

    //Need a declaration for my mode, not sure which.


    System.out.println("Enter first number: ");
    firstnumber = scn.nextDouble();

    System.out.println("Enter method: ");
    mode = scn.nextInt();
    if(mode == '+'){
    System.out.println("Enter second number: ");
    secondnumber = scn.nextDouble();

    answer = firstnumber + secondnumber;
    System.out.println(answer);
    }else if(mode == '-'){
    System.out.println("Enter second number: ");
    secondnumber = scn.nextDouble();

    answer = firstnumber - secondnumber;
    System.out.println(answer);
    }else if(mode == '*'){
    System.out.println("Enter second number: ");
    secondnumber = scn.nextDouble();

    answer = firstnumber * secondnumber;
    System.out.println(answer);
    }else if(mode == '/'){
    System.out.println("Enter second number: ");
    secondnumber = scn.nextDouble();

    answer = firstnumber / secondnumber;
    System.out.println(answer);
    }

    }
    }
    This is what I've gotten to, but I need a declaration for my mode variable. Anyone knows what kind?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    What type of data are you going to store in the mode variable?
    That would determine how you define it.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    The mode would be for the method user input. What is written in the input in "Enter method: ", will determine if you are going to +, -, * or /.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    Do you want mode to hold a String or a char value? "+' or '+'

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    Not quite sure. I'm here cause I need some help finishing this one. How would you have done it?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    The Scanner class has a method for reading a String, so that might be easier.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    I'm not really into Strings. How do I use them?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    Take a look at the API doc for String class and its methods:
    Java Platform SE 6
    Find String in the lower left, click on it and the doc will display in the main window.

    Also look at the doc for the Scanner class for samples.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    I added a string above: String str;

    But now, at
    if(str == '+'){
    I get "Incompatible operand types String and char"
    Why is that?

    If I change it to

    if(str = +){
    the + sign gets an error.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    To compare Strings, you need to use the equals() method:
    str.equals("+")

    I get "Incompatible operand types String and char"
    Why is that?
    String and char are not compatible. String is an object, char is a primitive.

  13. The Following User Says Thank You to Norm For This Useful Post:

    YourCrazyFriend (March 2nd, 2012)

  14. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    I fixed it Thank you so much

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    You're welcome.

  16. #15
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with creating simple calculater

    Ended up with making this:

    import java.util.Scanner;
     
    class apples{
    	public static void main(String args[]){
    		Scanner scn =  new Scanner(System.in);
    		double firstnumber, secondnumber, answer;
    		String str;
     
    		System.out.println("Enter first number: ");
    		firstnumber = scn.nextDouble();
     
    		System.out.println("Enter method: ");
    		str = scn.next();
     
    		if(str.equals("+")){
    			System.out.println("Enter second number: ");
    			secondnumber = scn.nextDouble();
     
    			answer = firstnumber + secondnumber;
    			System.out.println(firstnumber + " plus " + secondnumber + " equals " + answer);
     
    			}else if(str.equals("-")){
    				System.out.println("Enter second number: ");
    				secondnumber = scn.nextDouble();
     
    				answer = firstnumber - secondnumber;
    				System.out.println(firstnumber + " minus " + secondnumber + " equals " + answer);
     
    			}else if(str.equals("*")){
    				System.out.println("Enter second number: ");
    				secondnumber = scn.nextDouble();
     
    				answer = firstnumber * secondnumber;
    				System.out.println(firstnumber + " multiplied with " + secondnumber + " equals " + answer);
     
    			}else if(str.equals("/")){
    				System.out.println("Enter second number: ");
    				secondnumber = scn.nextDouble();
     
    				answer = firstnumber / secondnumber;
    				System.out.println(firstnumber + " divided by " + secondnumber + " equals " + answer);
     
    			}else if(str.equals(":")){
    				System.out.println("Enter second number: ");
    				secondnumber = scn.nextDouble();
     
    				answer = firstnumber / secondnumber;
    				System.out.println(firstnumber + " divided by " + secondnumber + " equals " + answer);
     
    			}else if(str.equals("%")){
    				System.out.println("Enter second number: ");
    				secondnumber = scn.nextDouble();
     
    				answer = firstnumber % secondnumber;
    				System.out.println("The leftovers from dividing " + firstnumber + " by " + secondnumber + " equals " + answer);
     
    			}else if(str.equals("sqrt")){
    				answer = Math.pow(firstnumber, 0.5);
    				System.out.println("The squareroot of " + firstnumber + " equals " + answer);
     
    			}else if(str.equals("squareroot")){
    				answer = Math.pow(firstnumber, 0.5);
    				System.out.println("The squareroot of " + firstnumber + " equals " + answer);
     
    			}else if(str.equals("^")){
    				System.out.println("Enter second number: ");
    				secondnumber = scn.nextDouble();
     
    				answer = Math.pow(firstnumber, secondnumber);
    				System.out.println(firstnumber + " ^ " + secondnumber + " equals " + answer);
     
    			}else{
    				System.out.println("Error, please try another method.");
    			}
    	}
    }
    Last edited by YourCrazyFriend; March 2nd, 2012 at 10:22 AM.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with creating simple calculater

    Your posted code would be easier to read if you used code tags.
    [code] ...[/code]

Similar Threads

  1. Replies: 3
    Last Post: November 10th, 2011, 07:11 AM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. Creating simple Jtable
    By Dharmarajan in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2011, 10:24 AM
  4. Replies: 8
    Last Post: December 9th, 2009, 04:45 PM