1
import java.util.Scanner;

public class InteractiveRectangle
{
    public static void main(String[] args)
    {

         int height = readPositiveInteger("Enter the height");

         int width = readPositiveInteger("Enter the width");

         printRectangleDetails(height, width);
    }

    /**
     * Read in an integer and return its value
     * @param the prompt to be shown to the user
     */
     public static int readInteger(String prompt)
    {

        Scanner scan = new Scanner(System.in);

        System.out.println(prompt);

        while (!scan.hasNextInt()) // while non-integers are present...
        {
            scan.next(); //...read and discard input, then prompt again

            System.out.println ("Bad input. Enter an integer");
         }

        int firstInteger = scan.nextInt();

        return firstInteger;

    }

    /**
     * Read in an integer greater than 0 and return its value
     * @ param the prompt to be shown to the user
     */
     public static int readPositiveInteger(String prompt)
    {
        int input = 0;

        Scanner scan = new Scanner(System.in);

        boolean first = false;

        while (!scan.hasNextInt())
        {
            int quantity = scan.nextInt();

            if (quantity > 0)
        {
            first = true;

            quantity = scan.nextInt();

        } 
            else if (quantity <= 0)
        {
            System.out.println("Bad input. Enter a positive integer.");
        }

        }
         return input;
   }

     /**
     * Returns the area of a rectangle
     * @param height the height of the rectangle
     * @param width the width of the rectangle
     */

      public static int area (int height, int width)
     {
        return height * width;
      }

     /**
      * Returns the perimeter of a retangle
      * @param height the height of the rectangle
      * @param width the width of the rectangle
      */
      public static int perimeter (int height, int width)
      {
         return (height * 2) + (width * 2);
      }

    /**
     * Prints the area, the perimeter, the length and the width 
     * @param heigth the height of the rectangle
     * @param width the width of the rectangle
     */
     public static void printRectangleDetails (int height, int width)
    {
        System.out.println("The height is " + height);

        System.out.println("The width is " + width);

        System.out.println("The area is " + area(height, width));

        System.out.println("The perimeter is " + perimeter(height, width));
    }
}

This is my program so far, but I have some problems with the readPositiveInteger method and I also have to write a method that asks the user if he wants another rectangle typing "y" if he wants it and "n" if he doesn't using a while loop. Thank you for

Konrad Krakowiak
  • 11,733
  • 10
  • 53
  • 44
Feih94
  • 11
  • 3

2 Answers2

1

Use do-while to grantee that your code a least get executed once then ask then user if he wants to calculate result again

char ch = 'y';
do{
int height = readPositiveInteger("Enter the height");
int width = readPositiveInteger("Enter the width");
printRectangleDetails(height, width);
System.out.print("Do you want to calculate other rectangle (y/n) ? : ");
Scanner reader = new Scanner(System.in);
char = reader.next().charAt(0);
}while(ch=='y' || ch=='Y');
karim mohsen
  • 2,051
  • 1
  • 11
  • 18
1

I assume you want the user to confirm each input by pressing enter.
When using scan.nextInt() this will try to read the next integer inside the input, and keep everything else in there which is discussed here Using scanner.nextLine().
I'm not a fan of skipping everything in the line that is not an int and read the first one that is. Because "hello world number 5" should not return 5.

public static int readPositiveInteger(String prompt) {
    int input = 0;

    Scanner scan = new Scanner(System.in);

    while (input <= 0) {
        // ask for a number
        System.out.println(prompt);
        try { // read the number
            input = scan.nextInt();
        } catch (InputMismatchException e){ // catch if there is no number
            input = 0;
        } // read the remainder of the input line
        scan.nextLine();
    }
    return input;
}

public static boolean anotherRound(){
    Scanner scan = new Scanner(System.in);

    System.out.println("Another Round?");
    // read a full line
    String input = scan.nextLine();
    if("y".equals(input)){ // if "y" then true, else false
        return true;
    }
    return false;
}

public static void main(String[] args) {
    do {
        int posInt = readPositiveInteger("Enter a positive integer");
        System.out.println("You entered " + posInt);
    } while (anotherRound());
}
Community
  • 1
  • 1
Aracurunir
  • 615
  • 6
  • 9