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 3 of 3

Thread: Help with simple calculator

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with simple calculator

    First time using the forums and I got this assignment to make a calculator, which I have down and know how to do. It has to loop through asking for an Integer and Double 4 times, which I know how to do. The problem I'm having when ever I enter two numbers , it's supposed to give the sum, difference, and product, quotient and remainder, but when I enter a 0 for the second number, it's supposed to give the sum, difference, and product, but say "cannot divide by 0" for the quotient and remainder. When I enter a 0 for the second number of my Double, it works fine and tells me I cannot divide by 0. When I enter the second number for my Integer, it gives me an error. Please help!
    package methodVersions;
     
    import java.util.Scanner;
     
    public class Method_Void {
     
    	public static void main(String[] args) {
     
    		System.out.println("This program was written by Daniel Pytlarz");
    		System.out.println("-----------------------------");
    		String varName;
            System.out.print("Enter your name: ");
            Scanner readStuff = new Scanner(System.in);
            varName = readStuff.nextLine();
     
            for (int loop = 0; loop <5; ++loop){
     
            		//Integers
     
            	System.out.println("");
            	System.out.println("Trial " + (loop+1) + " Integers: ");
    	        int fNum, sNum;
    	        System.out.print("Please enter your first number: ");
    	        fNum = readStuff.nextInt();
    	        System.out.print("Please enter your second number: ");
    	        sNum = readStuff.nextInt();
     
    	        System.out.println("");
     
    	        add(varName, fNum, sNum); //firstNum + secondNum;
     
    	        sub(varName, fNum, sNum); //firstNum - secondNum;
     
    	        mult(varName, fNum, sNum); //firstNum * secondNum;
     
    	        qotremain(varName, fNum, sNum); //firstNum / secondNum & firstNum % secondNum;
     
     
     
     
    	        //Doubles
     
    	        System.out.println("");
            	System.out.println("Trial " + (loop+1) + " Doubles: ");
    	        Double fNumD, sNumD;
    	        System.out.print("Please enter your first number: ");
    	        fNumD = readStuff.nextDouble();
    	        System.out.print("Please enter your second number: ");
    	        sNumD = readStuff.nextDouble();
     
    	        System.out.println("");
     
    	        addD(varName, fNumD, sNumD); //firstNum + secondNum;
     
    	        subD(varName, fNumD, sNumD); //firstNum - secondNum;
     
    	        multD(varName, fNumD, sNumD); //firstNum * secondNum;
     
    	        qotremainD(varName, fNumD, sNumD); //firstNum / secondNum & firstNum % secondNum;
     
     
     
     
     
            }
     
            System.out.println("-----------------------------");
            System.out.println("This program was run by " + varName);
            System.out.println("-----------------------------");
     
            readStuff.close();
     
        }
     
    			//Integer Methods
     
    	        public static void add(String z, int x, int y){
    	        	int sum;
    	        	sum = x + y;
    	        	System.out.println(z + ", the sum of " + x + " and " + y + " is " + sum);
     
    	        }
     
    	        public static void sub(String z, int x, int y){
    	        	int diff;
    	        	diff = x - y;
    	        	System.out.println(z + ", the difference of " + x + " and " + y + " is " + diff);
     
    	        }
     
    	        public static void mult(String z, int x, int y){
    	        	int prod;
    	        	prod= x * y;
    	        	System.out.println(z + ", the product of " + x + " and " + y + " is " + prod);
     
    	        }
     
    	        public static void qotremain(String z, int x, int y){
    	        	int qot;
    	        	int remain;
    	        	qot = x / y;
    	        	remain = x % y;
    	        	if(y == 0){ 
    		        	System.out.println(z + ", you cannot divide by 0. Remainder and quotent not available. ");
    		        } else{
    		        	System.out.println(z + ", the quotient of " + x + " and " + y + " is " + qot);
    		        	System.out.println(z + ", the remainder of " + x + " and " + y + " is " + remain);
    		  	          }
     
    	        }
     
     
     
     
     
    	      //Double Methods
     
    	        public static void addD(String z, double x, double y){
    	        	double total;
    	        	total = x + y;
    	        	System.out.println(z + ", the sum of " + x + " and " + y + " is " + total);
     
    	        }
     
    	        public static void subD(String z, double x, double y){
    	        	double diff;
    	        	diff = x - y;
    	        	System.out.println(z + ", the difference of " + x + " and " + y + " is " + diff);
     
    	        }
     
    	        public static void multD(String z, double x, double y){
    	        	double prod;
    	        	prod = x * y;
    	        	System.out.println(z + ", the product of " + x + " and " + y + " is " + prod);
     
    	        }
     
    	        public static void qotremainD(String z, double x, double y){
    	        	double qot;
    	        	double remain;
    	        	qot = x / y;
    	        	remain = x % y;
    	        	if(y == 0){ 
    		        	System.out.println(z + ", you cannot divide by 0. Remainder and quotent not available. ");
    		        } else{
    		        	System.out.println(z + ", the quotient of " + x + " and " + y + " is " + qot);
    		        	System.out.println(z + ", the remainder of " + x + " and " + y + " is " + remain);
    		  	          }
     
    	        }
     
     
     
     
     
    	}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with simple calculator

    For the integer dividing by 0 error, the program attempts the division before checking to see if it's an allowed operation in the method qotremain(). Fix the order of operations to prevent the error from occurring.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    GMPoison (September 22nd, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with simple calculator

    Quote Originally Posted by GregBrannon View Post
    For the integer dividing by 0 error, the program attempts the division before checking to see if it's an allowed operation in the method qotremain(). Fix the order of operations to prevent the error from occurring.
    Thank you, I knew it would be something stupid

Similar Threads

  1. Simple Calculator
    By Spanky13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2013, 05:33 PM
  2. Simple Commission Calculator
    By mrivera8504 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: August 27th, 2012, 06:35 PM
  3. [SOLVED] Simple calculator issue
    By ikocijan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2012, 02:43 AM
  4. Need help building a simple calculator
    By zigma in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 14th, 2011, 01:13 AM
  5. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM