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

Thread: I cant figure out why this code isn't doing what i want.

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I cant figure out why this code isn't doing what i want.

    So I am trying to make a simple calculator using java code. I want it to ask the user what he/she wants to do, then based on the answer, execute the code. For some reason it just closes after asking "Is there anything else you want to do". Can anyone help?

    Here's the code:
    import java.util.Scanner;
     
    public class calculator
    {
        public static void main(String[] args )
        {
            Scanner user_input = new Scanner(System.in);
            System.out.print("Is there anything you would you like to do?");        
            if(user_input.next() .equals("no"))
                {System.out.println("Ok thanks for trying this program");}
            if(user_input.next() .equals("yes"))
                {System.out.println("Ok what would you ike to do?");}
            if(user_input .equals("multiply"))      
            {
                double firstNo, secondNo;        
                System.out.println("Please input the first number ");
                firstNo = Double.parseDouble(user_input.next());
                System.out.println("Please input the second number ");
                secondNo = Double.parseDouble(user_input.next());
                double finalProduct= firstNo * secondNo;
                System.out.println("The final product is "+finalProduct );
                System.out.println("Is there anything else you want me to do?");
                if(user_input.next() .equals("no"))
                {System.out.println("Ok thanks for trying this program");}
            }  
            if(user_input.next() .equals("divide"))      
            {
                double firstNo, secondNo;        
                System.out.println("Please input the first number ");
                firstNo = Double.parseDouble(user_input.next());
                System.out.println("Please input the second number ");
                secondNo = Double.parseDouble(user_input.next());
                double finalProduct= firstNo / secondNo;
                System.out.println("The final product is "+finalProduct );
                System.out.println("Is there anything else you want me to do?");
                if(user_input.next() .equals("no"))
                {System.out.println("Ok thanks for trying this program");}
            }
            if(user_input.next() .equals("add"))      
            {
                double firstNo, secondNo;        
                System.out.println("Please input the first number ");
                firstNo = Double.parseDouble(user_input.next());
                System.out.println("Please input the second number ");
                secondNo = Double.parseDouble(user_input.next());
                double finalProduct= firstNo + secondNo;
                System.out.println("The final product is "+finalProduct );
                System.out.println("Is there anything else you want me to do?");
                if(user_input.next() .equals("no"))
                {System.out.println("Ok thanks for trying this program");}
            }
        }
    }
    Last edited by NyitStudent07; January 24th, 2019 at 07:34 PM.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Your code is difficult to read. Please ensure that the if/else clauses are indented properly and then place between code tags to preserver formatting. See BBCodes below to see how to use code tags.

    Regards,
    Jim

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    it just closes after asking
    Please copy the full contents of the console window from when you execute the program and paste it here so we can see what you are talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Attachment 3485
    Ok here it is. Also i edited the java. thanks.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Please post items as text in the thread, not as images or attachments.

    --- Update ---

    Is it the case that in the list of if statements in the code, that only one of them is expected to be true?
    Normally when that is the case, multiple, mutually exclusive if statements are coded as if/else if/else statements to show that only one is expected to be true and when that one is found, the remaining if statements are skipped.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    oh. How would i do that. Is there an elseif statement?
    Edit: Ok i found out there is , now the only problem is that the program terminates right after doing one instance of itself. How do i link the end back to the beginning again? do i use a while loop. If so, how? Untitled.jpg
    Last edited by NyitStudent07; January 24th, 2019 at 09:45 PM.

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    You have one fundamental problem. You take input from the Scanner to see if the answer is "no." But then you take input from the Scanner to see if the answer is "yes." You needed to use the original input. Also here are some suggestions:

    1. Why check for yes if no is not entered? Wouldn't that be the only other alternative? If you check for yes and no, what are you going to do if neither is entered?
    2. What happens if someone enters mixed case? Check the String class for some possible ways to cater to this problem.
    3. The scanner has a nextDouble() method. No need to read a string and convert.

    Regards,
    Jim

  8. #8
    Junior Member
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Oh thanks. ill look into that. Ill come back if i run into anything. Try to solve it myself tho

  9. #9
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Excellent attitude! Someone will be here to help.

    Regards,
    Jim

  10. #10
    Junior Member
    Join Date
    Jan 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    Ok I solved part of it. I put a while clause before the yes and no if's. I got rid of the yes if and that just made the whole thing repeat. The only thing that isnt working as good as it could be is that i have to enter the yes or no multiple time, and that it doesnt end if i say noo. Ill keep working on it but thanks for the help!! ������

    --- Update ---

    Ok It worked!! All i did was add a break in the while loop . Thanks for all the help . Here is the finished code:
    import java.util.Scanner;
     
    public class calculator
    {
        public static void main(String[] args )
        {
            Scanner user_input = new Scanner(System.in);
            System.out.println("Is there anything you would you like to do?");        
            while (true)
            {if(user_input.next() .equals("no"))
                {System.out.println("Ok thanks for trying this program");
                    break;}
                else 
                {System.out.println("Ok what would you ike to do? The choices are : multiply, divide, and add");
                    if(user_input.next() .equals("multiply"))      
                    {
                        double firstNo, secondNo;        
                        System.out.println("Please input the first number ");
                        firstNo = user_input.nextDouble();
                        System.out.println("Please input the second number ");
                        secondNo = user_input.nextDouble();
                        double finalProduct= firstNo * secondNo;
                        System.out.println("The final product is "+finalProduct );
                        System.out.println("Is there anything else you want me to do?");
                        if(user_input.next() .equals("no"))
                        {System.out.println("Ok thanks for trying this program");
                            break ;}
     
                    }  
                    else if(user_input.next() .equals("divide"))      
                    {
                        double firstNo, secondNo;        
                        System.out.println("Please input the first number ");
                        firstNo = user_input.nextDouble();
                        System.out.println("Please input the second number ");
                        secondNo = user_input.nextDouble();
                        double finalProduct= firstNo / secondNo;
                        System.out.println("The final product is "+finalProduct );
                        System.out.println("Is there anything else you want me to do?");
                        if(user_input.next() .equals("no"))
                        {System.out.println("Ok thanks for trying this program");
                            break;}
                    }
                    else if(user_input.next() .equals("add"))      
                    {
                        double firstNo, secondNo;        
                        System.out.println("Please input the first number ");
                        firstNo = user_input.nextDouble();
                        System.out.println("Please input the second number ");
                        secondNo = user_input.nextDouble();
                        double finalProduct= firstNo + secondNo;
                        System.out.println("The final product is "+finalProduct );
                        System.out.println("Is there anything else you want me to do?");
                        if(user_input.next() .equals("no"))
                        {System.out.println("Ok thanks for trying this program");
                            break;}
                    }
                }
            }    
        }
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: I cant figure out why this code isn't doing what i want.

    There is some non-standard coding used:
    There shouldn't be a statement on the same line following a {
    The } should not be hidden at the end of a statement.

    What is the required input to do the add operation? How many times do you have to enter "add"?
    Most users would expect to have to enter an operation just one time.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can you please help me figure out what's wrong with my code?
    By Edmacelroy in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 28th, 2013, 05:15 PM
  2. Help with java code that I can't figure out
    By redbull in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 9th, 2013, 05:28 PM
  3. Cannot figure out how to code this...
    By compsci21 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 20th, 2012, 11:56 PM
  4. Cant figure out how to reset my code
    By charliechawk01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2012, 04:45 PM
  5. my code isn't working an i can't figure out why
    By jack13580 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 12:21 PM