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: In a simple calculator the divison is not working for while loop

  1. #1
    Junior Member
    Join Date
    Aug 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default In a simple calculator the divison is not working for while loop

    I wanted to take the input for operand2 from user. That's is why I wrote the method division.
    and I am calling that method before my while loop inside main. So I did not think that I had to initialize the operand2 value before while loop.
    If I initialize operand2 then every time the program will execute with the same value. Can anybody please help me.
    thanks.



     
    package labs1301;
    import java.util.Scanner;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax. swing.JOptionPane;
    import java.util.Scanner; 
    import java.util.Scanner; 
    import java.util.Scanner; 
     
    import java.util.Scanner; 
    public class Java1301lab6 {
     
    	    private static String Operand2 = null;
     
    		public static void main(String[] args) {
     
    	        JFrame frame = new JFrame("My Lab6 Calculator");
     
    	        String author = "Khandoker Rahad"; // TASK 1: this requires your attention ***********************
    	        JOptionPane.showMessageDialog(frame, "Welcome to " + author + "'s calculator!");
     
    	        // Now, let's ask the user to pick an operation among a set (bulk memory) of defined operators
    	        String[] operators = {"+", "-", "*", "/"};
     
    	        // Selecting an operator *******************************************************************************
    	        String chosenOperator = (String) JOptionPane.showInputDialog(frame, 
    	                                                                    "Which operation do you want to use?",
    	                                                                    "Chosen Operator",
    	                                                                    JOptionPane.QUESTION_MESSAGE, 
    	                                                                    null, 
    	                                                                    operators, 
    	                                                                    operators[0]);
    	        while (chosenOperator == null) {
    	            chosenOperator = (String) JOptionPane.showInputDialog(frame, 
    	                                                                    "Please select an operator",
    	                                                                    "Chosen Operator",
    	                                                                    JOptionPane.QUESTION_MESSAGE, 
    	                                                                    null, 
    	                                                                    operators, 
    	                                                                    operators[0]);    
    	        }
     
    	        // Selecting operands ***********************************************************************************
    	        String Operand1 = (String) JOptionPane.showInputDialog(frame, 
    	                                                                    "What's your first operand?",
    	                                                                    "Operand 1",
    	                                                                    JOptionPane.QUESTION_MESSAGE 
    	                                                              );
     
    			 Operand2 = (String) JOptionPane.showInputDialog(frame, 
    	                                                                    "What's your second operand?",
    	                                                                    "Operand 2",
    	                                                                    JOptionPane.QUESTION_MESSAGE );
    	          float operand1 = Float.valueOf(Operand1);                                           
     
    			// float operand2;
     
    	        division();
     
    			//float operand2 = 5000;
     
    			while(operand2==0){
     
    					JOptionPane.showMessageDialog(frame, "Enter a valid value " );
     
     
    				 operand2 = Float.valueOf(Operand2);
     
     
     
    				}
     
     
    				//System.out.print("Enter valid value");
    				 //operand2 = float.valueOf(Operand2);	
    			//}
    	        // TASK 2 ************************************************************************************************
    	        // Here you should get operand2 in the same fashion as you got operand1 in the first place
     
    	        // TASK 3 ************************************************************************************************
    	        // Now think of it: if your operator is a division, you should not accept 0 as the second operand
    	        // So you have to put in place a while loop (similar to the one about the choice of operator) that keeps
    	        // prompting the user to enter a second operand while the user enters 0
     
    	        if (chosenOperator.compareTo("/") == 0)
    	            JOptionPane.showMessageDialog(frame, "Your result is " + ( operand1 /operand2) + "!");
    	        // TASK 4 ************************************************************************************************
    	        // Here you need to add the other operators
     
     
    	       // System.exit(0);
    	    }
     
    	    public static void division()
    	    {
     
    	    	// <-- Note that Java is case sensitive :) 
     
    	    	float operand2 = Float.valueOf(Operand2); 
    	    	//Scanner sc=new Scanner(System.in); 
     
    			// int operand=sc.nextInt();  
    			// return operand;
    	    } 
     
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,479
    Thanks
    53
    Thanked 2,406 Times in 2,359 Posts

    Default Re: In a simple calculator the divison is not working for while loop

    take the input for operand2 from user. That's is why I wrote the method division.
    For the division method to get user input, it could use a Scanner class method. The name should be something like: getInput instead of division. Names should describe what the method does or what a variable contains.
    Also the method needs to be defined to return a value instead of void.
    The returned value needs to be saved in a variable so it can be used:
    aVariable = someMethod(); // save returned value in aVariable

    --- Update ---

    Also posted at: https://coderanch.com/t/683445/java/simple-calculator
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,479
    Thanks
    53
    Thanked 2,406 Times in 2,359 Posts

    Default Re: In a simple calculator the divison is not working for while loop

    Also posted at: https://coderanch.com/t/683445/java/simple-calculator
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple Calculator HELP
    By krowley in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 20th, 2014, 08:55 PM
  2. Simple Calculator
    By Spanky13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 25th, 2013, 05:33 PM
  3. I have tried making a simple random calculator and its not working?
    By StackOfCookies in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 04:00 PM
  4. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM
  5. switch statement not working for JFrame Calculator
    By kaddyshack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2011, 10:35 AM