Q:

Don't understand what to insert into while loop to work correctly

import java.util.Scanner;

public class windowstore {

public static void main(String[] args) {

    int width;
    int height;
    int area;
    int perm;
    int costarea;
    int costperm;
    float glasscost;
    float trimcost;

    width = 0;
    height = 0;
    glasscost = (float) 3.50;
    trimcost = (float) 2.25;

    Scanner sc = new Scanner(System.in);

    String stringheight = "";
    String stringwidth = "";

    System.out.println("What is the height of your window?");

    stringheight = sc.nextLine();
    height = Integer.parseInt(stringheight);

        try {
            height = Integer.parseInt(stringheight);
        } catch (Exception ex) {
            System.out.println("Please enter a number not a word or letter");
        }

    while (height > 20 || height < 1) {

        if (height > 20) {
            System.out.println("Sorry " + height + " is above 20. Please enter a number below 20.");
            System.out.println("What is the height of your window?");

            stringheight = sc.nextLine();
            height = Integer.parseInt(stringheight);

        }
        if (height < 1) {
            System.out.println("Sorry " + height + " is below 1. Please enter a number above 1.");
            System.out.println("What is the height of your window?");

            stringheight = sc.nextLine();
            height = Integer.parseInt(stringheight);
        }
    }
    System.out.println("What is the width of your window?");

    stringwidth = sc.nextLine();

    height = Integer.parseInt(stringheight);

    width = Integer.parseInt(stringwidth);
    while (width > 20 || width < 1) {

        if (width > 20) {
            System.out.println("Sorry " + width + " is above 20. Please enter a number below 20.");
            System.out.println("What is the width of your window?");
            stringwidth = sc.nextLine();

            height = Integer.parseInt(stringheight);

            width = Integer.parseInt(stringwidth);
        }
        if (width < 1) {
            System.out.println("Sorry " + width + " is below 1. Please enter a number above 1.");
            System.out.println("What is the width of your window?");

            stringwidth = sc.nextLine();

            height = Integer.parseInt(stringheight);

            width = Integer.parseInt(stringwidth);
        }
    }

    area = width * height;

    System.out.println("The area of your window is " + area + " inches.");

    perm = width + width + height + height;

    System.out.println("The perimeter of your window is " + perm + " inches.");

    costarea = (int) (area * glasscost);

    System.out.println("The cost of glass for your window is " + costarea);

    costperm = (int) (perm * trimcost);

    System.out.println("The cost of trim for your window is " + costperm);

  }
}

I'm currently struggling to make the program ask the user to "Please enter a number not a word or letter" every single time they input a character that is not an integer. But when they do put an integer the code will run normally. 

A:

Here's an example which probably shows what you need:
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int height;
    while (true) {
        System.out.println("What is the height of your window?");
        String cmd = sc.nextLine();
        try {
            height = Integer.parseInt(cmd);
            break;
        } catch (Exception ex) {
            System.out.println("Please enter a number not a word or letter");
        }
    }
    System.out.println("You typed: " + height);
}

