Page 2 of 2 FirstFirst 12
Results 21 to 27 of 27
Like Tree2Likes

Thread: Need help with + - * / calculator

  1. #21
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    If I say yes for the add, it will take my first number, then my second. It will output my answer but it will also use that answer for the following question which is 'Do you want to subtract?'.

  2. #22
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    In case anyone wants the code:

    Java Code:
    import java.util.Scanner;
    
    public class calculator {
    	public static void main(String args[]) {
    		Scanner input = new Scanner(System.in);
    		double fnum, snum, answer;
    		String name;
    		System.out.println("Do you want to add?");
    		name = input.nextLine();
    		if (name.equalsIgnoreCase("Yes")) {
    			System.out.println("Please enter first number:");
    			fnum = input.nextDouble();
    			System.out.println("Please enter second number:");
    			snum = input.nextDouble();
    			answer = fnum + snum;
    			System.out.println(answer);
    		} else {
    			System.out.println("okay");
    		}
    		
    		System.out.println("Do you want to subtract?");
    		name = input.nextLine();
    		if (name.equalsIgnoreCase("Yes")) {
    			System.out.println("Please enter first number:");
    			fnum = input.nextDouble();
    			System.out.println("Please enter second number:");
    			snum = input.nextDouble();
    			answer = fnum - snum;
    			System.out.println(answer);
    		} else {
    			System.out.println("okay");
    
    		}
    		System.out.println("Do you want to multiply?");
    		name = input.nextLine();
    		if (name.equalsIgnoreCase("Yes")) {
    			System.out.println("Please enter first number:");
    			fnum = input.nextDouble();
    			System.out.println("Please enter second number:");
    			snum = input.nextDouble();
    			answer = fnum * snum;
    			System.out.println(answer);
    		} else {
    			System.out.println("okay");
    		}
    		System.out.println("Do you want to divide?");
    		name = input.nextLine();
    		if (name.equalsIgnoreCase("Yes")) {
    			System.out.println("Please enter first number:");
    			fnum = input.nextDouble();
    			System.out.println("Please enter second number:");
    			snum = input.nextDouble();
    			answer = fnum / snum;
    			System.out.println(answer);
    		} else {
    			System.out.println("okay");
    		}
    
    	}
    }

  3. #23
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    What numbers are you inputting for the addition part, and what numbers are you inputting for the subtraction part?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  4. #24
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    just any old numbers, it just uses the answer to answer the next question.
    I'm using integers.

  5. #25
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    The problem seems to be arising from the nextLine() statement. I remember a scenario like this from before. It doesn't even ask if the user wants to subtract. For the purposes of making your program work, you can replace nextLine() with next(). As far as why nextLine() is causing a problem, maybe Furarable or somebody else can provide some insight.
    Last edited by awinston; 08-04-2012 at 07:54 PM.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  6. #26
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Need help with + - * / calculator

    Here is an article about why this issue occurs: Stop Input Skipping in Java | Ryan Rampersad

    Apparently it happens whenever nextInt() is followed by nextLine(). The article above suggests a solution of having a meaningless nextLine() in order to bypass the skipping, but I can't believe that this is the best way to solve the problem.
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  7. #27
    Aconitum is offline Member
    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Default Re: Need help with + - * / calculator

    changing it to .next is the best way to solve it, works perfectly now. Thank you so much for your help!
    awinston likes this.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. calculator help
    By sirstroud in forum New To Java
    Replies: 0
    Last Post: 04-01-2012, 06:58 AM
  2. Help with AWT CALCULATOR
    By Megan Dosnueve in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2011, 06:49 PM
  3. Help in a calculator
    By Ayannie in forum New To Java
    Replies: 6
    Last Post: 01-04-2011, 09:21 PM
  4. Calculator
    By water in forum AWT / Swing
    Replies: 4
    Last Post: 09-23-2009, 07:00 AM
  5. help with calculator
    By kalibballer in forum New To Java
    Replies: 8
    Last Post: 04-01-2009, 01:57 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
  •