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

Thread: Problem with basic calculator - Beginner

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with basic calculator - Beginner

    Hello community ^_^

    I'm fairly new to the Java language and programming so bare with me on this one. I'm teaching myself and I'm using every resource I can to get better.

    I'm currently learning about methods and I got a little creative.
    I've created a simple calculator program implementing the use of methods (I'm not sure whether this is the correct way, but I'm doing it anyway)

    There are two problems I'm having with the code:

    1. It's not letting the user input Yes or No when the it gets to "input = nextLine();". Instead, it skips over this and ends the program.

    2. I'm not sure what to put in the if statement or in general to have the code loop back to the beginning of the program and start over instead of having to run the program over again.

    I appreciate all the help I can get.

    Thanks

        static int number;
        static String passing, input;
        static int numOne, numTwo, numThree;
     
        static Scanner sc = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            System.out.println("Input first number");
            numOne = sc.nextInt();
     
            System.out.println("Input second number");
            numTwo = sc.nextInt();
     
            System.out.println("What would you like to do with these numbers?");
            System.out.println("1. Addition "
                    + "2. Subtraction "
                    + "3. Multiplication "
                    + "4. Division");
            System.out.print(" Enter number: ");
     
             anotherOption();
     
             System.out.println(Recieving());
             System.out.println("Yes/No");
     
              input = sc.nextLine();
     
             if(input.equals("Yes")){
              //  back to the beginning
             }
     
             else if(input.equals("No")){
                 System.exit(0);
             }
     
        }
     
        public static void anotherOption() {
     
            number = sc.nextInt();
     
            switch(number) {
                case 1: 
                    System.out.println(numOne + numTwo);
                    break;
                case 2:
                    System.out.println(numOne - numTwo);
                    break;
                case 3:
                    System.out.println(numOne * numTwo);
                    break;
                case 4:
                    System.out.println(numOne / numTwo);
                    break;
                default:
                    System.out.println("Invalid input");
                    break;
     
     
            }
     
            passing = "Would you like to do anything else?";
     
        }
     
        public static String Recieving(){
            return passing;
        }
    }


  2. #2
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Problem with basic calculator - Beginner

    well you have to make it loop back to the beginning you cant just add a comment and say //back to the beginning and expect the program to think yeap he wants to start over

    the way you have it right now is if answer is yes do nothing

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with basic calculator - Beginner

    Quote Originally Posted by derekxec View Post
    well you have to make it loop back to the beginning you cant just add a comment and say //back to the beginning and expect the program to think yeap he wants to start over

    the way you have it right now is if answer is yes do nothing
    Lol, I didn't add the comment expecting the program to all of a sudden go back to the beginning.
    I added it so I would know what I code to replace the comment with.

    Another reason I added the comment is because the IDE will not record the user's input into "input = sc.nextLine;".

    I wanted to know why my code is doing that. After that, I wanted to know what code is appropriate to fill in for the comment.

  4. #4
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: Problem with basic calculator - Beginner

    lol sorry...you could make a boolean and set it to true and put everything in a while loop and set the value to false when the user puts in no

    and you can use next instead of nextLine...also just remember the way you have it right now the Yes and No have to Yes and No not yes and no

Similar Threads

  1. Help with beginner's calculator
    By CrashOverride in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 14th, 2012, 06:16 PM
  2. Calculator beginner tutorial(s)
    By Mr.Greedy in forum Java Theory & Questions
    Replies: 4
    Last Post: June 13th, 2012, 05:53 PM
  3. A basic calculator
    By SathApoc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 22nd, 2011, 12:47 PM
  4. having bad time with basic calculator code
    By hashey100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 09:29 PM
  5. Basic Calculator
    By Yes. in forum What's Wrong With My Code?
    Replies: 13
    Last Post: June 4th, 2010, 04:24 PM