Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Calculator program in java

Hi, im trying to program a simple Calculator program with java.. craig dosent explain alot of the things i did here, they are from google. here is the code :

import java.io.Console;


public class Calculator {

    public static void main(String[] args) {
    Console console = System.console();

        console.printf("Welcome to the calculator program! \n");
        String name = console.readLine("Enter your username: ");
        console.printf("Hello, %s!\n", name);

      while(true){


        try
        {
          String numberAsString = console.readLine("Please enter a number : ");
          int number = Integer.parseInt(numberAsString);

          String operation = console.readLine("Please enter an operation +/-/*/ : ");

          String numberTwoAsString = console.readLine("Please enter another number: ");
          int number2 = Integer.parseInt(numberTwoAsString);


          if(operation.equals("+"))
          {
           console.printf("The result is " + (number+number2));
          }
          else if(operation.equals("-"))
          {
           console.printf("The result is " + (number-number2));

          }else if(operation.equals("*"))
          {
           console.printf("The result is" + (number*number2));
          }




        }

        catch(NumberFormatException e)
        {
          console.printf("U have entered invalid input, Try again..\n ");
          continue;
        }


        String choice = console.readLine("Do u want to make a calculation again? Press Y or N\n");
        if(choice.equalsIgnoreCase("N"))
        {
          break;
        }
        else
        {
         continue;
        }



      }


    }










    }

I have few questions:

  1. i am struggling to block users to enter valid operations. 2.Any best practices with the code?

thanks