Q:

Java compressing some code

Very new to Java and I am looking to change the following to allow the same variable call to occur only once.
The second "b" variable is only called during rectangle and triangle. This does work just want to see if I can get that one extra line out of the main.
The "b" variable can not be moved out from the if statements as the program will not start as the user will only enter the a variable.
import java.util.*;

public class Main {

    public static void main(String[]args) {
        Scanner in = new Scanner(System.in);
        System.out.print("? ");
        String word = in.next();    
        Shape s = null;
        while (!word.equals("quit")) {
            double a = in.nextDouble();
            if (word.equals("triangle")){
                double b = in.nextDouble();
                s = new Shapet (a, b);
            }else if (word.equals("rectangle")){
                double b = in.nextDouble();
                s = new Shaper (a, b);          
            }else if (word.equals("square")){
                s = new Shapes (a);             
            }else if (word.equals("circle")){
                s = new Shapec (a);             
            }else if (word.equals("pentagon")){
                s = new Shapep (a);             
            }
            System.out.printf("Area of %s = %.2f\n", s, s.area());      
            System.out.print("? ");
            word = in.next();
        }
    }
}

A:

If you simply want to reduce lines of code, possibly you can go for inline code:
double a;
while (!word.equals("quit")) {
  a = in.nextDouble();
  if (word.equals("triangle")) {
    s = new Shapet(a, in.nextDouble());
  } else if (word.equals("rectangle")) {
    s = new Shaper(a, in.nextDouble());
  } else if (word.equals("square")) {
    s = new Shapes(a);
  }
  // ...
}

P.S. Avoid declaring variables inside loops

